@@ -14,35 +14,36 @@ sealed class DateTimeUnit {
14
14
abstract operator fun times (scalar : Int ): DateTimeUnit
15
15
16
16
class TimeBased (val nanoseconds : Long ) : DateTimeUnit() {
17
- internal val calendarUnit : CalendarUnit
18
- internal val calendarScale : Long
17
+ private val unitName : String
18
+ private val unitScale : Long
19
19
20
20
init {
21
21
require(nanoseconds > 0 ) { " Unit duration must be positive, but was $nanoseconds ns." }
22
+ // find a concise string representation for the unit with this duration
22
23
when {
23
24
nanoseconds % 3600_000_000_000 == 0L -> {
24
- calendarUnit = CalendarUnit . HOUR
25
- calendarScale = nanoseconds / 3600_000_000_000
25
+ unitName = " HOUR"
26
+ unitScale = nanoseconds / 3600_000_000_000
26
27
}
27
28
nanoseconds % 60_000_000_000 == 0L -> {
28
- calendarUnit = CalendarUnit . MINUTE
29
- calendarScale = nanoseconds / 60_000_000_000
29
+ unitName = " MINUTE"
30
+ unitScale = nanoseconds / 60_000_000_000
30
31
}
31
32
nanoseconds % 1_000_000_000 == 0L -> {
32
- calendarUnit = CalendarUnit . SECOND
33
- calendarScale = nanoseconds / 1_000_000_000
33
+ unitName = " SECOND"
34
+ unitScale = nanoseconds / 1_000_000_000
34
35
}
35
36
nanoseconds % 1_000_000 == 0L -> {
36
- calendarUnit = CalendarUnit . MILLISECOND
37
- calendarScale = nanoseconds / 1_000_000
37
+ unitName = " MILLISECOND"
38
+ unitScale = nanoseconds / 1_000_000
38
39
}
39
40
nanoseconds % 1_000 == 0L -> {
40
- calendarUnit = CalendarUnit . MICROSECOND
41
- calendarScale = nanoseconds / 1_000
41
+ unitName = " MICROSECOND"
42
+ unitScale = nanoseconds / 1_000
42
43
}
43
44
else -> {
44
- calendarUnit = CalendarUnit . NANOSECOND
45
- calendarScale = nanoseconds
45
+ unitName = " NANOSECOND"
46
+ unitScale = nanoseconds
46
47
}
47
48
}
48
49
}
@@ -57,7 +58,7 @@ sealed class DateTimeUnit {
57
58
58
59
override fun hashCode (): Int = nanoseconds.toInt() xor (nanoseconds shr Int .SIZE_BITS ).toInt()
59
60
60
- override fun toString (): String = formatToString(calendarScale, calendarUnit.toString() )
61
+ override fun toString (): String = formatToString(unitScale, unitName )
61
62
}
62
63
63
64
sealed class DateBased : DateTimeUnit () {
@@ -118,17 +119,3 @@ sealed class DateTimeUnit {
118
119
val CENTURY = YEAR * 100
119
120
}
120
121
}
121
-
122
-
123
- internal enum class CalendarUnit {
124
- YEAR ,
125
- MONTH ,
126
- DAY ,
127
- HOUR ,
128
- MINUTE ,
129
- SECOND ,
130
- MILLISECOND ,
131
- MICROSECOND ,
132
- NANOSECOND
133
- }
134
-
0 commit comments