Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell parse time duration from string into DiffTime

Tags:

haskell

I'm trying to parse and do operations on timestamps such as this one:

"01:46:22,041"

which stands for: 1 hour, 46 minutes, 22 seconds and 41 milliseconds.

I don't really know where to begin here. The existing datatypes all seem to include a date. In my case, I only need to add/substract from the timestamp and then print it out in the same format.

I obviously don't want to write my own calculator if there's one built in. Is there any standard way to tackle this in Haskell?

like image 256
ddccffvv Avatar asked Dec 31 '14 12:12

ddccffvv


1 Answers

Use Data.Time.Clock.DiffTime. To parse and format use the functions in Data.Time.Format.

Example:

import Data.Time
xyz :: String -> Maybe DiffTime
xyz x = parseTimeM True defaultTimeLocale "%H:%M:%S" x 
like image 98
Tom Ellis Avatar answered Nov 06 '22 09:11

Tom Ellis