Skip to content

Commit b4b0c01

Browse files
committed
Cleanup some CalendarUnit usages
1 parent 1beb5ea commit b4b0c01

File tree

5 files changed

+25
-38
lines changed

5 files changed

+25
-38
lines changed

core/commonMain/src/Instant.kt

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,6 @@ public fun String.toInstant(): Instant = Instant.parse(this)
7676
*/
7777
public expect fun Instant.plus(period: DateTimePeriod, zone: TimeZone): Instant
7878

79-
/**
80-
* @throws DateTimeArithmeticException if this value or the result is too large to fit in [LocalDateTime].
81-
*/
82-
internal expect fun Instant.plus(value: Int, unit: CalendarUnit, zone: TimeZone): Instant
83-
8479
/**
8580
* @throws DateTimeArithmeticException if this value or the result is too large to fit in [LocalDateTime].
8681
*/
@@ -97,7 +92,7 @@ public expect fun Instant.periodUntil(other: Instant, zone: TimeZone): DateTimeP
9792
*
9893
* @throws DateTimeArithmeticException if this [Instant] or [other] is too large to fit in [LocalDateTime].
9994
*/
100-
internal expect fun Instant.until(other: Instant, unit: CalendarUnit, zone: TimeZone): Long
95+
public expect fun Instant.until(other: Instant, unit: DateTimeUnit, zone: TimeZone): Long
10196

10297
/**
10398
* The return value is clamped to [Int.MAX_VALUE] or [Int.MIN_VALUE] if the result would otherwise cause an arithmetic
@@ -106,7 +101,7 @@ internal expect fun Instant.until(other: Instant, unit: CalendarUnit, zone: Time
106101
* @throws DateTimeArithmeticException if this [Instant] or [other] is too large to fit in [LocalDateTime].
107102
*/
108103
public fun Instant.daysUntil(other: Instant, zone: TimeZone): Int =
109-
until(other, CalendarUnit.DAY, zone).clampToInt()
104+
until(other, DateTimeUnit.DAY, zone).clampToInt()
110105

111106
/**
112107
* The return value is clamped to [Int.MAX_VALUE] or [Int.MIN_VALUE] if the result would otherwise cause an arithmetic
@@ -115,7 +110,7 @@ public fun Instant.daysUntil(other: Instant, zone: TimeZone): Int =
115110
* @throws DateTimeArithmeticException if this [Instant] or [other] is too large to fit in [LocalDateTime].
116111
*/
117112
public fun Instant.monthsUntil(other: Instant, zone: TimeZone): Int =
118-
until(other, CalendarUnit.MONTH, zone).clampToInt()
113+
until(other, DateTimeUnit.MONTH, zone).clampToInt()
119114

120115
/**
121116
* The return value is clamped to [Int.MAX_VALUE] or [Int.MIN_VALUE] if the result would otherwise cause an arithmetic
@@ -124,7 +119,7 @@ public fun Instant.monthsUntil(other: Instant, zone: TimeZone): Int =
124119
* @throws DateTimeArithmeticException if this [Instant] or [other] is too large to fit in [LocalDateTime].
125120
*/
126121
public fun Instant.yearsUntil(other: Instant, zone: TimeZone): Int =
127-
until(other, CalendarUnit.YEAR, zone).clampToInt()
122+
until(other, DateTimeUnit.YEAR, zone).clampToInt()
128123

129124
// TODO: move to internal utils
130125
internal fun Long.clampToInt(): Int =
@@ -133,15 +128,24 @@ internal fun Long.clampToInt(): Int =
133128
public fun Instant.minus(other: Instant, zone: TimeZone): DateTimePeriod = other.periodUntil(this, zone)
134129

135130

136-
131+
/**
132+
* @throws DateTimeArithmeticException if this value or the result is too large to fit in [LocalDateTime].
133+
*/
137134
public fun Instant.plus(unit: DateTimeUnit, zone: TimeZone): Instant =
138135
plus(unit.calendarScale, unit.calendarUnit, zone)
136+
137+
// TODO: safeMultiply
138+
/**
139+
* @throws DateTimeArithmeticException if this value or the result is too large to fit in [LocalDateTime].
140+
*/
139141
public fun Instant.plus(value: Int, unit: DateTimeUnit, zone: TimeZone): Instant =
140142
plus(value * unit.calendarScale, unit.calendarUnit, zone)
143+
144+
/**
145+
* @throws DateTimeArithmeticException if this value or the result is too large to fit in [LocalDateTime].
146+
*/
141147
public fun Instant.plus(value: Long, unit: DateTimeUnit, zone: TimeZone): Instant =
142148
plus(value * unit.calendarScale, unit.calendarUnit, zone)
143149

144-
public fun Instant.until(other: Instant, unit: DateTimeUnit, zone: TimeZone): Long =
145-
until(other, unit.calendarUnit, zone) / unit.calendarScale
146150

147151
public fun Instant.minus(other: Instant, unit: DateTimeUnit, zone: TimeZone): Long = other.until(this, unit, zone)

core/jsMain/src/Instant.kt

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,6 @@ public actual fun Instant.plus(period: DateTimePeriod, zone: TimeZone): Instant
7777
}.toInstant().let(::Instant)
7878
}
7979

