
I want to calculate the length of time between time ta and time tb. Time ta is a String looking like "N:NNAM" or "N:NNPM." I need tb to be 9:30AM on the current date. I think the flow should be 1) parse ta to utcTimeA :: UTCTime 2) get current date and form tb to utcTimeB :: UTCTime 3) call "diffUTCTime utcTimeA utcTimeB" So my program looks like as follows in a "do" expression: let utcTimeA = fromJust (parseTime defaultTimeLocale "%R%P" ta :: Maybe UTCTime) currentTime <- getCurrentTime let day = utctDay currentTime let utcTimeB = UTCTime day "09:30:00.0000000 UTC" let timeElapsed = diffUTCTime utcTimeA utcTimeB There are two difficulties I could not get around: 1) It seems that parseTime returns "Nothing". Why? 2) I knew "09:30:00.0000000 UTC" in the statement of "let utcTimeB ..." is incorrect, because it is a String instead of expected DiffTime type. After consulting Data.Time module (it just say "Data DiffTime"), I still cannot figure out how to get utcTimeB. Can someone help me? Thanks, Hong

Am Freitag 02 Oktober 2009 00:13:38 schrieb Hong Yang:
I want to calculate the length of time between time ta and time tb. Time ta is a String looking like "N:NNAM" or "N:NNPM." I need tb to be 9:30AM on the current date.
I think the flow should be 1) parse ta to utcTimeA :: UTCTime 2) get current date and form tb to utcTimeB :: UTCTime 3) call "diffUTCTime utcTimeA utcTimeB"
So my program looks like as follows in a "do" expression: <moved> There are two difficulties I could not get around: 1) It seems that parseTime returns "Nothing". Why?
parseTime expects two digits for the hours: Prelude Data.Time System.Locale> parseTime defaultTimeLocale "%R%P" "09:30AM" :: MaybeUTCTime Just 1970-01-01 09:30:00 UTC
2) I knew "09:30:00.0000000 UTC" in the statement of "let utcTimeB ..." is incorrect, because it is a String instead of expected DiffTime type. After consulting Data.Time module (it just say "Data DiffTime"), I still cannot figure out how to get utcTimeB.
let utcTimeA = fromJust (parseTime defaultTimeLocale "%R%P" ta :: Maybe UTCTime)
Note that that will be the time of day on the first of January 1970, probably not what you want. If you want it to have the current day, change the day like below, otherwise you'd have to give the date in the string to parse, too
currentTime <- getCurrentTime
let Just temp = parseTime defaultTimeLocale "%RP" "09:30AM" utcTimeB = currentTime{ utctDayTime = utctDayTime temp } -- utcTimeB = temp{ utctDay = utctDay currentTime } is another option timeElapsed = diffUTCTime utcTimeA utcTimeB
let day = utctDay currentTime let utcTimeB = UTCTime day "09:30:00.0000000 UTC" let timeElapsed = diffUTCTime utcTimeA utcTimeB
Can someone help me?
Thanks,
Hong

Prelude Data.Time System.Locale> parseTime defaultTimeLocale "%R%P" "09:30AM" :: MaybeUTCTime Just 1970-01-01 09:30:00 UTC
Prelude Data.Time System.Locale> parseTime defaultTimeLocale "%R%P" "09:30AM" :: Maybe UTCTime Nothing I suspect the parser malfunction is an issue with POSIX and Windows. 1970-01-01 00:00 UTC is the POSIX zero time...
participants (3)
-
Daniel Fischer
-
Hong Yang
-
Tim Attwood