Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I extract specific time components out of a UTCTime?

Tags:

haskell

If I have this code:

let time = read "2013-02-03 17:00:07.687" :: UTCTime

How can I extract the minutes and seconds components out of the UTCTime?

like image 970
ryeguy Avatar asked Mar 25 '13 03:03

ryeguy


2 Answers

A UTCTime has two components: a day and a DiffTime. You can get the DiffTime using utctDayTime or by pattern matching. From there, you can convert it to a TimeOfDay using timeToTimeOfDay. You can then just pattern match against the TimeOfDay to get the hours, minutes and seconds.

So you could do this:

let TimeOfDay hours minutes seconds = timeToTimeOfDay (utctDayTime time)

You can also use the todMin and todSec functions to get the minutes and seconds respectively out of the TimeOfDay.

like image 180
Tikhon Jelvis Avatar answered Sep 18 '22 18:09

Tikhon Jelvis


You can use the time-lens package, which makes time and date manipulation much easier. E.g.

> let time = read "2013-02-03 17:00:07.687" :: UTCTime
> getL seconds time
7.687000000000

If you import Data.Lens.Common, there's also an infix version of getL:

> time ^. minutes
0
like image 30
Roman Cheplyaka Avatar answered Sep 21 '22 18:09

Roman Cheplyaka