80-
internal actual fun Instant.plus(value: Int, unit: CalendarUnit, zone: TimeZone): Instant =
81-
when (unit) {
82-
CalendarUnit.YEAR -> this.value.atZone(zone.zoneId).plusYears(value).toInstant()
83-
CalendarUnit.MONTH -> this.value.atZone(zone.zoneId).plusMonths(value).toInstant()
84-
CalendarUnit.DAY -> this.value.atZone(zone.zoneId).plusDays(value).let { it as ZonedDateTime }.toInstant()
85-
CalendarUnit.HOUR -> this.value.atZone(zone.zoneId).plusHours(value).toInstant()
86-
CalendarUnit.MINUTE -> this.value.atZone(zone.zoneId).plusMinutes(value).toInstant()
87-
CalendarUnit.SECOND -> this.value.plusSeconds(value)
88-
CalendarUnit.MILLISECOND -> this.value.plusMillis(value)
89-
CalendarUnit.MICROSECOND -> this.value.plusNanos(value * 1000)
90-
CalendarUnit.NANOSECOND -> this.value.plusNanos(value)
91-
}.let(::Instant)
92-
9380
internal actual fun Instant.plus(value: Long, unit: CalendarUnit, zone: TimeZone): Instant =
9481
when (unit) {
9582
CalendarUnit.YEAR -> this.value.atZone(zone.zoneId).plusYears(value).toInstant()
@@ -116,9 +103,8 @@ public actual fun Instant.periodUntil(other: Instant, zone: TimeZone): DateTimeP
116103
return DateTimePeriod((months / 12).toInt(), (months % 12).toInt(), days.toInt(), hours, minutes, seconds.toLong(), nanoseconds.toLong())
117104
}
118105
}
119-
120-
internal actual fun Instant.until(other: Instant, unit: CalendarUnit, zone: TimeZone): Long =
121-
until(other, unit.toChronoUnit(), zone.zoneId)
106+
public actual fun Instant.until(other: Instant, unit: DateTimeUnit, zone: TimeZone): Long =
107+
until(other, unit.calendarUnit.toChronoUnit(), zone.zoneId) / unit.calendarScale
122108

123109
private fun Instant.until(other: Instant, unit: ChronoUnit, zone: ZoneId): Long =
124110
this.value.atZone(zone).until(other.value.atZone(zone), unit).toLong()

core/jvmMain/src/Instant.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,6 @@ public actual fun Instant.plus(period: DateTimePeriod, zone: TimeZone): Instant
7171
}.toInstant().let(::Instant)
7272
}
7373

74-
internal actual fun Instant.plus(value: Int, unit: CalendarUnit, zone: TimeZone): Instant =
75-
plus(value.toLong(), unit, zone)
76-
7774
internal actual fun Instant.plus(value: Long, unit: CalendarUnit, zone: TimeZone): Instant =
7875
when (unit) {
7976
CalendarUnit.YEAR -> this.value.atZone(zone.zoneId).plusYears(value).toInstant()
@@ -101,8 +98,8 @@ public actual fun Instant.periodUntil(other: Instant, zone: TimeZone): DateTimeP
10198
}
10299
}
103100

104-
internal actual fun Instant.until(other: Instant, unit: CalendarUnit, zone: TimeZone): Long =
105-
until(other, unit.toChronoUnit(), zone.zoneId)
101+
public actual fun Instant.until(other: Instant, unit: DateTimeUnit, zone: TimeZone): Long =
102+
until(other, unit.calendarUnit.toChronoUnit(), zone.zoneId) / unit.calendarScale
106103

107104
private fun Instant.until(other: Instant, unit: ChronoUnit, zone: ZoneId): Long =
108105
this.value.atZone(zone).until(other.value.atZone(zone), unit)

core/nativeMain/src/Instant.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -299,9 +299,6 @@ actual fun Instant.plus(period: DateTimePeriod, zone: TimeZone): Instant = try {
299299
throw DateTimeArithmeticException("Boundaries of Instant exceeded when adding CalendarPeriod", e)
300300
}
301301

302-
internal actual fun Instant.plus(value: Int, unit: CalendarUnit, zone: TimeZone): Instant =
303-
plus(value.toLong(), unit, zone)
304-
305302
internal actual fun Instant.plus(value: Long, unit: CalendarUnit, zone: TimeZone): Instant = try {
306303
when (unit) {
307304
CalendarUnit.YEAR -> toZonedLocalDateTimeFailing(zone).plusYears(value).toInstant()
@@ -352,9 +349,10 @@ actual fun Instant.periodUntil(other: Instant, zone: TimeZone): DateTimePeriod {
352349
}
353350
}
354351

355-
internal actual fun Instant.until(other: Instant, unit: CalendarUnit, zone: TimeZone): Long =
352+
public actual fun Instant.until(other: Instant, unit: DateTimeUnit, zone: TimeZone): Long =
356353
try {
357-
toZonedLocalDateTimeFailing(zone).until(other.toZonedLocalDateTimeFailing(zone), unit)
354+
// TODO: inline 'until' here and simplify operation for time-based units
355+
toZonedLocalDateTimeFailing(zone).until(other.toZonedLocalDateTimeFailing(zone), unit.calendarUnit) / unit.calendarScale
358356
} catch (e: ArithmeticException) {
359357
if (this < other) Long.MAX_VALUE else Long.MIN_VALUE
360358
}

core/nativeMain/src/ZonedDateTime.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ internal fun Instant.toZonedLocalDateTime(zone: TimeZone): ZonedDateTime {
7070
* @throws DateTimeArithmeticException if setting [other] to the offset of [this] leads to exceeding boundaries of
7171
* [LocalDateTime].
7272
*/
73+
74+
// TODO: use DateTimeUnit
7375
internal fun ZonedDateTime.until(other: ZonedDateTime, unit: CalendarUnit): Long =
7476
when (unit) {
7577
// if the time unit is date-based, the offsets are disregarded and only the dates and times are compared.

0 commit comments

Comments
 (0)