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 <hesselink@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 <dokondr@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
>