RFC: Time Library 0.2

Ashley Yakeley ashley at semantic.org
Wed Nov 2 02:07:44 EST 2005

In article <slrndmetn6.i4l.jgoerzen at erwin.complete.org>,
 John Goerzen <jgoerzen at complete.org> wrote:

> There are just a few functions that a lot of people need, and they are:

These can all be done easily if you use UTCTime for epoch time.

> (C functions in parens)
>  * Convert epoch time to a printable string (ctime)

  ctime :: UTCTime -> String
  ctime t = show (utcToLocalTime utc t)

TODO: add a "Show UTCTime" instance, allowing

  ctime = show

>  * Convert epoch time to something like struct tm (localtime/gmtime)

  gmtime :: UTCTime -> ZonedTime
  gmtime = zonedTimeFromUTC utc

TODO: rename zonedTimeFromUTC to utcToZonedTime.

Depending on exactly what you want it to do, localtime is one of these:

  -- use time zone at given time
  localtime :: UTCTime -> IO ZonedTime
  localtime = utcToLocalZonedTime

  -- use time zone at current time
  localtime' :: UTCTime -> IO ZonedTime
  localtime' t = do
    zone <- getCurrentTimeZone
    zonedTimeFromUTC zone t

The current time zone is different at different times according to the 
summertime rule. It's possible to find out what it is for a given time.

>  * Convert a struct tm to epoch time (mktime)

  mktime :: ZonedTime -> UTCTime
  mktime = ztUTC

TODO: rename ztUTC to something more sensible.

>  * Convert a struct tm to a printable string (asctime)

  asctime = ZonedTime -> String
  asctime = show

>  * Get the current time in epoch seconds (time)

  time :: IO UTCTime
  time = getCurrentTime

> For many programmers, myself included, the Unix epoch is the most
> common, pervasive, and useful way to work with time.  Calculations are
> simple (no need for various TimeDiff sort of things -- just add on x
> number of seconds), and it is used *everywhere* in the OS, databases,
> etc.  Something that is based on that, such as the C API, is a plus,

Simon Marlow seems to be generally of the opinion that POSIX time is 
evil and should be expunged (because it's a broken representation of 
UTC). I do however have a few POSIX functions exposed.

My UTCTime is more or less the simplest correct representation 
(consisting of a day count and a time offset). I think UTCTime should 
remain the main representation of UTC time, but perhaps there should be 
another module (Data.Time.POSIX) for people who need to muck about with 
seconds since epoch. It would have POSIXTime with arithmetic, conversion 
to UTCTime, and getting the current time.


Ashley Yakeley, Seattle WA

More information about the Libraries mailing list