module Data.Time.Calendar.CalendarDiffDays
    (
        -- * Calendar Duration
        module Data.Time.Calendar.CalendarDiffDays
    ) where

#if MIN_VERSION_base(4,8,0)
#else
import Data.Monoid
#endif
#if MIN_VERSION_base(4,9,0) && !MIN_VERSION_base(4,11,0)
import Data.Semigroup hiding (option)
#endif
import Data.Typeable
import Data.Data

data CalendarDiffDays = CalendarDiffDays
    { CalendarDiffDays -> Integer
cdMonths :: Integer
    , CalendarDiffDays -> Integer
cdDays :: Integer
    } deriving (CalendarDiffDays -> CalendarDiffDays -> Bool
(CalendarDiffDays -> CalendarDiffDays -> Bool)
-> (CalendarDiffDays -> CalendarDiffDays -> Bool)
-> Eq CalendarDiffDays
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CalendarDiffDays -> CalendarDiffDays -> Bool
$c/= :: CalendarDiffDays -> CalendarDiffDays -> Bool
== :: CalendarDiffDays -> CalendarDiffDays -> Bool
$c== :: CalendarDiffDays -> CalendarDiffDays -> Bool
Eq,
    Typeable CalendarDiffDays
Typeable CalendarDiffDays
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> CalendarDiffDays -> c CalendarDiffDays)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c CalendarDiffDays)
-> (CalendarDiffDays -> Constr)
-> (CalendarDiffDays -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c CalendarDiffDays))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c CalendarDiffDays))
-> ((forall b. Data b => b -> b)
    -> CalendarDiffDays -> CalendarDiffDays)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> CalendarDiffDays -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> CalendarDiffDays -> r)
-> (forall u.
    (forall d. Data d => d -> u) -> CalendarDiffDays -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> CalendarDiffDays -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d)
    -> CalendarDiffDays -> m CalendarDiffDays)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> CalendarDiffDays -> m CalendarDiffDays)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> CalendarDiffDays -> m CalendarDiffDays)
-> Data CalendarDiffDays
CalendarDiffDays -> DataType
CalendarDiffDays -> Constr
(forall b. Data b => b -> b)
-> CalendarDiffDays -> CalendarDiffDays
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u.
Int -> (forall d. Data d => d -> u) -> CalendarDiffDays -> u
forall u. (forall d. Data d => d -> u) -> CalendarDiffDays -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> CalendarDiffDays -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> CalendarDiffDays -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> CalendarDiffDays -> m CalendarDiffDays
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> CalendarDiffDays -> m CalendarDiffDays
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c CalendarDiffDays
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> CalendarDiffDays -> c CalendarDiffDays
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c CalendarDiffDays)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c CalendarDiffDays)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> CalendarDiffDays -> m CalendarDiffDays
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> CalendarDiffDays -> m CalendarDiffDays
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> CalendarDiffDays -> m CalendarDiffDays
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> CalendarDiffDays -> m CalendarDiffDays
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> CalendarDiffDays -> m CalendarDiffDays
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> CalendarDiffDays -> m CalendarDiffDays
gmapQi :: forall u.
Int -> (forall d. Data d => d -> u) -> CalendarDiffDays -> u
$cgmapQi :: forall u.
Int -> (forall d. Data d => d -> u) -> CalendarDiffDays -> u
gmapQ :: forall u. (forall d. Data d => d -> u) -> CalendarDiffDays -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> CalendarDiffDays -> [u]
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> CalendarDiffDays -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> CalendarDiffDays -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> CalendarDiffDays -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> CalendarDiffDays -> r
gmapT :: (forall b. Data b => b -> b)
-> CalendarDiffDays -> CalendarDiffDays
$cgmapT :: (forall b. Data b => b -> b)
-> CalendarDiffDays -> CalendarDiffDays
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c CalendarDiffDays)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c CalendarDiffDays)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c CalendarDiffDays)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c CalendarDiffDays)
dataTypeOf :: CalendarDiffDays -> DataType
$cdataTypeOf :: CalendarDiffDays -> DataType
toConstr :: CalendarDiffDays -> Constr
$ctoConstr :: CalendarDiffDays -> Constr
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c CalendarDiffDays
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c CalendarDiffDays
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> CalendarDiffDays -> c CalendarDiffDays
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> CalendarDiffDays -> c CalendarDiffDays
Data
#if __GLASGOW_HASKELL__ >= 802
    -- ^ @since 1.9.2
#endif
    ,Typeable
#if __GLASGOW_HASKELL__ >= 802
    -- ^ @since 1.9.2
#endif
    )

#if MIN_VERSION_base(4,9,0)
-- | Additive
instance Semigroup CalendarDiffDays where
    CalendarDiffDays Integer
m1 Integer
d1 <> :: CalendarDiffDays -> CalendarDiffDays -> CalendarDiffDays
<> CalendarDiffDays Integer
m2 Integer
d2 = Integer -> Integer -> CalendarDiffDays
CalendarDiffDays (Integer
m1 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
m2) (Integer
d1 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
d2)
#endif

-- | Additive
instance Monoid CalendarDiffDays where
    mempty :: CalendarDiffDays
mempty = Integer -> Integer -> CalendarDiffDays
CalendarDiffDays Integer
0 Integer
0
#if MIN_VERSION_base(4,9,0)
    mappend :: CalendarDiffDays -> CalendarDiffDays -> CalendarDiffDays
mappend = CalendarDiffDays -> CalendarDiffDays -> CalendarDiffDays
forall a. Semigroup a => a -> a -> a
(<>)
#else
    mappend (CalendarDiffDays m1 d1) (CalendarDiffDays m2 d2) = CalendarDiffDays (m1 + m2) (d1 + d2)
#endif

instance Show CalendarDiffDays where
    show :: CalendarDiffDays -> String
show (CalendarDiffDays Integer
m Integer
d) = String
"P" String -> ShowS
forall a. [a] -> [a] -> [a]
++ Integer -> String
forall a. Show a => a -> String
show Integer
m String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"M" String -> ShowS
forall a. [a] -> [a] -> [a]
++ Integer -> String
forall a. Show a => a -> String
show Integer
d String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"D"

calendarDay :: CalendarDiffDays
calendarDay :: CalendarDiffDays
calendarDay = Integer -> Integer -> CalendarDiffDays
CalendarDiffDays Integer
0 Integer
1

calendarWeek :: CalendarDiffDays
calendarWeek :: CalendarDiffDays
calendarWeek = Integer -> Integer -> CalendarDiffDays
CalendarDiffDays Integer
0 Integer
7

calendarMonth :: CalendarDiffDays
calendarMonth :: CalendarDiffDays
calendarMonth = Integer -> Integer -> CalendarDiffDays
CalendarDiffDays Integer
1 Integer
0

calendarYear :: CalendarDiffDays
calendarYear :: CalendarDiffDays
calendarYear = Integer -> Integer -> CalendarDiffDays
CalendarDiffDays Integer
12 Integer
0

-- | Scale by a factor. Note that @scaleCalendarDiffDays (-1)@ will not perfectly invert a duration, due to variable month lengths.
scaleCalendarDiffDays :: Integer -> CalendarDiffDays -> CalendarDiffDays
scaleCalendarDiffDays :: Integer -> CalendarDiffDays -> CalendarDiffDays
scaleCalendarDiffDays Integer
k (CalendarDiffDays Integer
m Integer
d) = Integer -> Integer -> CalendarDiffDays
CalendarDiffDays (Integer
k Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
m) (Integer
k Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
d)