diff --git a/src/Data/Date.purs b/src/Data/Date.purs index 1a74cc0..e797faa 100644 --- a/src/Data/Date.purs +++ b/src/Data/Date.purs @@ -7,6 +7,7 @@ module Data.Date , day , weekday , diff + , isLeapYear , module Data.Date.Component ) where @@ -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 diff --git a/test/Test/Main.purs b/test/Test/Main.purs index a5a147b..200008b 100644 --- a/test/Test/Main.purs +++ b/test/Test/Main.purs @@ -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