Skip to content

Commit 5b00773

Browse files
MichaelXaviergaryb
authored andcommitted
Add isLeapYear function (#47)
1 parent 4ab0818 commit 5b00773

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

src/Data/Date.purs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module Data.Date
77
, day
88
, weekday
99
, diff
10+
, isLeapYear
1011
, module Data.Date.Component
1112
) where
1213

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

80+
-- | Is this year a leap year according to the proleptic Gregorian calendar?
81+
isLeapYear :: Year -> Boolean
82+
isLeapYear y = (mod y' 4 == 0) && ((mod y' 400 == 0) || not (mod y' 100 == 0))
83+
where
84+
y' = fromEnum y
85+
7986
-- TODO: these could (and probably should) be implemented in PS
8087
foreign import canonicalDateImpl :: Fn4 (Year -> Int -> Day -> Date) Year Int Day Date
8188
foreign import calcWeekday :: Fn3 Year Int Day Int

test/Test/Main.purs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ main = do
9898
log "Check that diff behaves as expected"
9999
assert $ Date.diff d2 d1 == Duration.Days 29.0
100100

101+
let unsafeYear = unsafePartial fromJust <<< toEnum
102+
log "Check that isLeapYear behaves as expected"
103+
assert $ not $ Date.isLeapYear (unsafeYear 2017)
104+
assert $ Date.isLeapYear (unsafeYear 2016)
105+
106+
101107
-- datetime ----------------------------------------------------------------
102108

103109
let dt1 = DateTime.DateTime d1 t1

0 commit comments

Comments
 (0)