module Data.Hourglass.Calendar
( isLeapYear
, getWeekDay
, getDayOfTheYear
, daysInMonth
, dateToUnixEpoch
, dateFromUnixEpoch
, todToSeconds
, dateTimeToUnixEpoch
, dateTimeFromUnixEpoch
, dateTimeFromUnixEpochP
) where
import Data.Hourglass.Types
import Data.Hourglass.Internal
isLeapYear :: Int -> Bool
isLeapYear :: Int -> Bool
isLeapYear Int
year
| Int
year Int -> Int -> Int
forall a. Integral a => a -> a -> a
`mod` Int
4 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
0 = Bool
False
| Int
year Int -> Int -> Int
forall a. Integral a => a -> a -> a
`mod` Int
100 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
0 = Bool
True
| Int
year Int -> Int -> Int
forall a. Integral a => a -> a -> a
`mod` Int
400 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 = Bool
True
| Bool
otherwise = Bool
False
getWeekDay :: Date -> WeekDay
getWeekDay :: Date -> WeekDay
getWeekDay Date
date = Int -> WeekDay
forall a. Enum a => Int -> a
toEnum (Int
d Int -> Int -> Int
forall a. Integral a => a -> a -> a
`mod` Int
7)
where d :: Int
d = Date -> Int
daysOfDate Date
date
daysUntilMonth :: Int -> Month -> Int
daysUntilMonth :: Int -> Month -> Int
daysUntilMonth Int
y Month
m
| Int -> Bool
isLeapYear Int
y = [Int]
leapYears [Int] -> Int -> Int
forall a. [a] -> Int -> a
!! Month -> Int
forall a. Enum a => a -> Int
fromEnum Month
m
| Bool
otherwise = [Int]
normalYears [Int] -> Int -> Int
forall a. [a] -> Int -> a
!! Month -> Int
forall a. Enum a => a -> Int
fromEnum Month
m
where normalYears :: [Int]
normalYears = [ Int
0, Int
31, Int
59, Int
90, Int
120, Int
151, Int
181, Int
212, Int
243, Int
273, Int
304, Int
334, Int
365 ]
leapYears :: [Int]
leapYears = [ Int
0, Int
31, Int
60, Int
91, Int
121, Int
152, Int
182, Int
213, Int
244, Int
274, Int
305, Int
335, Int
366 ]
daysInMonth :: Int -> Month -> Int
daysInMonth :: Int -> Month -> Int
daysInMonth Int
y Month
m
| Month
m Month -> Month -> Bool
forall a. Eq a => a -> a -> Bool
== Month
February Bool -> Bool -> Bool
&& Int -> Bool
isLeapYear Int
y = Int
29
| Bool
otherwise = [Int]
days [Int] -> Int -> Int
forall a. [a] -> Int -> a
!! Month -> Int
forall a. Enum a => a -> Int
fromEnum Month
m
where days :: [Int]
days = [Int
31,Int
28,Int
31,Int
30,Int
31,Int
30,Int
31,Int
31,Int
30,Int
31,Int
30,Int
31]
getDayOfTheYear :: Date -> Int
getDayOfTheYear :: Date -> Int
getDayOfTheYear (Date Int
y Month
m Int
d) = Int -> Month -> Int
daysUntilMonth Int
y Month
m Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
d
daysBeforeYear :: Int -> Int
daysBeforeYear :: Int -> Int
daysBeforeYear Int
year = Int
y Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
365 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ (Int
y Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
4) Int -> Int -> Int
forall a. Num a => a -> a -> a
- (Int
y Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
100) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ (Int
y Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
400)
where y :: Int
y = Int
year Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1
daysOfDate :: Date -> Int
daysOfDate :: Date -> Int
daysOfDate (Date Int
y Month
m Int
d) = Int -> Int
daysBeforeYear Int
y Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int -> Month -> Int
daysUntilMonth Int
y Month
m Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
d
dateToUnixEpoch :: Date -> Elapsed
dateToUnixEpoch :: Date -> Elapsed
dateToUnixEpoch Date
date = Seconds -> Elapsed
Elapsed (Seconds -> Elapsed) -> Seconds -> Elapsed
forall a b. (a -> b) -> a -> b
$ Int64 -> Seconds
Seconds (Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Date -> Int
daysOfDate Date
date Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
epochDays) Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
* Int64
secondsPerDay)
where epochDays :: Int
epochDays = Int
719163
secondsPerDay :: Int64
secondsPerDay = Int64
86400
dateFromUnixEpoch :: Elapsed -> Date
dateFromUnixEpoch :: Elapsed -> Date
dateFromUnixEpoch Elapsed
e = DateTime -> Date
dtDate (DateTime -> Date) -> DateTime -> Date
forall a b. (a -> b) -> a -> b
$ Elapsed -> DateTime
dateTimeFromUnixEpoch Elapsed
e
todToSeconds :: TimeOfDay -> Seconds
todToSeconds :: TimeOfDay -> Seconds
todToSeconds (TimeOfDay Hours
h Minutes
m Seconds
s NanoSeconds
_) = Hours -> Seconds
forall i. TimeInterval i => i -> Seconds
toSeconds Hours
h Seconds -> Seconds -> Seconds
forall a. Num a => a -> a -> a
+ Minutes -> Seconds
forall i. TimeInterval i => i -> Seconds
toSeconds Minutes
m Seconds -> Seconds -> Seconds
forall a. Num a => a -> a -> a
+ Seconds
s
dateTimeToUnixEpoch :: DateTime -> Elapsed
dateTimeToUnixEpoch :: DateTime -> Elapsed
dateTimeToUnixEpoch (DateTime Date
d TimeOfDay
t) = Date -> Elapsed
dateToUnixEpoch Date
d Elapsed -> Elapsed -> Elapsed
forall a. Num a => a -> a -> a
+ Seconds -> Elapsed
Elapsed (TimeOfDay -> Seconds
todToSeconds TimeOfDay
t)