Skip to content

Commit

Permalink
assertValidDate checks for sentinel month
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour committed Apr 17, 2020
1 parent 9bff8a9 commit 901a20b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
9 changes: 7 additions & 2 deletions lib/pure/times.nim
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ proc getDaysInYear*(year: int): int =

proc assertValidDate(monthday: MonthdayRange, month: Month, year: int)
{.inline.} =
assert monthday <= getDaysInMonth(month, year),
assert monthday > 0 and monthday <= getDaysInMonth(month, year),
$year & "-" & intToStr(ord(month), 2) & "-" & $monthday &
" is not a valid date"

Expand Down Expand Up @@ -1578,9 +1578,14 @@ proc `<=`*(a, b: DateTime): bool =
## Returns true if ``a`` happened before or at the same time as ``b``.
return a.toTime <= b.toTime

proc isDefault[T](a: T): bool =
system.`==`(a, default(T))

proc `==`*(a, b: DateTime): bool =
## Returns true if ``a`` and ``b`` represent the same point in time.
return a.toTime == b.toTime
if a.isDefault: b.isDefault
elif b.isDefault: false
else: a.toTime == b.toTime

proc isStaticInterval(interval: TimeInterval): bool =
interval.years == 0 and interval.months == 0 and
Expand Down
11 changes: 8 additions & 3 deletions tests/stdlib/ttimes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -615,14 +615,19 @@ suite "ttimes":
doAssert between(x, y) == 1.months + 1.weeks

test "default DateTime": # https://github.com/nim-lang/RFCs/issues/211
var num = 0
for ai in Month: num.inc
doAssert num == 12

var a: DateTime
doAssert a == DateTime.default
doAssert ($a).len > 0 # no crash
doAssert a.month.Month.ord == 0
doAssert a.month.Month == cast[Month](0)
var num = 0
for ai in Month: num.inc
doAssert num == 12
doAssert a.monthday == 0

doAssertRaises(AssertionError): discard getDayOfWeek(a.monthday, a.month, a.year)
doAssertRaises(AssertionError): discard a.toTime

test "inX procs":
doAssert initDuration(seconds = 1).inSeconds == 1
Expand Down

0 comments on commit 901a20b

Please sign in to comment.