[Haskell-cafe] Parsing LocalTime from Unix seconds

Joachim Durchholz jo at durchholz.org
Mon Sep 17 21:02:52 UTC 2018


Am 17.09.2018 um 21:29 schrieb Neil Mayhew:
> On 2018-09-17 12:01 AM, Tobias Dammers wrote:
>> … you can convert unix timestamps to some "local time without timezone 
>> info" data structure (e.g. LocalTime); whether you assume UTC or any 
>> timezone becomes relevant when you want to convert that data structure 
>> into a data structure that does contain timezone info explicitly (like 
>> ZonedTime) or implicitly (like UTCTime).
> 
> I don't think this is possible. If you want to measure seconds since 
> 1970-01-01 00:00:00 local time (which may actually be 1969 in UTC) you 
> have to know the timezone at the start (ie in 1970) and at the end (ie 
> after the seconds have elapsed). That requires knowing whether DST 
> applies at either end, and whether the location was rezoned during the 
> intervening time.

Actually you need only know the time offset at start and end of 
interval. Any intervening changes cancel out.

You do need to know whether the location had its time zone changed 
(which happened multiple times in some areas I believe).

> This requires IO and the conversion from seconds is no 
> longer a pure operation.

Eek. IO just to handle timezone is a really bad idea.
Just use a timestamp-with-timezone data type for the parameters and 
you're back with pure functions.

> IHMO, the only sane way to interpret seconds-since-the-epoch is using UTC.

Now that's true. From the point of view in the paragraph above, simply 
because it's lacking the data needed for any other interpretation.

Note that there are excellent designs for time representation arounds. A 
good one would have to cover instants (multiple representations), 
intervals (with and without leap seconds), time zones, and calendars.
 From my daily Java work, the one that works best is the Jodatime 
library, documented at http://www.joda.org/joda-time/. If anybody wishes 
to check whether it has good ideas worth stealing, take a look at the 
Javadoc at http://www.joda.org/joda-time/apidocs/index.html.

If you want to be really thorough, you can also study JSR-310, which was 
supposed to be a slightly improve version of Jodatime, AND part of the 
Java standard library. That work stopped when it was "good enough", 
which isn't good enough. Still, the differences between Jodatime and 
JSR-310 may be interesting in themselves (they may show areas where the 
Jodatime author believed his work could be improved, as he was deeply 
involved in the JSR as well).
Anyway: the JSR-310 classes are documented at 
https://docs.oracle.com/javase/8/docs/api/index.html, in the java.time 
package and subpackages.

Regards,
Jo


More information about the Haskell-Cafe mailing list