parent
525ab5a497
commit
22d1ba4be7
3 changed files with 21 additions and 3 deletions
|
|
@ -81,6 +81,7 @@
|
||||||
|
|
||||||
|
|
||||||
```
|
```
|
||||||
|
- Added `times.isLeapDay`
|
||||||
|
|
||||||
|
|
||||||
## Library changes
|
## Library changes
|
||||||
|
|
|
||||||
|
|
@ -637,6 +637,19 @@ proc isLeapYear*(year: int): bool =
|
||||||
doAssert not isLeapYear(1900)
|
doAssert not isLeapYear(1900)
|
||||||
year mod 4 == 0 and (year mod 100 != 0 or year mod 400 == 0)
|
year mod 4 == 0 and (year mod 100 != 0 or year mod 400 == 0)
|
||||||
|
|
||||||
|
proc isLeapDay*(t: DateTime): bool {.since: (1,1).} =
|
||||||
|
## returns whether `t` is a leap day, ie, Feb 29 in a leap year. This matters
|
||||||
|
## as it affects time offset calculations.
|
||||||
|
runnableExamples:
|
||||||
|
let t = initDateTime(29, mFeb, 2020, 00, 00, 00, utc())
|
||||||
|
doAssert t.isLeapDay
|
||||||
|
doAssert t+1.years-1.years != t
|
||||||
|
let t2 = initDateTime(28, mFeb, 2020, 00, 00, 00, utc())
|
||||||
|
doAssert not t2.isLeapDay
|
||||||
|
doAssert t2+1.years-1.years == t2
|
||||||
|
doAssertRaises(Exception): discard initDateTime(29, mFeb, 2021, 00, 00, 00, utc())
|
||||||
|
t.year.isLeapYear and t.month == mFeb and t.monthday == 29
|
||||||
|
|
||||||
proc getDaysInMonth*(month: Month, year: int): int =
|
proc getDaysInMonth*(month: Month, year: int): int =
|
||||||
## Get the number of days in ``month`` of ``year``.
|
## Get the number of days in ``month`` of ``year``.
|
||||||
# http://www.dispersiondesign.com/articles/time/number_of_days_in_a_month
|
# http://www.dispersiondesign.com/articles/time/number_of_days_in_a_month
|
||||||
|
|
|
||||||
|
|
@ -345,19 +345,23 @@ suite "ttimes":
|
||||||
test "adding/subtracting TimeInterval":
|
test "adding/subtracting TimeInterval":
|
||||||
# add/subtract TimeIntervals and Time/TimeInfo
|
# add/subtract TimeIntervals and Time/TimeInfo
|
||||||
let now = getTime().utc
|
let now = getTime().utc
|
||||||
|
let isSpecial = now.isLeapDay
|
||||||
check now + convert(Seconds, Nanoseconds, 1).nanoseconds == now + 1.seconds
|
check now + convert(Seconds, Nanoseconds, 1).nanoseconds == now + 1.seconds
|
||||||
check now + 1.weeks == now + 7.days
|
check now + 1.weeks == now + 7.days
|
||||||
check now - 1.seconds == now - 3.seconds + 2.seconds
|
check now - 1.seconds == now - 3.seconds + 2.seconds
|
||||||
check now + 65.seconds == now + 1.minutes + 5.seconds
|
check now + 65.seconds == now + 1.minutes + 5.seconds
|
||||||
check now + 60.minutes == now + 1.hours
|
check now + 60.minutes == now + 1.hours
|
||||||
check now + 24.hours == now + 1.days
|
check now + 24.hours == now + 1.days
|
||||||
|
if not isSpecial:
|
||||||
check now + 13.months == now + 1.years + 1.months
|
check now + 13.months == now + 1.years + 1.months
|
||||||
check toUnix(fromUnix(0) + 2.seconds) == 2
|
check toUnix(fromUnix(0) + 2.seconds) == 2
|
||||||
check toUnix(fromUnix(0) - 2.seconds) == -2
|
check toUnix(fromUnix(0) - 2.seconds) == -2
|
||||||
var ti1 = now + 1.years
|
var ti1 = now + 1.years
|
||||||
ti1 = ti1 - 1.years
|
ti1 = ti1 - 1.years
|
||||||
|
if not isSpecial:
|
||||||
check ti1 == now
|
check ti1 == now
|
||||||
ti1 = ti1 + 1.days
|
ti1 = ti1 + 1.days
|
||||||
|
if not isSpecial:
|
||||||
check ti1 == now + 1.days
|
check ti1 == now + 1.days
|
||||||
|
|
||||||
# Bug with adding a day to a Time
|
# Bug with adding a day to a Time
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue