packages feed

cqrs-example-0.8.0: src/CQRSExample/Duration.hs

module CQRSExample.Duration
       ( Duration
       , hours
       , minutes
       , seconds
       , toMinutes
       , toSeconds
       ) where

import           Data.SafeCopy (base, deriveSafeCopySimple)
import           Data.Typeable (Typeable)

-- | Duration type.
data Duration = Duration Integer
                deriving (Typeable, Show, Eq)

-- | Create a new duration from a number of minutes.
minutes :: Integer -> Duration
minutes m = Duration (60 * m)

-- | Create a new duration from a number of seconds.
seconds :: Integer -> Duration
seconds s = Duration s

-- | Create a new duration from a number of hours.
hours :: Integer -> Duration
hours h = Duration (60 * 60 * h)

-- | Get the number of seconds from a duration.
toSeconds :: Duration -> Integer
toSeconds (Duration s) = s

-- | Get the number of minutes from a duration.
-- Any seconds are truncated.
toMinutes :: Duration -> Integer
toMinutes (Duration s) = s `div` 60

-- SafeCopy instances.

$(deriveSafeCopySimple 1 'base ''Duration)