kizitonwose/Time

Operator extension functions do not support time intervals longer than Int.MAX_VALUE

Closed this issue · 1 comments

Reproduction steps

  1. Create a time interval for years:
class Year : TimeUnit {
    override val timeIntervalRatio = 31_556_952.0
}

val Number.years: Interval<Year>
	get() = Interval(this)
  1. Create a Calendar instance and add 1 year:
val cal = Calendar.getInstance() + 1.years

Expected behavior

The calendar instance adds 1 year to the current time.

Actual behavior

1 year is not added to the calendar instance.


If you change the following extensions functions to use timeInMillis instead of converting the long to an int and using Calendar#add it would fix the intended behavior for time intervals over Int.MAX_VALUE:

operator fun Calendar.plus(other: Interval<TimeUnit>): Calendar = (clone() as Calendar).apply {
    timeInMillis += other.inMilliseconds.longValue
}

operator fun Calendar.minus(other: Interval<TimeUnit>): Calendar = (clone() as Calendar).apply {
    timeInMillis -= other.inMilliseconds.longValue
}

Thank you for bringing this up, it's now included in 1.0.4