On Thu, Dec 8, 2011 at 9:01 AM, dokondr <doko...@gmail.com> wrote: > Now, when I have managed to convert UTCTime to seconds (see code below) I > got stuck trying to convert from UTCTime to CalendarTime, how to do this? > >
It might be easier to use 'diffUTCTime' and 'addUTCTime' instead of converting to and from POSIX seconds. What do you need the 'CalendarTime' for? I recommend not mixing the 'time' and 'old-time' packages if you can avoid it. If you really need to for inter-operating with some other library, it looks like you can use the 'datetime' package to convert from a UTCTime to a ClockTime, and then you can use the 'old-time' package to convert from a 'ClockTime' to a 'CalendarTime'. Antoine > import Data.Time.Format > import Data.Time.Clock > import Locale > import Data.Maybe > import Data.Time.Clock.POSIX > > > s1 = "Wed, 07 Dec 2011 10:09:21 +0000" > s2 = "Wed, 07 Dec 2011 10:11:00 +0000" > t1 = fromJust $ tryParseTime s1 > t2 = fromJust $ tryParseTime s2 > pt1 = utcTimeToPOSIXSeconds t1 -- :: UTCTime -> POSIXTime > pt2 = utcTimeToPOSIXSeconds t2 > pt3 = pt1 + (pt2 - pt1) / 2 > t3 = posixSecondsToUTCTime pt3 > > > t = compare t1 t2 > > tryParseTime :: String -> Maybe UTCTime > tryParseTime timeStr = tryFormat (parseTime defaultTimeLocale timeFormat1 > timeStr :: Maybe UTCTime) > where > tryFormat time > | time == Nothing = parseTime defaultTimeLocale timeFormat2 timeStr > :: Maybe UTCTime > | otherwise = time > > timeFormat1 = "%a, %d %b %Y %T %z" > timeFormat2 = "%m/%e/%Y %l:%M:%S %p" > -- timeFormat1 = "%m/%d/%Y %l:%M:%S %p" > > > > On Thu, Dec 8, 2011 at 6:30 PM, dokondr <doko...@gmail.com> wrote: >> >> I need to parse time strings like "Wed, 07 Dec 2011 10:09:21 +0000" to a >> type that: >> 1) implements Eq, Ord >> 2) is numerical, so I could subtract one value from another to find the >> difference or interval length >> >> To answer 1) requirement I wrote the following snippet. Yet I could not >> subtract UTCTime values. How can I convert them to milliseconds? >> >> import Data.Time.Format >> import Data.Time.Clock >> import Locale >> import Data.Maybe >> >> s1 = "Wed, 07 Dec 2011 10:09:21 +0000" >> s2 = "Wed, 07 Dec 2011 10:11:00 +0000" >> t1 = fromJust $ tryParseTime s1 >> t2 = fromJust $ tryParseTime s2 >> >> t = compare t1 t2 >> >> tryParseTime :: String -> Maybe UTCTime >> tryParseTime timeStr = tryFormat (parseTime defaultTimeLocale timeFormat1 >> timeStr :: Maybe UTCTime) >> where >> tryFormat time >> | time == Nothing = parseTime defaultTimeLocale timeFormat2 >> timeStr :: Maybe UTCTime >> | otherwise = time >> >> timeFormat1 = "%a, %d %b %Y %T %z" >> timeFormat2 = "%m/%e/%Y %l:%M:%S %p" >> >> >> >> >> On Thu, Dec 8, 2011 at 6:12 PM, Erik Hesselink <hessel...@gmail.com> >> wrote: >>> >>> I'm not sure if you really need ClockTime (from old-time), but if you >>> don't, the types from the 'time' package are all parseable with >>> `parseTime` [1]. >>> >>> Erik >>> >>> [1] >>> http://hackage.haskell.org/packages/archive/time/latest/doc/html/Data-Time-Format.html#v:parseTime >>> >>> On Thu, Dec 8, 2011 at 14:16, dokondr <doko...@gmail.com> wrote: >>> > Hi, >>> > What would be the simplest way to convert strings like "Wed, 07 Dec >>> > 2011 >>> > 10:09:21 +0000" to System.Time.ClockTime ? >>> > >>> > Thanks! >>> > >>> > >>> > >>> > _______________________________________________ >>> > Haskell-Cafe mailing list >>> > Haskell-Cafe@haskell.org >>> > http://www.haskell.org/mailman/listinfo/haskell-cafe >>> > >> >> >> >> >> > > > > _______________________________________________ > Haskell-Cafe mailing list > Haskell-Cafe@haskell.org > http://www.haskell.org/mailman/listinfo/haskell-cafe > _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe