{-# LANGUAGE Safe #-}

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

#if MIN_VERSION_base(4,11,0)
#else
import Data.Semigroup hiding (option)
#endif
import Data.Typeable
import Data.Data
import Control.DeepSeq

data CalendarDiffDays = CalendarDiffDays
    { CalendarDiffDays -> Integer
cdMonths :: Integer
    , CalendarDiffDays -> Integer
cdDays :: Integer
    } deriving (CalendarDiffDays -> CalendarDiffDays -> Bool
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
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
    )

instance NFData CalendarDiffDays where
    rnf :: CalendarDiffDays -> ()
rnf (CalendarDiffDays Integer
m Integer
d) = forall a. NFData a => a -> ()
rnf Integer
m seq :: forall a b. a -> b -> b
`seq` forall a. NFData a => a -> ()
rnf Integer
d seq :: forall a b. a -> b -> b
`seq` ()

-- | Additive
instance Semigroup CalendarDiffDays where
    CalendarDiffDays Integer
m1 Integer
d1 <> :: CalendarDiffDays -> CalendarDiffDays -> CalendarDiffDays
<> CalendarDiffDays Integer
m2 Integer
d2 = Integer -> Integer -> CalendarDiffDays
CalendarDiffDays (Integer
m1 forall a. Num a => a -> a -> a
+ Integer
m2) (Integer
d1 forall a. Num a => a -> a -> a
+ Integer
d2)

-- | Additive
instance Monoid CalendarDiffDays where
    mempty :: CalendarDiffDays
mempty = Integer -> Integer -> CalendarDiffDays
CalendarDiffDays Integer
0 Integer
0
    mappend :: CalendarDiffDays -> CalendarDiffDays -> CalendarDiffDays
mappend = forall a. Semigroup a => a -> a -> a
(<>)

instance Show CalendarDiffDays where
    show :: CalendarDiffDays -> String
show (CalendarDiffDays Integer
m Integer
d) = String
"P" forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Integer
m forall a. [a] -> [a] -> [a]
++ String
"M" forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Integer
d 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 forall a. Num a => a -> a -> a
* Integer
m) (Integer
k forall a. Num a => a -> a -> a
* Integer
d)