Skip to content

Commit 09f4faf

Browse files
committed
Fixes
1 parent a550370 commit 09f4faf

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

core/commonMain/src/DateTimePeriod.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,6 @@ sealed class DateTimePeriod {
6767
return result
6868
}
6969

70-
internal operator fun unaryMinus(): DateTimePeriod =
71-
DateTimePeriod(-years, -months, -days, -hours, -minutes, -seconds, -nanoseconds)
72-
7370
// TODO: parsing from iso string
7471
}
7572

core/commonMain/src/Instant.kt

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,23 @@ public expect fun Instant.plus(period: DateTimePeriod, timeZone: TimeZone): Inst
180180
* @throws DateTimeArithmeticException if this value or the results of intermediate computations are too large to fit in
181181
* [LocalDateTime].
182182
*/
183-
public fun Instant.minus(period: DateTimePeriod, timeZone: TimeZone): Instant = plus(-period, timeZone)
183+
public fun Instant.minus(period: DateTimePeriod, timeZone: TimeZone): Instant =
184+
/* An overflow can happen for any component, but we are only worried about nanoseconds, as having an overflow in
185+
any other component means that `plus` will throw due to the minimum value of the numeric type overflowing the
186+
platform-specific limits. */
187+
if (period.nanoseconds != Long.MIN_VALUE) {
188+
val negatedPeriod = with(period) {
189+
DateTimePeriod(-years, -months, -days, -hours, -minutes, -seconds, -nanoseconds)
190+
}
191+
plus(negatedPeriod, timeZone)
192+
} else {
193+
val negatedPeriod = with(period) {
194+
DateTimePeriod(-years, -months, -days, -hours, -minutes, -seconds, -(nanoseconds+1))
195+
}
196+
plus(negatedPeriod, timeZone).plus(DateTimeUnit.NANOSECOND)
197+
}
184198

185-
/**
199+
/**
186200
* Returns a [DateTimePeriod] representing the difference between `this` and [other] instants.
187201
*
188202
* The components of [DateTimePeriod] are calculated so that adding it to `this` instant results in the [other] instant.
@@ -386,7 +400,12 @@ public expect fun Instant.plus(value: Long, unit: DateTimeUnit, timeZone: TimeZo
386400
*
387401
* @throws DateTimeArithmeticException if this value or the result is too large to fit in [LocalDateTime].
388402
*/
389-
public fun Instant.minus(value: Long, unit: DateTimeUnit, timeZone: TimeZone) = plus(-value, unit, timeZone)
403+
public fun Instant.minus(value: Long, unit: DateTimeUnit, timeZone: TimeZone) =
404+
if (value != Long.MIN_VALUE) {
405+
plus(-value, unit, timeZone)
406+
} else {
407+
plus(-(value+1), unit, timeZone).plus(unit, timeZone)
408+
}
390409

391410
/**
392411
* Returns an instant that is the result of adding the [value] number of the specified [unit] to this instant.

core/commonMain/src/LocalDate.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,14 @@ expect operator fun LocalDate.plus(period: DatePeriod): LocalDate
118118
* @throws DateTimeArithmeticException if this value or the results of intermediate computations are too large to fit in
119119
* [LocalDate].
120120
*/
121-
public operator fun LocalDate.minus(period: DatePeriod): LocalDate = plus(-period as DatePeriod)
121+
public operator fun LocalDate.minus(period: DatePeriod): LocalDate =
122+
if (period.days != Int.MIN_VALUE && period.months != Int.MIN_VALUE) {
123+
plus(with(period) { DatePeriod(-years, -months, -days) })
124+
} else {
125+
minus(period.years, DateTimeUnit.YEAR).
126+
minus(period.months, DateTimeUnit.MONTH).
127+
minus(period.days, DateTimeUnit.DAY)
128+
}
122129

123130
/**
124131
* Returns a [DatePeriod] representing the difference between `this` and [other] dates.

0 commit comments

Comments
 (0)