Converting string to System.Time.ClockTime

Hi, What would be the simplest way to convert strings like "Wed, 07 Dec 2011 10:09:21 +0000" to System.Time.ClockTime ? Thanks!

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-F...
On Thu, Dec 8, 2011 at 14:16, dokondr
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

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
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-F...
On Thu, Dec 8, 2011 at 14:16, dokondr
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

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?
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
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
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-F...
On Thu, Dec 8, 2011 at 14:16, dokondr
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

On Thu, Dec 8, 2011 at 9:01 AM, dokondr
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
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
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-F...
On Thu, Dec 8, 2011 at 14:16, dokondr
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

On Thu, Dec 8, 2011 at 9:13 AM, Antoine Latter
On Thu, Dec 8, 2011 at 9:01 AM, dokondr
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.
For those reading along at home, 'addUTCTime' and 'diffUTCTime' are implemented in terms of 'posixSecondsToUTCTime' and 'utcTimeToPOSIXSeconds'. So it's pretty similar in the end. Antoine

Ok, maybe you could advise what packages to use for this simple scenario:
I have two text strings with dates:
s1 = "Wed, 07 Dec 2011 10:09:21 +0000"
s2 = "Wed, 07 Dec 2011 10:11:00 +0000"
I need:
1) Find how many seconds are between these dates
2) Calculate the date in the middle between these dates
3) Print out all three dates in the different format, like these:
2011, 7 Dec, Wed, 10:11:00
What functions should I use to implement this?
On Thu, Dec 8, 2011 at 7:13 PM, Antoine Latter
On Thu, Dec 8, 2011 at 9:01 AM, dokondr
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
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
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-F...
On Thu, Dec 8, 2011 at 14:16, dokondr
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!

On Thu, Dec 8, 2011 at 9:30 AM, dokondr
Ok, maybe you could advise what packages to use for this simple scenario:
I have two text strings with dates:
s1 = "Wed, 07 Dec 2011 10:09:21 +0000" s2 = "Wed, 07 Dec 2011 10:11:00 +0000"
I need: 1) Find how many seconds are between these dates 2) Calculate the date in the middle between these dates
It looks like you already have 1) and 2) finished, using the 'time' package.
3) Print out all three dates in the different format, like these: 2011, 7 Dec, Wed, 10:11:00
If you need to convert into a specific time-zone you can use the 'utcToLocalTime' function in the 'time' package, which takes a UTCTime and a TimeZone to create a 'LocalTime'. I'm just guessing that you might want this, as your output format doesn't include time-zone information. Then for formatting, the 'Data.Time.Format' module in the 'time' package has the function 'formatTime', which uses the same sort of format string used by 'parseTime'. I hope that helps! It took me a while to find my way around the 'time' package properly. Antoine

On Thu, Dec 8, 2011 at 7:39 PM, Antoine Latter
On Thu, Dec 8, 2011 at 9:30 AM, dokondr
wrote: Ok, maybe you could advise what packages to use for this simple scenario:
I have two text strings with dates:
s1 = "Wed, 07 Dec 2011 10:09:21 +0000" s2 = "Wed, 07 Dec 2011 10:11:00 +0000"
I need: 1) Find how many seconds are between these dates 2) Calculate the date in the middle between these dates
It looks like you already have 1) and 2) finished, using the 'time' package.
3) Print out all three dates in the different format, like these: 2011, 7 Dec, Wed, 10:11:00
If you need to convert into a specific time-zone you can use the 'utcToLocalTime' function in the 'time' package, which takes a UTCTime and a TimeZone to create a 'LocalTime'. I'm just guessing that you might want this, as your output format doesn't include time-zone information.
Then for formatting, the 'Data.Time.Format' module in the 'time' package has the function 'formatTime', which uses the same sort of format string used by 'parseTime'.
I hope that helps! It took me a while to find my way around the 'time' package properly.
Antoine
Thanks so much for your help! I think I finally :) solved this problem: import Data.Time.Format import Data.Time.Clock import Locale import Data.Maybe import Data.Time.Clock.POSIX timeFormat1 = "%a, %d %b %Y %T %z" timeFormat2 = "%m/%e/%Y %l:%M:%S %p" s1 = "Wed, 07 Dec 2011 10:09:21 +0000" s2 = "Wed, 07 Dec 2011 10:11:00 +0000" t1 = fromJust $ tryParseTime s1 -- :: UTCTime t2 = fromJust $ tryParseTime s2 pt1 = utcTimeToPOSIXSeconds t1 -- :: POSIXTime pt2 = utcTimeToPOSIXSeconds t2 pt3 = pt1 + (pt2 - pt1) / 2 t3 = posixSecondsToUTCTime pt3 -- :: UTCTime -- formatTime :: FormatTime t => TimeLocale -> String -> t -> String s3 = formatTime defaultTimeLocale timeFormat2 t3 test = (compare t1 t2) == (compare pt1 pt2) 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
participants (3)
-
Antoine Latter
-
dokondr
-
Erik Hesselink