{-# LANGUAGE Safe #-}

module Data.Time.Calendar.Days
    (
    -- * Days
      Day(..)
    , addDays
    , diffDays
    ) where

import Control.DeepSeq
import Data.Data
import Data.Ix

-- | The Modified Julian Day is a standard count of days, with zero being the day 1858-11-17.
newtype Day = ModifiedJulianDay
    { Day -> Integer
toModifiedJulianDay :: Integer
    } deriving (Day -> Day -> Bool
(Day -> Day -> Bool) -> (Day -> Day -> Bool) -> Eq Day
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Day -> Day -> Bool
$c/= :: Day -> Day -> Bool
== :: Day -> Day -> Bool
$c== :: Day -> Day -> Bool
Eq, Eq Day
Eq Day
-> (Day -> Day -> Ordering)
-> (Day -> Day -> Bool)
-> (Day -> Day -> Bool)
-> (Day -> Day -> Bool)
-> (Day -> Day -> Bool)
-> (Day -> Day -> Day)
-> (Day -> Day -> Day)
-> Ord Day
Day -> Day -> Bool
Day -> Day -> Ordering
Day -> Day -> Day
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Day -> Day -> Day
$cmin :: Day -> Day -> Day
max :: Day -> Day -> Day
$cmax :: Day -> Day -> Day
>= :: Day -> Day -> Bool
$c>= :: Day -> Day -> Bool
> :: Day -> Day -> Bool
$c> :: Day -> Day -> Bool
<= :: Day -> Day -> Bool
$c<= :: Day -> Day -> Bool
< :: Day -> Day -> Bool
$c< :: Day -> Day -> Bool
compare :: Day -> Day -> Ordering
$ccompare :: Day -> Day -> Ordering
Ord, Typeable Day
Typeable Day
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> Day -> c Day)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c Day)
-> (Day -> Constr)
-> (Day -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c Day))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Day))
-> ((forall b. Data b => b -> b) -> Day -> Day)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Day -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Day -> r)
-> (forall u. (forall d. Data d => d -> u) -> Day -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> Day -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> Day -> m Day)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> Day -> m Day)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> Day -> m Day)
-> Data Day
Day -> DataType
Day -> Constr
(forall b. Data b => b -> b) -> Day -> Day
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) -> Day -> u
forall u. (forall d. Data d => d -> u) -> Day -> [u]
forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Day -> r
forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Day -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Day -> m Day
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Day -> m Day
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Day
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Day -> c Day
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Day)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Day)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Day -> m Day
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Day -> m Day
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Day -> m Day
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Day -> m Day
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Day -> m Day
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Day -> m Day
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Day -> u
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Day -> u
gmapQ :: forall u. (forall d. Data d => d -> u) -> Day -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> Day -> [u]
gmapQr :: forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Day -> r
$cgmapQr :: forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Day -> r
gmapQl :: forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Day -> r
$cgmapQl :: forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Day -> r
gmapT :: (forall b. Data b => b -> b) -> Day -> Day
$cgmapT :: (forall b. Data b => b -> b) -> Day -> Day
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Day)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Day)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Day)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Day)
dataTypeOf :: Day -> DataType
$cdataTypeOf :: Day -> DataType
toConstr :: Day -> Constr
$ctoConstr :: Day -> Constr
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Day
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Day
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Day -> c Day
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Day -> c Day
Data, Typeable)

instance NFData Day where
    rnf :: Day -> ()
rnf (ModifiedJulianDay Integer
a) = Integer -> ()
forall a. NFData a => a -> ()
rnf Integer
a

instance Enum Day where
    succ :: Day -> Day
succ (ModifiedJulianDay Integer
a) = Integer -> Day
ModifiedJulianDay (Integer -> Integer
forall a. Enum a => a -> a
succ Integer
a)
    pred :: Day -> Day
