Skip to content

Commit e3919b6

Browse files
authored
fix nim-lang/RFCs#211: var a: DateTime compiles and is usable (#14002) [backport:1.2]
* fix nim-lang/RFCs#211: `var a: DateTime` works * assertValidDate checks for sentinel month
1 parent d839eb9 commit e3919b6

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

lib/pure/times.nim

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ type
260260
Month* = enum ## Represents a month. Note that the enum starts at ``1``,
261261
## so ``ord(month)`` will give the month number in the
262262
## range ``1..12``.
263+
# mInvalid = (0, "Invalid") # intentionally left out so `items` works
263264
mJan = (1, "January")
264265
mFeb = "February"
265266
mMar = "March"
@@ -296,7 +297,8 @@ when defined(nimHasStyleChecks):
296297
{.pop.}
297298

298299
type
299-
MonthdayRange* = range[1..31]
300+
MonthdayRange* = range[0..31]
301+
## 0 represents an invalid day of the month
300302
HourRange* = range[0..23]
301303
MinuteRange* = range[0..59]
302304
SecondRange* = range[0..60]
@@ -670,7 +672,7 @@ proc getDaysInYear*(year: int): int =
670672

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

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

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

15831590
proc isStaticInterval(interval: TimeInterval): bool =
15841591
interval.years == 0 and interval.months == 0 and

tests/stdlib/ttimes.nim

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,21 @@ suite "ttimes":
614614
doAssert x + between(x, y) == y
615615
doAssert between(x, y) == 1.months + 1.weeks
616616

617+
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+
622+
var a: DateTime
623+
doAssert a == DateTime.default
624+
doAssert ($a).len > 0 # no crash
625+
doAssert a.month.Month.ord == 0
626+
doAssert a.month.Month == cast[Month](0)
627+
doAssert a.monthday == 0
628+
629+
doAssertRaises(AssertionError): discard getDayOfWeek(a.monthday, a.month, a.year)
630+
doAssertRaises(AssertionError): discard a.toTime
631+
617632
test "inX procs":
618633
doAssert initDuration(seconds = 1).inSeconds == 1
619634
doAssert initDuration(seconds = -1).inSeconds == -1

0 commit comments

Comments
 (0)