Uncategorized

Independence Day Time Warp

Let’s see how familiar you are with Java 8’s new Date/Time classes.


ZonedDateTime americasBirthday =
  ZonedDateTime.of(1776, 7, 4, 9, 1, 2, 3, ZoneId.of(“America/New_York”));

(I actually have no idea exactly at what time the Declaration of Independence was read that day, but that’s thoroughly beside the point).

So question: what does this print?

System.out.println(
  americasBirthday.withZoneSameInstant(ZoneId.of(“UTC”)));

If you said “1776-07-04T13:57:04.000000003Z[UTC]“, you were right!

The withZoneSameInstant method applies the source date’s time zone’s offset and the target timezone’s offsets to produce an equivalent date in the target time zone. But a time zone’s offset is itself a function of the date. For example, today in summer, the offset of Eastern Standard time (identified as “America/New_York”) is -4. In Winter, it’s -5. And in 1776, the offset from UTC was…

System.out.println(americasBirthday.getOffset());

-04:56:02

Ok – so you’d expect it to be -5, since Daylight Savings Time had not yet been institutionalized. But how do you lose 3 minutes and 58 seconds? Java 8’s timezone definitions include sets of rules that compensate for “discontinuities” in local time. These discontinuities are quite common – each time you switch to daylight savings time you experience one.

And Java 8 will be happy to generate a complete list of these discontinuities for you:

americasBirthday.getZone().getRules().getTransitions()

And if you get the toString() of the first element in that list, you get your answer:

Transition[Overlap at 1883-11-18T12:03:58-04:56:02 to -05:00]

Java 8 records the offset jumping from -4:56:02 to -5:00:00 on November 18, 1883. Why that date? I’m assuming it’s because time zones were established on that date. I guess whatever the “clock of record” was in the newly-defined eastern time zone, it had to be set back almost four minutes to sync up with the rest of the U.S.

The next discontinuity comes three decades later:

Transition[Gap at 1918-03-31T02:00-05:00 to -04:00]

The very first changeover to Daylight Savings Time.

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s