[Haskell-cafe] Time zones and IO

Heller Time hellertime at gmail.com
Sun Nov 6 23:25:14 CET 2011


Glad to hear you have found use for my library, always nice to see something you produce find purpose other than your own.

I rationalized the use of unsafePerformIO in the library by deciding that it was only being used in the case of a LocalTime instance (a user could always stick to UTCTime and avoid the issues) and that unless the machine running the program using time-recurrence was traveling across timezones (and the system was updating that fact) the program would essentially be referentially transparent :)

That said, I'm curious to hear what opinions those with more Haskell knowledge than myself have so add to this question.

-Chris

On Nov 6, 2011, at 2:42 PM, Ben Gamari <bgamari.foss at gmail.com> wrote:

> Recently I've been working on an iCalendar parser implementation with
> support for recurrence rules. For the recurrence logic, I've been
> relying on Chris Heller's excellent time-recurrence
> package. Unfortunately, it seems we have both encountered great
> difficulty in cleanly handling time zones. For example, as seen in the
> case of the CalendarTime LocalTime instance[1], Chris does the following,
> 
>     instance CalendarTimeConvertible LocalTime where
>       toCalendarTime (LocalTime day t) = CalendarTime (fromEnum $ todSec t) (todMin t) (todHour t) d (toEnum m) y weekDay yearDay tz
>         where
>           (y, m, d, weekDay, yearDay) = dayInfo day
>           tz = unsafePerformIO getCurrentTimeZone
> 
> Ouch. Unfortunately, I have been entirely unable to come up with any
> better way to deal with timezones in a referentially transparent
> way. Passing the current timezone as an argument seems heavy-handed and
> reveals the leakiness of the CalendarTimeConvertible abstraction. Short
> of making toCalendarTime an action, can anyone see a suitable way of
> refactoring CalendarTimeConvertible to avoid unsafe IO?
> 
> Perhaps a TimeZoneInfo monad is in order, exposing lookup of arbitrary
> (through timezone-olson) as well the current timezone? It seems like
> this would be inconvenient and overkill, however.
> 
> Ideas?
> 
> - Ben
> 
> 
> [1] https://github.com/hellertime/time-recurrence/blob/master/src/Data/Time/CalendarTime/CalendarTime.hs#L92



More information about the Haskell-Cafe mailing list