hodatime-1.0.0.0: src/Data/HodaTime/Interval.hs
-----------------------------------------------------------------------------
-- |
-- Module : Data.HodaTime.Interval
-- Copyright : (C) 2016 Jason Johnson
-- License : BSD-style (see the file LICENSE)
-- Maintainer : Jason Johnson <jason.johnson.081@gmail.com>
-- Stability : experimental
-- Portability : POSIX, Windows
--
-- An 'Interval' is a period of time between two 'Instant's.
----------------------------------------------------------------------------
module Data.HodaTime.Interval
(
-- * Types
Interval
-- * Constructor
,interval
-- * Lenses
,start
,end
-- * Functions
,contains
,duration
)
where
import Data.HodaTime.Instant.Internal (Instant)
import Data.HodaTime.Duration.Internal (Duration)
import Data.HodaTime.Instant (difference)
import Control.DeepSeq (NFData(..))
import Data.Hashable (Hashable(..))
data Interval = Interval Instant Instant
deriving (Eq, Ord)
instance Show Interval where
showsPrec p (Interval s e) = showParen (p > 10) $
showString "interval " . showsPrec 11 s . showChar ' ' . showsPrec 11 e
instance NFData Interval where
rnf (Interval s e) = rnf s `seq` rnf e
instance Hashable Interval where
hashWithSalt salt (Interval s e) = salt `hashWithSalt` s `hashWithSalt` e
interval :: Instant -> Instant -> Interval
interval = Interval -- TODO: We probably need some checks here
-- | Lens for the start component of the 'Interval'
start :: Functor f => (Instant -> f Instant) -> Interval -> f Interval
start f (Interval s e) = flip Interval e <$> f s
{-# INLINE start #-}
-- | Lens for the end component of the 'Interval'
end :: Functor f => (Instant -> f Instant) -> Interval -> f Interval
end f (Interval s e) = Interval s <$> f e
{-# INLINE end #-}
-- | Determines if the 'Instant' is between the start and end of the 'Interval'. The interval includes the start but excludes the end
contains :: Interval -> Instant -> Bool
contains (Interval s e) i = s <= i && e > i
-- | Get the 'Duration' between the start and the end of the 'Interval'
duration :: Interval -> Duration
duration (Interval s e) = e `difference` s