Timezones on Kotlin/JS

Named zones like Europe/Berlin work in the browser without any setup and without bundled timezone data.

Nothing to set up

val berlin = MpTimezone.of("Europe/Berlin")

MpInstant.now().atZone(berlin)                      // works in the browser as is
MpLocalDate.of(2022, 3, 27).atStartOfDay(berlin)

On JS, ultra:datetime reads zone offsets from the browser's own IANA database through Intl.DateTimeFormat, so it is as current as the browser and adds no data to your bundle. The zone math on top of it is shared with the JVM and Native and follows java.time:

  • A local time in a DST gap moves forward by the length of the gap (02:30 on a spring-forward night becomes 03:30).
  • A local time in a DST overlap takes the earlier offset.
  • Adding days or months keeps the time of day, and keeps the current offset if the result falls into an overlap.
  • The start of a day whose midnight falls into a gap is the end of the gap.
A test table generated from java.time runs on all three targets, and on the JVM the same algorithm is checked against java.time around every transition of every zone from 1900 to 2040.

Zone ids are case-sensitive

The browser's Intl accepts europe/berlin, java.time does not. So that an id accepted in the browser is also accepted on the server, ultra:datetime rejects case variants of known ids on JS too, with an IllegalTimeZoneException.

Raw kotlinx zones

Only code that calls kotlinx-datetime directly, e.g. TimeZone.of("Europe/Berlin"), needs timezone data in the browser. Without it kotlinx knows only UTC and fixed offsets. The kraft:addons:datetime module bundles a js-joda dataset for that case — call only the loader you need, so dead-code elimination keeps just that dataset:

dependencies {
    implementation("io.peekandpoke.kraft:addons-datetime:0.117.0")
}
Loader Kraft builder Coverage Size (min / gzip)
installFullTimezones() fullTimezones() all zones, full history ~713 / 37 KB
installTimezones1970to2030() timezones1970to2030() years 1970–2030 ~130 / 18 KB
installTimezones10YearRange() timezones10YearRange() ~±5 years from build ~40 / 10 KB
import io.peekandpoke.kraft.addons.datetime.timezones1970to2030

val app = kraftApp {
    timezones1970to2030()   // only needed for raw kotlinx TimeZone.of(...)
}

Outside Kraft, call the matching install…() function once at startup, before the first zone is resolved.