Skip to content

Commit

Permalink
Cleanup some CalendarUnit usages
Browse files Browse the repository at this point in the history
  • Loading branch information
ilya-g committed Jul 16, 2020
1 parent 44ebcf9 commit 904db57
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 38 deletions.
28 changes: 16 additions & 12 deletions core/commonMain/src/Instant.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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].
*/
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -124,23 +119,32 @@ 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()

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)
18 changes: 2 additions & 16 deletions core/jsMain/src/Instant.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down
7 changes: 2 additions & 5 deletions core/jvmMain/src/Instant.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 3 additions & 5 deletions core/nativeMain/src/Instant.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
}
2 changes: 2 additions & 0 deletions core/nativeMain/src/ZonedDateTime.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 904db57

Please sign in to comment.