Skip to content

Instantly share code, notes, and snippets.

@ccapndave
Last active June 1, 2021 18:17
Show Gist options
  • Save ccapndave/2cb627cf9705a7b9f9fe9da1790d0609 to your computer and use it in GitHub Desktop.
Save ccapndave/2cb627cf9705a7b9f9fe9da1790d0609 to your computer and use it in GitHub Desktop.
Clock.hs
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