summaryrefslogtreecommitdiff
path: root/HMSTime.hs
blob: cbe9e0914d752ac9b6ff62b416a39d646bfe75da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
module HMSTime
( HMSTime(HMSTime, hours, minutes, seconds)
, secondsToHMS
, hmsTimeStringToHMSTime
, hmsTimeStringToSeconds
) where

import Text.Printf( printf )
import Data.List.Split( splitOn )

data HMSTime = HMSTime { hours :: Integer, minutes :: Integer, seconds :: Integer }

instance Show HMSTime where
  show (HMSTime h m s) = printf "%d:%02d:%02d" h m s

secondsToHMS :: Integer -> HMSTime
secondsToHMS numSeconds = HMSTime h m s where
                          (mLeft, s) = numSeconds `divMod` 60
                          (h, m)     = mLeft `divMod` 60

hmsTimeStringToHMSTime :: String -> HMSTime
hmsTimeStringToHMSTime hmsString = HMSTime h m s where
                          h:m:s:_ = map readInteger $ splitOn ":" hmsString

hmsTimeToSeconds :: HMSTime -> Integer
hmsTimeToSeconds (HMSTime {hours = h, minutes = m, seconds = s}) = h*3600 + m*60 + s

hmsTimeStringToSeconds :: String -> Integer
hmsTimeStringToSeconds = hmsTimeToSeconds . hmsTimeStringToHMSTime

readInteger :: String -> Integer
readInteger x = read x :: Integer