    {-# LANGUAGE Safe #-}

    module Data.Time.Calendar.Week
        (
          -- * Week
          DayOfWeek(..)
        , dayOfWeek
        , dayOfWeekDiff
        , firstDayOfWeekOnAfter
        ) where

    import Data.Fixed
    import Data.Ix
    import Data.Data
    import Control.DeepSeq
    import Data.Time.Calendar.Days

    data DayOfWeek
        = Monday
        | Tuesday
        | Wednesday
        | Thursday
        | Friday
        | Saturday
        | Sunday
        deriving (DayOfWeek -> DayOfWeek -> Bool
    forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
    /= :: DayOfWeek -> DayOfWeek -> Bool
    $c/= :: DayOfWeek -> DayOfWeek -> Bool
    == :: DayOfWeek -> DayOfWeek -> Bool
    $c== :: DayOfWeek -> DayOfWeek -> Bool
    Eq, Int -> DayOfWeek -> ShowS
    [DayOfWeek] -> ShowS
    DayOfWeek -> String
    forall a.
    (Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
    showList :: [DayOfWeek] -> ShowS
    $cshowList :: [DayOfWeek] -> ShowS
    show :: DayOfWeek -> String
    $cshow :: DayOfWeek -> String
    showsPrec :: Int -> DayOfWeek -> ShowS
    $cshowsPrec :: Int -> DayOfWeek -> ShowS
    Show, ReadPrec [DayOfWeek]
    ReadPrec DayOfWeek
    Int -> ReadS DayOfWeek
    ReadS [DayOfWeek]
    forall a.
    (Int -> ReadS a)
    -> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
    readListPrec :: ReadPrec [DayOfWeek]
    $creadListPrec :: ReadPrec [DayOfWeek]
    readPrec :: ReadPrec DayOfWeek
    $creadPrec :: ReadPrec DayOfWeek
    readList :: ReadS [DayOfWeek]
    $creadList :: ReadS [DayOfWeek]
    readsPrec :: Int -> ReadS DayOfWeek
    $creadsPrec :: Int -> ReadS DayOfWeek
    Read, Typeable DayOfWeek
    DayOfWeek -> DataType
    DayOfWeek -> Constr
    (forall b. Data b => b -> b) -> DayOfWeek -> DayOfWeek
    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) -> DayOfWeek -> u
    forall u. (forall d. Data d => d -> u) -> DayOfWeek -> [u]
    forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> DayOfWeek -> r
    forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> DayOfWeek -> r
    forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> DayOfWeek -> m DayOfWeek
    forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> DayOfWeek -> m DayOfWeek
    forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c DayOfWeek
    forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> DayOfWeek -> c DayOfWeek
    forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c DayOfWeek)
    forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c DayOfWeek)
    gmapMo :: forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> DayOfWeek -> m DayOfWeek
    $cgmapMo :: forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> DayOfWeek -> m DayOfWeek
    gmapMp :: forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> DayOfWeek -> m DayOfWeek
    $cgmapMp :: forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> DayOfWeek -> m DayOfWeek
    gmapM :: forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> DayOfWeek -> m DayOfWeek
    $cgmapM :: forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> DayOfWeek -> m DayOfWeek
    gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> DayOfWeek -> u
    $cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> DayOfWeek -> u
    gmapQ :: forall u. (forall d. Data d => d -> u) -> DayOfWeek -> [u]
    $cgmapQ :: forall u. (forall d. Data d => d -> u) -> DayOfWeek -> [u]
    gmapQr :: forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> DayOfWeek -> r
    $cgmapQr :: forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> DayOfWeek -> r
    gmapQl :: forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> DayOfWeek -> r
    $cgmapQl :: forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> DayOfWeek -> r
    gmapT :: (forall b. Data b => b -> b) -> DayOfWeek -> DayOfWeek
    $cgmapT :: (forall b. Data b => b -> b) -> DayOfWeek -> DayOfWeek
    dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c DayOfWeek)
    $cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c DayOfWeek)
    dataCast1 :: forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c DayOfWeek)
    $cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c DayOfWeek)
    dataTypeOf :: DayOfWeek -> DataType
    $cdataTypeOf :: DayOfWeek -> DataType
    toConstr :: DayOfWeek -> Constr
    $ctoConstr :: DayOfWeek -> Constr
    gunfold :: forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c DayOfWeek
    $cgunfold :: forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c DayOfWeek
    gfoldl :: forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> DayOfWeek -> c DayOfWeek
    $cgfoldl :: forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> DayOfWeek -> c DayOfWeek
    Data, Typeable, Eq DayOfWeek
    DayOfWeek -> DayOfWeek -> Bool
    DayOfWeek -> DayOfWeek -> Ordering
    DayOfWeek -> DayOfWeek -> DayOfWeek
    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 :: DayOfWeek -> DayOfWeek -> DayOfWeek
    $cmin :: DayOfWeek -> DayOfWeek -> DayOfWeek
    max :: DayOfWeek -> DayOfWeek -> DayOfWeek
    $cmax :: DayOfWeek -> DayOfWeek -> DayOfWeek
    >= :: DayOfWeek -> DayOfWeek -> Bool
    $c>= :: DayOfWeek -> DayOfWeek -> Bool
    > :: DayOfWeek -> DayOfWeek -> Bool
    $c> :: DayOfWeek -> DayOfWeek -> Bool
    <= :: DayOfWeek -> DayOfWeek -> Bool
    $c<= :: DayOfWeek -> DayOfWeek -> Bool
    < :: DayOfWeek -> DayOfWeek -> Bool
    $c< :: DayOfWeek -> DayOfWeek -> Bool
    compare :: DayOfWeek -> DayOfWeek -> Ordering
    $ccompare :: DayOfWeek -> DayOfWeek -> Ordering
    Ord, Ord DayOfWeek
    (DayOfWeek, DayOfWeek) -> Int
    (DayOfWeek, DayOfWeek) -> [DayOfWeek]
    (DayOfWeek, DayOfWeek) -> DayOfWeek -> Bool
    (DayOfWeek, DayOfWeek) -> DayOfWeek -> Int
    forall a.
    Ord a
    -> ((a, a) -> [a])
    -> ((a, a) -> a -> Int)
    -> ((a, a) -> a -> Int)
    -> ((a, a) -> a -> Bool)
    -> ((a, a) -> Int)
    -> ((a, a) -> Int)
    -> Ix a
    unsafeRangeSize :: (DayOfWeek, DayOfWeek) -> Int
    $cunsafeRangeSize :: (DayOfWeek, DayOfWeek) -> Int
    rangeSize :: (DayOfWeek, DayOfWeek) -> Int
    $crangeSize :: (DayOfWeek, DayOfWeek) -> Int
    inRange :: (DayOfWeek, DayOfWeek) -> DayOfWeek -> Bool
    $cinRange :: (DayOfWeek, DayOfWeek) -> DayOfWeek -> Bool
    unsafeIndex :: (DayOfWeek, DayOfWeek) -> DayOfWeek -> Int
    $cunsafeIndex :: (DayOfWeek, DayOfWeek) -> DayOfWeek -> Int
    index :: (DayOfWeek, DayOfWeek) -> DayOfWeek -> Int
    $cindex :: (DayOfWeek, DayOfWeek) -> DayOfWeek -> Int
    range :: (DayOfWeek, DayOfWeek) -> [DayOfWeek]
    $crange :: (DayOfWeek, DayOfWeek) -> [DayOfWeek]
    Ix)

    instance NFData DayOfWeek where
        rnf :: DayOfWeek -> ()
    rnf DayOfWeek
    Monday = ()
        rnf DayOfWeek
    Tuesday = ()
        rnf DayOfWeek
    Wednesday = ()
        rnf DayOfWeek
    Thursday = ()
        rnf DayOfWeek
    Friday = ()
        rnf DayOfWeek
    Saturday = ()
        rnf DayOfWeek
    Sunday = ()

    -- | \"Circular\", so for example @[Tuesday ..]@ gives an endless sequence.
    -- Also: 'fromEnum' gives [1 .. 7] for [Monday .. Sunday], and 'toEnum' performs mod 7 to give a cycle of days.
    instance Enum DayOfWeek where
        toEnum :: Int -> DayOfWeek
    toEnum Int
    i =
            case forall a. Integral a => a -> a -> a
    mod Int
    i Int
    7 of
                Int
    0 -> DayOfWeek
    Sunday
                Int
    1 -> DayOfWeek
    Monday
                Int
    2 -> DayOfWeek
    Tuesday
                Int
    3 -> DayOfWeek
    Wednesday
                Int
    4 -> DayOfWeek
    Thursday
                Int
    5 -> DayOfWeek
    Friday
                Int
    _ -> DayOfWeek
    Saturday
        fromEnum :: DayOfWeek -> Int
    fromEnum DayOfWeek
    Monday = Int
    1
        fromEnum DayOfWeek
    Tuesday = Int
    2
        fromEnum DayOfWeek
    Wednesday = Int
    3
        fromEnum DayOfWeek
    Thursday = Int
    4
        fromEnum DayOfWeek
    Friday = Int
    5
        fromEnum DayOfWeek
    Saturday = Int
    6
        fromEnum DayOfWeek
    Sunday = Int
    7
        enumFromTo :: DayOfWeek -> DayOfWeek -> [DayOfWeek]
    enumFromTo DayOfWeek
    wd1 DayOfWeek
    wd2
            | DayOfWeek
    wd1 forall a. Eq a => a -> a -> Bool
    == DayOfWeek
    wd2 = [DayOfWeek
    wd1]
        enumFromTo DayOfWeek
    wd1 DayOfWeek
    wd2 = DayOfWeek
    wd1 forall a. a -> [a] -> [a]
    : forall a. Enum a => a -> a -> [a]
    enumFromTo (forall a. Enum a => a -> a
    succ DayOfWeek
    wd1) DayOfWeek
    wd2
        enumFromThenTo :: DayOfWeek -> DayOfWeek -> DayOfWeek -> [DayOfWeek]
    enumFromThenTo DayOfWeek
    wd1 DayOfWeek
    wd2 DayOfWeek
    wd3
            | DayOfWeek
    wd2 forall a. Eq a => a -> a -> Bool
    == DayOfWeek
    wd3 = [DayOfWeek
    wd1, DayOfWeek
    wd2]
        enumFromThenTo DayOfWeek
    wd1 DayOfWeek
    wd2 DayOfWeek
    wd3 = DayOfWeek
    wd1 forall a. a -> [a] -> [a]
    : forall a. Enum a => a -> a -> a -> [a]
    enumFromThenTo DayOfWeek
    wd2 (forall a. Enum a => Int -> a
    toEnum forall a b. (a -> b) -> a -> b
    $ (Int
    2 forall a. Num a => a -> a -> a
    * forall a. Enum a => a -> Int
    fromEnum DayOfWeek
    wd2) forall a. Num a => a -> a -> a
    - (forall a. Enum a => a -> Int
    fromEnum DayOfWeek
    wd1)) DayOfWeek
    wd3

    dayOfWeek :: Day -> DayOfWeek
    dayOfWeek :: Day -> DayOfWeek
    dayOfWeek (ModifiedJulianDay Integer
    d) = forall a. Enum a => Int -> a
    toEnum forall a b. (a -> b) -> a -> b
    $ forall a. Num a => Integer -> a
    fromInteger forall a b. (a -> b) -> a -> b
    $ Integer
    d forall a. Num a => a -> a -> a
    + Integer
    3

    -- | @dayOfWeekDiff a b = a - b@ in range 0 to 6.
    -- The number of days from b to the next a.
    dayOfWeekDiff :: DayOfWeek -> DayOfWeek -> Int
    dayOfWeekDiff :: DayOfWeek -> DayOfWeek -> Int
    dayOfWeekDiff DayOfWeek
    a DayOfWeek
    b = forall a. Real a => a -> a -> a
    mod' (forall a. Enum a => a -> Int
    fromEnum DayOfWeek
    a forall a. Num a => a -> a -> a
    - forall a. Enum a => a -> Int
    fromEnum DayOfWeek
    b) Int
    7

    -- | The first day-of-week on or after some day
    firstDayOfWeekOnAfter :: DayOfWeek -> Day -> Day
    firstDayOfWeekOnAfter :: DayOfWeek -> Day -> Day
    firstDayOfWeekOnAfter DayOfWeek
    dw Day
    d = Integer -> Day -> Day
    addDays (forall a. Integral a => a -> Integer
    toInteger forall a b. (a -> b) -> a -> b
    $ DayOfWeek -> DayOfWeek -> Int
    dayOfWeekDiff DayOfWeek
    dw forall a b. (a -> b) -> a -> b
    $ Day -> DayOfWeek
    dayOfWeek Day
    d) Day
    d