[Haskell-beginners] Help with Data.Time
Daniel Fischer
daniel.is.fischer at web.de
Thu Oct 1 21:33:34 EDT 2009
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
More information about the Beginners
mailing list