CKTIS / Java
ZoneRules — valid offsets and clock transitions
ZoneRules describes changes to a zone's offset. Its getValidOffsets and getTransition methods detect nonexistent or ambiguous local times before an offset is selected.
Version: Java SE 21; repository runtime, production runtime not attested
Syntax
List<ZoneOffset> offsets = rules.getValidOffsets(localDateTime);
ZoneOffsetTransition transition = rules.getTransition(localDateTime);ZoneRules describes a zone's offset rules. ZoneRules.getValidOffsets(LocalDateTime) returns an empty list for a gap, normally one element, or two for an overlap. In an overlap the pre-transition offset comes first; more than two offsets are theoretically possible.
ZoneRules.getTransition(LocalDateTime) returns a ZoneOffsetTransition for a gap or overlap, otherwise null. Moving clocks forward creates nonexistent local times; moving them backward creates times occurring twice. isGap() and isOverlap() distinguish these cases. Do not assume a transition lasts one hour.
Define an explicit policy: reject a gap or adjust the time; choose an overlap offset deliberately. getOffset(LocalDateTime) alone does not validate the time: for gaps and overlaps it returns the pre-transition offset.
Examples
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.zone.ZoneOffsetTransition;
import java.time.zone.ZoneRules;
import java.util.List;
public class Main {
public static void main(String[] args) {
ZoneOffset before = ZoneOffset.ofHours(1);
ZoneOffset after = ZoneOffset.ofHours(2);
ZoneOffsetTransition jump = ZoneOffsetTransition.of(
LocalDateTime.of(2024, 3, 31, 2, 0), before, after);
ZoneRules rules = ZoneRules.of(
before, before, List.of(), List.of(jump), List.of());
LocalDateTime local = LocalDateTime.of(2024, 3, 31, 2, 30);
System.out.println(rules.getValidOffsets(local));
ZoneOffsetTransition transition = rules.getTransition(local);
System.out.println(transition != null && transition.isGap());
System.out.println(rules.getValidOffsets(local.plusHours(1)));
System.out.println(rules.getTransition(local.plusHours(1)) == null);
}
}Result
[] true [+02:00] true
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.