Skip to content

Commit 22d1ba4

Browse files
authored
fixes #13543 and added times.isLeapDay (#13547)
1 parent 525ab5a commit 22d1ba4

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
8282
8383
```
84+
- Added `times.isLeapDay`
8485

8586

8687
## Library changes

lib/pure/times.nim

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,19 @@ proc isLeapYear*(year: int): bool =
637637
doAssert not isLeapYear(1900)
638638
year mod 4 == 0 and (year mod 100 != 0 or year mod 400 == 0)
639639

640+
proc isLeapDay*(t: DateTime): bool {.since: (1,1).} =
641+
## returns whether `t` is a leap day, ie, Feb 29 in a leap year. This matters
642+
## as it affects time offset calculations.
643+
runnableExamples:
644+
let t = initDateTime(29, mFeb, 2020, 00, 00, 00, utc())
645+
doAssert t.isLeapDay
646+
doAssert t+1.years-1.years != t
647+
let t2 = initDateTime(28, mFeb, 2020, 00, 00, 00, utc())
648+
doAssert not t2.isLeapDay
649+
doAssert t2+1.years-1.years == t2
650+
doAssertRaises(Exception): discard initDateTime(29, mFeb, 2021, 00, 00, 00, utc())
651+
t.year.isLeapYear and t.month == mFeb and t.monthday == 29
652+
640653
proc getDaysInMonth*(month: Month, year: int): int =
641654
## Get the number of days in ``month`` of ``year``.
642655
# http://www.dispersiondesign.com/articles/time/number_of_days_in_a_month

tests/stdlib/ttimes.nim

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -345,20 +345,24 @@ suite "ttimes":
345345
test "adding/subtracting TimeInterval":
346346
# add/subtract TimeIntervals and Time/TimeInfo
347347
let now = getTime().utc
348+
let isSpecial = now.isLeapDay
348349
check now + convert(Seconds, Nanoseconds, 1).nanoseconds == now + 1.seconds
349350
check now + 1.weeks == now + 7.days
350351
check now - 1.seconds == now - 3.seconds + 2.seconds
351352
check now + 65.seconds == now + 1.minutes + 5.seconds
352353
check now + 60.minutes == now + 1.hours
353354
check now + 24.hours == now + 1.days
354-
check now + 13.months == now + 1.years + 1.months
355+
if not isSpecial:
356+
check now + 13.months == now + 1.years + 1.months
355357
check toUnix(fromUnix(0) + 2.seconds) == 2
356358
check toUnix(fromUnix(0) - 2.seconds) == -2
357359
var ti1 = now + 1.years
358360
ti1 = ti1 - 1.years
359-
check ti1 == now
361+
if not isSpecial:
362+
check ti1 == now
360363
ti1 = ti1 + 1.days
361-
check ti1 == now + 1.days
364+
if not isSpecial:
365+
check ti1 == now + 1.days
362366

363367
# Bug with adding a day to a Time
364368
let day = 24.hours

0 commit comments

Comments
 (0)