pred (ModifiedJulianDay Integer
a) = Integer -> Day
ModifiedJulianDay (Integer -> Integer
forall a. Enum a => a -> a
pred Integer
a)
    toEnum :: Int -> Day
toEnum = Integer -> Day
ModifiedJulianDay (Integer -> Day) -> (Int -> Integer) -> Int -> Day
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Integer
forall a. Enum a => Int -> a
toEnum
    fromEnum :: Day -> Int
fromEnum (ModifiedJulianDay Integer
a) = Integer -> Int
forall a. Enum a => a -> Int
fromEnum Integer
a
    enumFrom :: Day -> [Day]
enumFrom (ModifiedJulianDay Integer
a) = (Integer -> Day) -> [Integer] -> [Day]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Integer -> Day
ModifiedJulianDay (Integer -> [Integer]
forall a. Enum a => a -> [a]
enumFrom Integer
a)
    enumFromThen :: Day -> Day -> [Day]
enumFromThen (ModifiedJulianDay Integer
a) (ModifiedJulianDay Integer
b) = (Integer -> Day) -> [Integer] -> [Day]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Integer -> Day
ModifiedJulianDay (Integer -> Integer -> [Integer]
forall a. Enum a => a -> a -> [a]
enumFromThen Integer
a Integer
b)
    enumFromTo :: Day -> Day -> [Day]
enumFromTo (ModifiedJulianDay Integer
a) (ModifiedJulianDay Integer
b) = (Integer -> Day) -> [Integer] -> [Day]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Integer -> Day
ModifiedJulianDay (Integer -> Integer -> [Integer]
forall a. Enum a => a -> a -> [a]
enumFromTo Integer
a Integer
b)
    enumFromThenTo :: Day -> Day -> Day -> [Day]
enumFromThenTo (ModifiedJulianDay Integer
a) (ModifiedJulianDay Integer
b) (ModifiedJulianDay Integer
c) =
        (Integer -> Day) -> [Integer] -> [Day]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Integer -> Day
ModifiedJulianDay (Integer -> Integer -> Integer -> [Integer]
forall a. Enum a => a -> a -> a -> [a]
enumFromThenTo Integer
a Integer
b Integer
c)

instance Ix Day where
    range :: (Day, Day) -> [Day]
range (ModifiedJulianDay Integer
a, ModifiedJulianDay Integer
b) = (Integer -> Day) -> [Integer] -> [Day]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Integer -> Day
ModifiedJulianDay ((Integer, Integer) -> [Integer]
forall a. Ix a => (a, a) -> [a]
range (Integer
a, Integer
b))
    index :: (Day, Day) -> Day -> Int
index (ModifiedJulianDay Integer
a, ModifiedJulianDay Integer
b) (ModifiedJulianDay Integer
c) = (Integer, Integer) -> Integer -> Int
forall a. Ix a => (a, a) -> a -> Int
index (Integer
a, Integer
b) Integer
c
    inRange :: (Day, Day) -> Day -> Bool
inRange (ModifiedJulianDay Integer
a, ModifiedJulianDay Integer
b) (ModifiedJulianDay Integer
c) = (Integer, Integer) -> Integer -> Bool
forall a. Ix a => (a, a) -> a -> Bool
inRange (Integer
a, Integer
b) Integer
c
    rangeSize :: (Day, Day) -> Int
rangeSize (ModifiedJulianDay Integer
a, ModifiedJulianDay Integer
b) = (Integer, Integer) -> Int
forall a. Ix a => (a, a) -> Int
rangeSize (Integer
a, Integer
b)

addDays :: Integer -> Day -> Day
addDays :: Integer -> Day -> Day
addDays Integer
n (ModifiedJulianDay Integer
a) = Integer -> Day
ModifiedJulianDay (Integer
a Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
n)

diffDays :: Day -> Day -> Integer
diffDays :: Day -> Day -> Integer
diffDays (ModifiedJulianDay Integer
a) (ModifiedJulianDay Integer
b) = Integer
a Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Integer
b