[Haskell-cafe] GHC 7.0.3 / Win32: Data.Time library?

Yitzchak Gale gale at sefer.org
Wed Jun 15 07:49:44 CEST 2011


Dmitri O.Kondratiev wrote:
> It would also help to see a simple example of parsing "10/11/2009 7:04:28
> PM" to  time and date objects.

Let's assume that 10/11/2009 means October 11, as in the U.S.
Then you can use:

import System.Locale (defaultTimeLocale)
import Data.Time

thatMoment :: Maybe UTCTime
thatMoment = parseTime defaultTimeLocale "%m/%d/%Y %l:%M:%S %p"
"10/11/2009 7:04:28 PM"

Then use diffUTCTime to subtract two UTCTime and
get the amount of time between them. The resulting object
can then be used as if it is a regular floating point number
in units of seconds, or you can use the functions in Data.Time
that treat it specially as an amount of time.

There are many options for the format string and locale that will
affect how the date is parsed - the order of month and day,
leading zeros or leading spaces, upper case or lower case AM or PM
(or 24-hour clock), etc. You can also get different behavior on
parsing failure by using readTime or readsTime instead of parseTime.

For details, see:

http://www.haskell.org/ghc/docs/7.0.3/html/libraries/time-1.2.0.3/Data-Time-Format.html
http://www.haskell.org/ghc/docs/7.0.3/html/libraries/old-locale-1.0.0.2/System-Locale.html#t:TimeLocale
http://www.haskell.org/ghc/docs/7.0.3/html/libraries/old-locale-1.0.0.2/src/System-Locale.html#TimeLocale

As an example of modifying the locale, let's say you want to use "a" and "p"
instead of "AM" and "PM", as is customary in some parts of the world.
Then you can say:

myLocale = defaultTimeLocale {amPm = ("a","p")}

and then use myLocale instead of defaultTimeLocale.

Hope this helps,
Yitz



More information about the Haskell-Cafe mailing list