[Haskell-cafe] Time zones and IO

Ben Gamari bgamari.foss at gmail.com
Sun Nov 6 20:42:56 CET 2011

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
           (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.


- 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