From 904db578369cdec7ec6ed664ebd274c82a146e4a Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Thu, 16 Jul 2020 05:34:28 +0300 Subject: [PATCH] Cleanup some CalendarUnit usages --- core/commonMain/src/Instant.kt | 28 ++++++++++++++++------------ core/jsMain/src/Instant.kt | 18 ++---------------- core/jvmMain/src/Instant.kt | 7 ++----- core/nativeMain/src/Instant.kt | 8 +++----- core/nativeMain/src/ZonedDateTime.kt | 2 ++ 5 files changed, 25 insertions(+), 38 deletions(-) diff --git a/core/commonMain/src/Instant.kt b/core/commonMain/src/Instant.kt index 1a7542989..9b0292148 100644 --- a/core/commonMain/src/Instant.kt +++ b/core/commonMain/src/Instant.kt @@ -76,11 +76,6 @@ public fun String.toInstant(): Instant = Instant.parse(this) */ public expect fun Instant.plus(period: DateTimePeriod, zone: TimeZone): Instant -/** - * @throws DateTimeArithmeticException if this value or the result is too large to fit in [LocalDateTime]. - */ -internal expect fun Instant.plus(value: Int, unit: CalendarUnit, zone: TimeZone): Instant - /** * @throws DateTimeArithmeticException if this value or the result is too large to fit in [LocalDateTime]. */ @@ -97,7 +92,7 @@ public expect fun Instant.periodUntil(other: Instant, zone: TimeZone): DateTimeP * * @throws DateTimeArithmeticException if this [Instant] or [other] is too large to fit in [LocalDateTime]. */ -internal expect fun Instant.until(other: Instant, unit: CalendarUnit, zone: TimeZone): Long +public expect fun Instant.until(other: Instant, unit: DateTimeUnit, zone: TimeZone): Long /** * 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 * @throws DateTimeArithmeticException if this [Instant] or [other] is too large to fit in [LocalDateTime]. */ public fun Instant.daysUntil(other: Instant, zone: TimeZone): Int = - until(other, CalendarUnit.DAY, zone).clampToInt() + until(other, DateTimeUnit.DAY, zone).clampToInt() /** * 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 = * @throws DateTimeArithmeticException if this [Instant] or [other] is too large to fit in [LocalDateTime]. */ public fun Instant.monthsUntil(other: Instant, zone: TimeZone): Int = - until(other, CalendarUnit.MONTH, zone).clampToInt() + until(other, DateTimeUnit.MONTH, zone).clampToInt() /** * 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 = * @throws DateTimeArithmeticException if this [Instant] or [other] is too large to fit in [LocalDateTime]. */ public fun Instant.yearsUntil(other: Instant, zone: TimeZone): Int = - until(other, CalendarUnit.YEAR, zone).clampToInt() + until(other, DateTimeUnit.YEAR, zone).clampToInt() private fun Long.clampToInt(): Int = if (this > Int.MAX_VALUE) Int.MAX_VALUE else if (this < Int.MIN_VALUE) Int.MIN_VALUE else toInt() @@ -132,15 +127,24 @@ private fun Long.clampToInt(): Int = public fun Instant.minus(other: Instant, zone: TimeZone): DateTimePeriod = other.periodUntil(this, zone) - +/** + * @throws DateTimeArithmeticException if this value or the result is too large to fit in [LocalDateTime]. + */ public fun Instant.plus(unit: DateTimeUnit, zone: TimeZone): Instant = plus(unit.calendarScale, unit.calendarUnit, zone) + +// TODO: safeMultiply +/** + * @throws DateTimeArithmeticException if this value or the result is too large to fit in [LocalDateTime]. + */ public fun Instant.plus(value: Int, unit: DateTimeUnit, zone: TimeZone): Instant = plus(value * unit.calendarScale, unit.calendarUnit, zone) + +/** + * @throws DateTimeArithmeticException if this value or the result is too large to fit in [LocalDateTime]. + */ public fun Instant.plus(value: Long, unit: DateTimeUnit, zone: TimeZone): Instant = plus(value * unit.calendarScale, unit.calendarUnit, zone) -public fun Instant.until(other: Instant, unit: DateTimeUnit, zone: TimeZone): Long = - until(other, unit.calendarUnit, zone) / unit.calendarScale public fun Instant.minus(other: Instant, unit: DateTimeUnit, zone: TimeZone): Long = other.until(this, unit, zone) diff --git a/core/jsMain/src/Instant.kt b/core/jsMain/src/Instant.kt index 2682d02b4..38c1d9c64 100644 --- a/core/jsMain/src/Instant.kt +++ b/core/jsMain/src/Instant.kt @@ -77,19 +77,6 @@ public actual fun Instant.plus(period: DateTimePeriod, zone: TimeZone): Instant }.toInstant().let(::Instant) } -internal actual fun Instant.plus(value: Int, unit: CalendarUnit, zone: TimeZone): Instant = - when (unit) { - CalendarUnit.YEAR -> this.value.atZone(zone.zoneId).plusYears(value).toInstant() - CalendarUnit.MONTH -> this.value.atZone(zone.zoneId).plusMonths(value).toInstant() - CalendarUnit.DAY -> this.value.atZone(zone.zoneId).plusDays(value).let { it as ZonedDateTime }.toInstant() - CalendarUnit.HOUR -> this.value.atZone(zone.zoneId).plusHours(value).toInstant() - CalendarUnit.MINUTE -> this.value.atZone(zone.zoneId).plusMinutes(value).toInstant() - CalendarUnit.SECOND -> this.value.plusSeconds(value) - CalendarUnit.MILLISECOND -> this.value.plusMillis(value) - CalendarUnit.MICROSECOND -> this.value.plusNanos(value * 1000) - CalendarUnit.NANOSECOND -> this.value.plusNanos(value) - }.let(::Instant) - internal actual fun Instant.plus(value: Long, unit: CalendarUnit, zone: TimeZone): Instant = when (unit) { CalendarUnit.YEAR -> this.value.atZone(zone.zoneId).plusYears(value).toInstant() @@ -116,9 +103,8 @@ public actual fun Instant.periodUntil(other: Instant, zone: TimeZone): DateTimeP return DateTimePeriod((months / 12).toInt(), (months % 12).toInt(), days.toInt(), hours, minutes, seconds.toLong(), nanoseconds.toLong()) } } - -internal actual fun Instant.until(other: Instant, unit: CalendarUnit, zone: TimeZone): Long = - until(other, unit.toChronoUnit(), zone.zoneId) +public actual fun Instant.until(other: Instant, unit: DateTimeUnit, zone: TimeZone): Long = + until(other, unit.calendarUnit.toChronoUnit(), zone.zoneId) / unit.calendarScale private fun Instant.until(other: Instant, unit: ChronoUnit, zone: ZoneId): Long = this.value.atZone(zone).until(other.value.atZone(zone), unit).toLong() diff --git a/core/jvmMain/src/Instant.kt b/core/jvmMain/src/Instant.kt index 517d8606f..a60ef214f 100644 --- a/core/jvmMain/src/Instant.kt +++ b/core/jvmMain/src/Instant.kt @@ -71,9 +71,6 @@ public actual fun Instant.plus(period: DateTimePeriod, zone: TimeZone): Instant }.toInstant().let(::Instant) } -internal actual fun Instant.plus(value: Int, unit: CalendarUnit, zone: TimeZone): Instant = - plus(value.toLong(), unit, zone) - internal actual fun Instant.plus(value: Long, unit: CalendarUnit, zone: TimeZone): Instant = when (unit) { CalendarUnit.YEAR -> this.value.atZone(zone.zoneId).plusYears(value).toInstant() @@ -101,8 +98,8 @@ public actual fun Instant.periodUntil(other: Instant, zone: TimeZone): DateTimeP } } -internal actual fun Instant.until(other: Instant, unit: CalendarUnit, zone: TimeZone): Long = - until(other, unit.toChronoUnit(), zone.zoneId) +public actual fun Instant.until(other: Instant, unit: DateTimeUnit, zone: TimeZone): Long = + until(other, unit.calendarUnit.toChronoUnit(), zone.zoneId) / unit.calendarScale private fun Instant.until(other: Instant, unit: ChronoUnit, zone: ZoneId): Long = this.value.atZone(zone).until(other.value.atZone(zone), unit) diff --git a/core/nativeMain/src/Instant.kt b/core/nativeMain/src/Instant.kt index 556d7f2c5..93d44532f 100644 --- a/core/nativeMain/src/Instant.kt +++ b/core/nativeMain/src/Instant.kt @@ -299,9 +299,6 @@ actual fun Instant.plus(period: DateTimePeriod, zone: TimeZone): Instant = try { throw DateTimeArithmeticException("Boundaries of Instant exceeded when adding CalendarPeriod", e) } -internal actual fun Instant.plus(value: Int, unit: CalendarUnit, zone: TimeZone): Instant = - plus(value.toLong(), unit, zone) - internal actual fun Instant.plus(value: Long, unit: CalendarUnit, zone: TimeZone): Instant = try { when (unit) { CalendarUnit.YEAR -> toZonedLocalDateTimeFailing(zone).plusYears(value).toInstant() @@ -352,9 +349,10 @@ actual fun Instant.periodUntil(other: Instant, zone: TimeZone): DateTimePeriod { } } -internal actual fun Instant.until(other: Instant, unit: CalendarUnit, zone: TimeZone): Long = +public actual fun Instant.until(other: Instant, unit: DateTimeUnit, zone: TimeZone): Long = try { - toZonedLocalDateTimeFailing(zone).until(other.toZonedLocalDateTimeFailing(zone), unit) + // TODO: inline 'until' here and simplify operation for time-based units + toZonedLocalDateTimeFailing(zone).until(other.toZonedLocalDateTimeFailing(zone), unit.calendarUnit) / unit.calendarScale } catch (e: ArithmeticException) { if (this < other) Long.MAX_VALUE else Long.MIN_VALUE } diff --git a/core/nativeMain/src/ZonedDateTime.kt b/core/nativeMain/src/ZonedDateTime.kt index 16c0678da..17c76ac1b 100644 --- a/core/nativeMain/src/ZonedDateTime.kt +++ b/core/nativeMain/src/ZonedDateTime.kt @@ -70,6 +70,8 @@ internal fun Instant.toZonedLocalDateTime(zone: TimeZone): ZonedDateTime { * @throws DateTimeArithmeticException if setting [other] to the offset of [this] leads to exceeding boundaries of * [LocalDateTime]. */ + +// TODO: use DateTimeUnit internal fun ZonedDateTime.until(other: ZonedDateTime, unit: CalendarUnit): Long = when (unit) { // if the time unit is date-based, the offsets are disregarded and only the dates and times are compared.