Skip to content

Commit 6d49559

Browse files
committed
Remove CalendarUnit enum entirely
1 parent bc07761 commit 6d49559

File tree

2 files changed

+17
-30
lines changed

2 files changed

+17
-30
lines changed

core/commonMain/src/DateTimeUnit.kt

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,36 @@ sealed class DateTimeUnit {
1414
abstract operator fun times(scalar: Int): DateTimeUnit
1515

1616
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
1919

2020
init {
2121
require(nanoseconds > 0) { "Unit duration must be positive, but was $nanoseconds ns." }
22+
// find a concise string representation for the unit with this duration
2223
when {
2324
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
2627
}
2728
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
3031
}
3132
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
3435
}
3536
nanoseconds % 1_000_000 == 0L -> {
36-
calendarUnit = CalendarUnit.MILLISECOND
37-
calendarScale = nanoseconds / 1_000_000
37+
unitName = "MILLISECOND"
38+
unitScale = nanoseconds / 1_000_000
3839
}
3940
nanoseconds % 1_000 == 0L -> {
40-
calendarUnit = CalendarUnit.MICROSECOND
41-
calendarScale = nanoseconds / 1_000
41+
unitName = "MICROSECOND"
42+
unitScale = nanoseconds / 1_000
4243
}
4344
else -> {
44-
calendarUnit = CalendarUnit.NANOSECOND
45-
calendarScale = nanoseconds
45+
unitName = "NANOSECOND"
46+
unitScale = nanoseconds
4647
}
4748
}
4849
}
@@ -57,7 +58,7 @@ sealed class DateTimeUnit {
5758

5859
override fun hashCode(): Int = nanoseconds.toInt() xor (nanoseconds shr Int.SIZE_BITS).toInt()
5960

60-
override fun toString(): String = formatToString(calendarScale, calendarUnit.toString())
61+
override fun toString(): String = formatToString(unitScale, unitName)
6162
}
6263

6364
sealed class DateBased : DateTimeUnit() {
@@ -118,17 +119,3 @@ sealed class DateTimeUnit {
118119
val CENTURY = YEAR * 100
119120
}
120121
}
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-

core/commonMain/src/Instant.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public expect fun Instant.periodUntil(other: Instant, zone: TimeZone): DateTimeP
9797

9898
/**
9999
* The return value is clamped to [Long.MAX_VALUE] or [Long.MIN_VALUE] if [unit] is more granular than
100-
* [CalendarUnit.DAY] and the result is too large.
100+
* [DateTimeUnit.DAY] and the result is too large.
101101
*
102102
* @throws DateTimeArithmeticException if this [Instant] or [other] is too large to fit in [LocalDateTime].
103103
*/

0 commit comments

Comments
 (0)