Skip to content

Add isLeapYear function #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Data/Date.purs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Data.Date
, day
, weekday
, diff
, isLeapYear
, module Data.Date.Component
) where

Expand Down Expand Up @@ -76,6 +77,12 @@ diff :: forall d. Duration d => Date -> Date -> d
diff (Date y1 m1 d1) (Date y2 m2 d2) =
toDuration $ runFn6 calcDiff y1 (fromEnum m1) d1 y2 (fromEnum m2) d2

-- | Is this year a leap year according to the proleptic Gregorian calendar?
isLeapYear :: Year -> Boolean
isLeapYear y = (mod y' 4 == 0) && ((mod y' 400 == 0) || not (mod y' 100 == 0))
where
y' = fromEnum y

-- TODO: these could (and probably should) be implemented in PS
foreign import canonicalDateImpl :: Fn4 (Year -> Int -> Day -> Date) Year Int Day Date
foreign import calcWeekday :: Fn3 Year Int Day Int
Expand Down
6 changes: 6 additions & 0 deletions test/Test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ main = do
log "Check that diff behaves as expected"
assert $ Date.diff d2 d1 == Duration.Days 29.0

let unsafeYear = unsafePartial fromJust <<< toEnum
log "Check that isLeapYear behaves as expected"
assert $ not $ Date.isLeapYear (unsafeYear 2017)
assert $ Date.isLeapYear (unsafeYear 2016)


-- datetime ----------------------------------------------------------------

let dt1 = DateTime.DateTime d1 t1
Expand Down