{-# LANGUAGE Safe #-}
module Data.Time.Calendar.Week (
DayOfWeek (..),
dayOfWeek,
dayOfWeekDiff,
firstDayOfWeekOnAfter,
weekAllDays,
weekFirstDay,
weekLastDay,
) where
import Control.DeepSeq
import Data.Data
import Data.Fixed
import Data.Ix
import Data.Time.Calendar.Days
data DayOfWeek
= Monday
| Tuesday
| Wednesday
| Thursday
| Friday
| Saturday
| Sunday
deriving (DayOfWeek -> DayOfWeek -> Bool
(DayOfWeek -> DayOfWeek -> Bool)
-> (DayOfWeek -> DayOfWeek -> Bool) -> Eq DayOfWeek
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
(Int -> DayOfWeek -> ShowS)
-> (DayOfWeek -> String)
-> ([DayOfWeek] -> ShowS)
-> Show DayOfWeek
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]
(Int -> ReadS DayOfWeek)
-> ReadS [DayOfWeek]
-> ReadPrec DayOfWeek
-> ReadPrec [DayOfWeek]
-> Read 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
DataType
Constr
Typeable DayOfWeek
-> (forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> DayOfWeek -> c DayOfWeek)
-> (forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c DayOfWeek)
-> (DayOfWeek -> Constr)
-> (DayOfWeek -> DataType)
-> (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))
-> ((forall b. Data b => b -> b) -> DayOfWeek -> DayOfWeek)
-> (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 u. (forall d. Data d => d -> u) -> DayOfWeek -> [u])
-> (forall u.
Int -> (forall d. Data d => d -> u) -> DayOfWeek -> u)
-> (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 (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> DayOfWeek -> m DayOfWeek)
-> Data DayOfWeek
DayOfWeek -> DataType
DayOfWeek -> Constr
(forall b. Data b => b -> b) -> DayOfWeek -> DayOfWeek
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> DayOfWeek -> c DayOfWeek
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c 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)
$cSunday :: Constr
$cSaturday :: Constr
$cFriday :: Constr
$cThursday :: Constr
$cWednesday :: Constr
$cTuesday :: Constr
$cMonday :: Constr
$tDayOfWeek :: DataType
gmapMo :: (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 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 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 :: Int -> (forall d. Data d => d -> u) -> DayOfWeek -> u
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> DayOfWeek -> u
gmapQ :: (forall d. Data d => d -> u) -> DayOfWeek -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> DayOfWeek -> [u]
gmapQr :: (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 :: (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 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 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 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 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
$cp1Data :: Typeable DayOfWeek
Data, Typeable, Eq DayOfWeek
Eq DayOfWeek
-> (DayOfWeek -> DayOfWeek -> Ordering)
-> (DayOfWeek -> DayOfWeek -> Bool)
-> (DayOfWeek -> DayOfWeek -> Bool)
-> (DayOfWeek -> DayOfWeek -> Bool)
-> (DayOfWeek -> DayOfWeek -> Bool)
-> (DayOfWeek -> DayOfWeek -> DayOfWeek)
-> (DayOfWeek -> DayOfWeek -> DayOfWeek)
-> Ord 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
$cp1Ord :: Eq DayOfWeek
Ord, Ord DayOfWeek
Ord DayOfWeek
-> ((DayOfWeek, DayOfWeek) -> [DayOfWeek])
-> ((DayOfWeek, DayOfWeek) -> DayOfWeek -> Int)
-> ((DayOfWeek, DayOfWeek) -> DayOfWeek -> Int)
-> ((DayOfWeek, DayOfWeek) -> DayOfWeek -> Bool)
-> ((DayOfWeek, DayOfWeek) -> Int)
-> ((DayOfWeek, DayOfWeek) -> Int)
-> Ix 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]
$cp1Ix :: Ord 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 = ()
instance Enum DayOfWeek where
toEnum :: Int -> DayOfWeek
toEnum Int
i = case Int -> Int -> Int
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 DayOfWeek -> DayOfWeek -> Bool
forall a. Eq a => a -> a -> Bool
== DayOfWeek
wd2 = [DayOfWeek
wd1]
enumFromTo DayOfWeek
wd1 DayOfWeek
wd2 = DayOfWeek
wd1 DayOfWeek -> [DayOfWeek] -> [DayOfWeek]
forall a. a -> [a] -> [a]
: DayOfWeek -> DayOfWeek -> [DayOfWeek]
forall a. Enum a => a -> a -> [a]
enumFromTo (DayOfWeek -> DayOfWeek
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 DayOfWeek -> DayOfWeek -> Bool
forall a. Eq a => a -> a -> Bool
== DayOfWeek
wd3 = [DayOfWeek
wd1, DayOfWeek
wd2]
enumFromThenTo DayOfWeek
wd1 DayOfWeek
wd2 DayOfWeek
wd3 = DayOfWeek
wd1 DayOfWeek -> [DayOfWeek] -> [DayOfWeek]
forall a. a -> [a] -> [a]
: DayOfWeek -> DayOfWeek -> DayOfWeek -> [DayOfWeek]
forall a. Enum a => a -> a -> a -> [a]
enumFromThenTo DayOfWeek
wd2 (Int -> DayOfWeek
forall a. Enum a => Int -> a
toEnum (Int -> DayOfWeek) -> Int -> DayOfWeek
forall a b. (a -> b) -> a -> b
$ (Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
* DayOfWeek -> Int
forall a. Enum a => a -> Int
fromEnum DayOfWeek
wd2) Int -> Int -> Int
forall a. Num a => a -> a -> a
- (DayOfWeek -> Int
forall a. Enum a => a -> Int
fromEnum DayOfWeek
wd1)) DayOfWeek
wd3
dayOfWeek :: Day -> DayOfWeek
dayOfWeek :: Day -> DayOfWeek
dayOfWeek (ModifiedJulianDay Integer
d) = Int -> DayOfWeek
forall a. Enum a => Int -> a
toEnum (Int -> DayOfWeek) -> Int -> DayOfWeek
forall a b. (a -> b) -> a -> b
$ Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Integer -> Int) -> Integer -> Int
forall a b. (a -> b) -> a -> b
$ Integer
d Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
3
dayOfWeekDiff :: DayOfWeek -> DayOfWeek -> Int
dayOfWeekDiff :: DayOfWeek -> DayOfWeek -> Int
dayOfWeekDiff DayOfWeek
a DayOfWeek
b = Int -> Int -> Int
forall a. Real a => a -> a -> a
mod' (DayOfWeek -> Int
forall a. Enum a => a -> Int
fromEnum DayOfWeek
a Int -> Int -> Int
forall a. Num a => a -> a -> a
- DayOfWeek -> Int
forall a. Enum a => a -> Int
fromEnum DayOfWeek
b) Int
7
firstDayOfWeekOnAfter :: DayOfWeek -> Day -> Day
firstDayOfWeekOnAfter :: DayOfWeek -> Day -> Day
firstDayOfWeekOnAfter DayOfWeek
dw Day
d = Integer -> Day -> Day
addDays (Int -> Integer
forall a. Integral a => a -> Integer
toInteger (Int -> Integer) -> Int -> Integer
forall a b. (a -> b) -> a -> b
$ DayOfWeek -> DayOfWeek -> Int
dayOfWeekDiff DayOfWeek
dw (DayOfWeek -> Int) -> DayOfWeek -> Int
forall a b. (a -> b) -> a -> b
$ Day -> DayOfWeek
dayOfWeek Day
d) Day
d
weekAllDays :: DayOfWeek -> Day -> [Day]
weekAllDays :: DayOfWeek -> Day -> [Day]
weekAllDays DayOfWeek
firstDay Day
day = [DayOfWeek -> Day -> Day
weekFirstDay DayOfWeek
firstDay Day
day .. DayOfWeek -> Day -> Day
weekLastDay DayOfWeek
firstDay Day
day]
weekFirstDay :: DayOfWeek -> Day -> Day
weekFirstDay :: DayOfWeek -> Day -> Day
weekFirstDay DayOfWeek
firstDay Day
day = Integer -> Day -> Day
addDays (Integer -> Integer
forall a. Num a => a -> a
negate Integer
7) (Day -> Day) -> Day -> Day
forall a b. (a -> b) -> a -> b
$ DayOfWeek -> Day -> Day
firstDayOfWeekOnAfter DayOfWeek
firstDay (Day -> Day) -> Day -> Day
forall a b. (a -> b) -> a -> b
$ Day -> Day
forall a. Enum a => a -> a
succ Day
day
weekLastDay :: DayOfWeek -> Day -> Day
weekLastDay :: DayOfWeek -> Day -> Day
weekLastDay DayOfWeek
firstDay Day
day = Day -> Day
forall a. Enum a => a -> a
pred (Day -> Day) -> Day -> Day
forall a b. (a -> b) -> a -> b
$ DayOfWeek -> Day -> Day
firstDayOfWeekOnAfter DayOfWeek
firstDay (Day -> Day) -> Day -> Day
forall a b. (a -> b) -> a -> b
$ Day -> Day
forall a. Enum a => a -> a
succ Day
day