[Haskell-cafe] time of day

Yitzchak Gale gale at sefer.org
Mon Dec 28 09:14:59 EST 2009

Brian Denheyer wrote:
>> What's the best way to get the year/month/day/hour/min/sec of the
>> current time ?

import Data.Time
  now <- getCurrentTime
  tz <- getCurrentTimeZone
  let t = utcToLocalTime tz now
      (year, month, day) = toGregorian $ localDay t
      TimeOfDay hour minute sec = localTimeOfDay t

If you need the day of week, then also:

import Data.Time.Calendar.OrdinalDate (sundayStartWeek)
      (_, dow) = sundayStartWeek $ localDay t -- Sunday = 0

If you need to use different time zones other than the
current local one, construct them like this:

est = TimeZone
  {timeZoneMinutes = -300,
   timeZoneSummerOnly = False,
   timeZoneName = "EST"}
edt = TimeZone
  {timeZoneMinutes = -240,
   timeZoneSummerOnly = True,
   timeZoneName = "EDT"}

>> I've become mired in confusion with Time, Data.Time,
>> DateTime and I think there is even an old-time.
>> Might be a couple of calendar libraries in there, not quite sure...

Thomas DuBuisson wrote:
> 'time' is the generally accepted package which exports the
> Data.Time you mentioned.

Yes, use that.

> 'DateTime' looks to use 'time' to provide an aledgedly simpler
> API.

I tried that once. It is supposed to have a slightly gentler learning
curve, but in the end I think Data.Time itself is nicer.

The only reason Data.Time is a little confusing at first is because
the most useful functions are sprinkled into many different
modules in the documentation, and hidden among less useful
ones. Once you know where to look for things, it's easy.


More information about the Haskell-Cafe mailing list