diff options
author | Oscar Nihlgård <oscarnihlgard@gmail.com> | 2017-10-10 15:47:12 +0200 |
---|---|---|
committer | Oscar Nihlgård <oscarnihlgard@gmail.com> | 2017-10-10 15:47:12 +0200 |
commit | f2ba3d174c3e6b65d5785d1e621ce702c4bc36e1 (patch) | |
tree | 8399a85ef13994dc83849dac064169f81486a86b | |
parent | 1063085850d9d32e82302854cf3ac64049bf998f (diff) | |
download | Nim-f2ba3d174c3e6b65d5785d1e621ce702c4bc36e1.tar.gz |
Fix countLeapYears
-rw-r--r-- | lib/pure/times.nim | 2 | ||||
-rw-r--r-- | tests/stdlib/ttimes.nim | 6 |
2 files changed, 7 insertions, 1 deletions
diff --git a/lib/pure/times.nim b/lib/pure/times.nim index 96668c4f8..7dd428904 100644 --- a/lib/pure/times.nim +++ b/lib/pure/times.nim @@ -1025,7 +1025,7 @@ proc countLeapYears*(yearSpan: int): int = ## counts the number of leap years up to January 1st of a given year. ## Keep in mind that if specified year is a leap year, the leap day ## has not happened before January 1st of that year. - (((yearSpan - 1) / 4) - ((yearSpan - 1) / 100) + ((yearSpan - 1) / 400)).int + (yearSpan - 1) div 4 - (yearSpan - 1) div 100 + (yearSpan - 1) div 400 proc countDays*(yearSpan: int): int = ## Returns the number of days spanned by a given number of years. diff --git a/tests/stdlib/ttimes.nim b/tests/stdlib/ttimes.nim index 05c91ccb2..84e00f8de 100644 --- a/tests/stdlib/ttimes.nim +++ b/tests/stdlib/ttimes.nim @@ -235,3 +235,9 @@ block dstTest: parsedJul = parse("2016-07-01 04:00:00+01:00", "yyyy-MM-dd HH:mm:sszzz") doAssert toTime(parsedJan) == fromSeconds(1451962800) doAssert toTime(parsedJul) == fromSeconds(1467342000) + +block countLeapYears: + # 1920, 2004 and 2020 are leap years, and should be counted starting at the following year + doAssert countLeapYears(1920) + 1 == countLeapYears(1921) + doAssert countLeapYears(2004) + 1 == countLeapYears(2005) + doAssert countLeapYears(2020) + 1 == countLeapYears(2021) \ No newline at end of file |