Skip to content

fix: Handle DST transitions correctly in Instant.periodUntil #538

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions core/common/test/InstantTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,23 @@ class InstantTest {
assertFalse(Instant.MIN.isDistantFuture)
}

@Test
fun periodUntilDuringDstTransition() {
val tz = TimeZone.of("Europe/Berlin")

// Test during spring forward (when clocks move forward)
val springStart = LocalDateTime(2025, 3, 29, 2, 30).toInstant(tz)
val springEnd = LocalDateTime(2025, 3, 30, 3, 10).toInstant(tz)
val springPeriod = springStart.periodUntil(springEnd, tz)
assertTrue(springPeriod.minutes >= 0, "Minutes should be non-negative during spring forward")

// Test during fall back (when clocks move backward)
val fallStart = LocalDateTime(2024, 10, 26, 2, 30).toInstant(tz)
val fallEnd = LocalDateTime(2024, 10, 27, 3, 10).toInstant(tz)
val fallPeriod = fallStart.periodUntil(fallEnd, tz)
assertTrue(fallPeriod.minutes >= 0, "Minutes should be non-negative during fall back")
}

}

class InstantRangeTest {
Expand Down
23 changes: 18 additions & 5 deletions core/jvm/src/Instant.kt
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,24 @@ public actual fun Instant.periodUntil(other: Instant, timeZone: TimeZone): DateT
var thisZdt = this.atZone(timeZone)
val otherZdt = other.atZone(timeZone)

val months = thisZdt.until(otherZdt, ChronoUnit.MONTHS); thisZdt = thisZdt.plusMonths(months)
val days = thisZdt.until(otherZdt, ChronoUnit.DAYS); thisZdt = thisZdt.plusDays(days)
val nanoseconds = thisZdt.until(otherZdt, ChronoUnit.NANOS)

return buildDateTimePeriod(months, days.toInt(), nanoseconds)
// Calculate date-based components first
val months = thisZdt.until(otherZdt, ChronoUnit.MONTHS)
thisZdt = thisZdt.plusMonths(months)
val days = thisZdt.until(otherZdt, ChronoUnit.DAYS)
thisZdt = thisZdt.plusDays(days)

// Calculate time-based components using the actual duration
val remainingDuration = java.time.Duration.between(thisZdt, otherZdt)
val hours = remainingDuration.toHours()
val minutes = remainingDuration.toMinutesPart()
val seconds = remainingDuration.toSecondsPart()
val nanos = remainingDuration.toNanosPart()

return buildDateTimePeriod(
months,
days.toInt(),
hours * 3600_000_000_000L + minutes * 60_000_000_000L + seconds * 1_000_000_000L + nanos
)
}

public actual fun Instant.until(other: Instant, unit: DateTimeUnit, timeZone: TimeZone): Long = try {
Expand Down