Skip to content

Commit 901a20b

Browse files
committed
assertValidDate checks for sentinel month
1 parent 9bff8a9 commit 901a20b

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

lib/pure/times.nim

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ proc getDaysInYear*(year: int): int =
672672

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

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

1581+
proc isDefault[T](a: T): bool =
1582+
system.`==`(a, default(T))
1583+
15811584
proc `==`*(a, b: DateTime): bool =
15821585
## Returns true if ``a`` and ``b`` represent the same point in time.
1583-
return a.toTime == b.toTime
1586+
if a.isDefault: b.isDefault
1587+
elif b.isDefault: false
1588+
else: a.toTime == b.toTime
15841589

15851590
proc isStaticInterval(interval: TimeInterval): bool =
15861591
interval.years == 0 and interval.months == 0 and

tests/stdlib/ttimes.nim

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -615,14 +615,19 @@ suite "ttimes":
615615
doAssert between(x, y) == 1.months + 1.weeks
616616

617617
test "default DateTime": # https://github.com/nim-lang/RFCs/issues/211
618+
var num = 0
619+
for ai in Month: num.inc
620+
doAssert num == 12
621+
618622
var a: DateTime
619623
doAssert a == DateTime.default
620624
doAssert ($a).len > 0 # no crash
621625
doAssert a.month.Month.ord == 0
622626
doAssert a.month.Month == cast[Month](0)
623-
var num = 0
624-
for ai in Month: num.inc
625-
doAssert num == 12
627+
doAssert a.monthday == 0
628+
629+
doAssertRaises(AssertionError): discard getDayOfWeek(a.monthday, a.month, a.year)
630+
doAssertRaises(AssertionError): discard a.toTime
626631

627632
test "inX procs":
628633
doAssert initDuration(seconds = 1).inSeconds == 1

0 commit comments

Comments
 (0)