Showing posts with label Time. Show all posts
Showing posts with label Time. Show all posts

Wednesday, April 13, 2016

LocalDateTime Is Not Local!

No one ever has problems with Dates and Times!  (Y2k, 2038, 10,000)

Java8 dates and times were supposed to be a lot easier and designed by the Joda-Time guys.

One problem, after struggling for a full working day - LocalDateTimes are NOT Local!  Maybe in the sense of localized?  They are definitely UTC, not your local timezone.  Looking back at it, it says that it doesn't capture timezone information in the docs, but I didn't look at the docs because I thought that the name was blindingly obvious.

Copyright BBC
I was pulling my hair out when I wrote a function todayAt that returned a date that didn't register as today.  A time at 3:45 PM was being converted to 2 AM UTC (the next day) because LocalDateTime wasn't local.  I was looking for ZonedDateTime.

As they say, there are two hard things in Computer Science: caching, naming things and off by one errors.

Sunday, November 6, 2011

Fun Times: Happy Daylight Savings (US)

Here in the US we're ending daylight savings time ("spring ahead, fall back") and to honor this, this post will be about all of those tricky things about time that affect programming.

There are already some good posts about this issue, but here are some fun facts:

  • In the US west coast, the two time zones uses (yes, there are two) are Pacific Daylight Time (PDT, during the summer) and Pacific Standard Time (PST, during the winter).  When it changes time zones in the fall 1:59 AM PDT is followed by 1:00 AM PST.  This also causes the day to have 25 hours.
  • The US state of Arizona does not observe daylight savings time.
  • Some time zones are not on even hour increments from GMT (i.e. -8 or +3), but can involve minutes as well (GMT +5:30 for Mumbai and GMT +5:45 for Kathmandu).
  • In Java, Calendars are important (but the standard one is the Gregorian Calendar, as far as I know).
  • Time zones are decided by the nation's government, as when Venezuela changed it's time zone in 2007.
  • Usage of GMT is depreciated in technical contexts, and can refer to UTC or UTC1.
  • Windows XP references GMT, but Windows 7 references UTC.
  • UTC and UTC1 are different, but always by less than a second (when it's more there is a leap second).
  • Leap seconds can cause some minutes to have 59 or 61 seconds in them.
  • java.util.Date is really a time stamp from the Epoch, and I find it useful to subclass a "TimeStamp" object for clarity.
  • XKCD has a hilarious comic related to all of this.
  • Birthdays should not be related to the java.util.Date object.  If someone is born on at 11:30 PM and later goes east to another time zone their birthday does not change.
  • When mankind starts doing serious space travel relativity is going to make all of this much worse.
  • I'm sure that there are more things that escape my memory right now, so do not consider this list complete.
All in all, time is one of those "tricky" things.

There are also some libraries that aim to replace the core java.util.Date classes: Date4j and Joda Time.