]> git.netwichtig.de Git - user/henk/code/haskell/diddohs.git/blob - HMSTime.hs
On branch master
[user/henk/code/haskell/diddohs.git] / HMSTime.hs
1 module HMSTime
2 ( HMSTime(HMSTime, hours, minutes, seconds)
3 , secondsToHMS
4 , hmsTimeStringToHMSTime
5 , hmsTimeStringToSeconds
6 ) where
7
8 import Text.Printf( printf )
9 import Data.List.Split( splitOn )
10
11 data HMSTime = HMSTime { hours :: Integer, minutes :: Integer, seconds :: Integer }
12
13 instance Show HMSTime where
14   show (HMSTime h m s) = printf "%d:%02d:%02d" h m s
15
16 secondsToHMS :: Integer -> HMSTime
17 secondsToHMS seconds =  HMSTime h m s where
18                           (mLeft, s) = seconds `divMod` 60
19                           (h, m)     = mLeft `divMod` 60
20
21 hmsTimeStringToHMSTime :: String -> HMSTime
22 hmsTimeStringToHMSTime hmsString = HMSTime h m s where
23                           h:m:s:_ = map readInteger $ splitOn ":" hmsString
24
25 hmsTimeToSeconds :: HMSTime -> Integer
26 hmsTimeToSeconds (HMSTime {hours = h, minutes = m, seconds = s}) = h*3600 + m*60 + s
27
28 hmsTimeStringToSeconds :: String -> Integer
29 hmsTimeStringToSeconds = hmsTimeToSeconds . hmsTimeStringToHMSTime
30
31 hmsIntsToSeconds :: [Int] -> Int
32 hmsIntsToSeconds (h:m:s:_) = (3600*h + 60*m + s)
33
34 readInteger :: String -> Integer
35 readInteger x = read x :: Integer
36