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

Dmitri O.Kondratiev dokondr at gmail.com
Wed Jun 15 14:57:40 CEST 2011


Still have trouble iterating days (
I have:
ds1 = "10/11/2009 7:04:28 PM"
ds2 = "10/17/2009 8:48:29 AM"
t1 = readTime defaultTimeLocale "%m/%d/%Y %l:%M:%S %p" ds1 :: UTCTime
t2 = readTime defaultTimeLocale "%m/%d/%Y %l:%M:%S %p" ds2 :: UTCTime
dif = diffUTCTime t2 t1

I need to:
1) Find how many complete days elapsed between t1 and t2
2) Find UTCTime for a moment 6 days after t1,  in other words time equal to
t1 + 6 * 24 hours.

Questions:
1) Is there any library function that will convert (diff = diffUTCTime t2
t1) to a data structure similar to a tuple (days, hours, mins, secs) where
'days' is a whole number of days in my 'diff'' interval, and similar for
'hours', 'mins' and 'secs' in the tuple above?
2) What is the 'right way' of adding days to UTCTime? Should I add just
equivalent number of seconds or should I convert UTCTime to
Data.Time.Calendar.Day type first and then use 'addDays' function?
3) How to convert UTCTime to Data.Time.Calendar.Day and back to UTCTime?

Thanks!


On Wed, Jun 15, 2011 at 12:55 PM, Dmitri O.Kondratiev <dokondr at gmail.com>wrote:

>
> Hi, Yitz!
> Your example puts scattered around pieces in place, thanks a lot!
>
> On Wed, Jun 15, 2011 at 9:49 AM, Yitzchak Gale <gale at sefer.org> wrote:
>
>> 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
>>
>
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20110615/33864ff4/attachment.htm>


More information about the Haskell-Cafe mailing list