Last active
June 1, 2021 18:17
-
-
Save ccapndave/2cb627cf9705a7b9f9fe9da1790d0609 to your computer and use it in GitHub Desktop.
Clock.hs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Clock (addDelta, fromHourMin, toString) where | |
import Text.Printf | |
newtype Clock = Clock Int | |
deriving Eq | |
instance Semigroup Clock where | |
Clock a <> Clock b = mkClock $ a + b | |
mkClock :: Int -> Clock | |
mkClock minutes = Clock $ minutes `mod` (24 * 60) | |
fromHourMin :: Int -> Int -> Clock | |
fromHourMin hours minutes = mkClock $ hours * 60 + minutes | |
toString :: Clock -> String | |
toString (Clock m) = printf "%02d:%02d" (m `div` 60) (m `mod` 60) | |
addDelta :: Int -> Int -> Clock -> Clock | |
addDelta hours minutes clock = | |
clock <> fromHourMin hours minutes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment