fix https://github.com/nim-lang/RFCs/issues/211: var a: DateTime compiles and is usable (#14002) [backport:1.2]
* fix https://github.com/nim-lang/RFCs/issues/211: `var a: DateTime` works * assertValidDate checks for sentinel month
This commit is contained in:
parent
d839eb9352
commit
e3919b658f
2 changed files with 25 additions and 3 deletions
|
|
@ -260,6 +260,7 @@ type
|
||||||
Month* = enum ## Represents a month. Note that the enum starts at ``1``,
|
Month* = enum ## Represents a month. Note that the enum starts at ``1``,
|
||||||
## so ``ord(month)`` will give the month number in the
|
## so ``ord(month)`` will give the month number in the
|
||||||
## range ``1..12``.
|
## range ``1..12``.
|
||||||
|
# mInvalid = (0, "Invalid") # intentionally left out so `items` works
|
||||||
mJan = (1, "January")
|
mJan = (1, "January")
|
||||||
mFeb = "February"
|
mFeb = "February"
|
||||||
mMar = "March"
|
mMar = "March"
|
||||||
|
|
@ -296,7 +297,8 @@ when defined(nimHasStyleChecks):
|
||||||
{.pop.}
|
{.pop.}
|
||||||
|
|
||||||
type
|
type
|
||||||
MonthdayRange* = range[1..31]
|
MonthdayRange* = range[0..31]
|
||||||
|
## 0 represents an invalid day of the month
|
||||||
HourRange* = range[0..23]
|
HourRange* = range[0..23]
|
||||||
MinuteRange* = range[0..59]
|
MinuteRange* = range[0..59]
|
||||||
SecondRange* = range[0..60]
|
SecondRange* = range[0..60]
|
||||||
|
|
@ -670,7 +672,7 @@ proc getDaysInYear*(year: int): int =
|
||||||
|
|
||||||
proc assertValidDate(monthday: MonthdayRange, month: Month, year: int)
|
proc assertValidDate(monthday: MonthdayRange, month: Month, year: int)
|
||||||
{.inline.} =
|
{.inline.} =
|
||||||
assert monthday <= getDaysInMonth(month, year),
|
assert monthday > 0 and monthday <= getDaysInMonth(month, year),
|
||||||
$year & "-" & intToStr(ord(month), 2) & "-" & $monthday &
|
$year & "-" & intToStr(ord(month), 2) & "-" & $monthday &
|
||||||
" is not a valid date"
|
" is not a valid date"
|
||||||
|
|
||||||
|
|
@ -1576,9 +1578,14 @@ proc `<=`*(a, b: DateTime): bool =
|
||||||
## Returns true if ``a`` happened before or at the same time as ``b``.
|
## Returns true if ``a`` happened before or at the same time as ``b``.
|
||||||
return a.toTime <= b.toTime
|
return a.toTime <= b.toTime
|
||||||
|
|
||||||
|
proc isDefault[T](a: T): bool =
|
||||||
|
system.`==`(a, default(T))
|
||||||
|
|
||||||
proc `==`*(a, b: DateTime): bool =
|
proc `==`*(a, b: DateTime): bool =
|
||||||
## Returns true if ``a`` and ``b`` represent the same point in time.
|
## 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 =
|
proc isStaticInterval(interval: TimeInterval): bool =
|
||||||
interval.years == 0 and interval.months == 0 and
|
interval.years == 0 and interval.months == 0 and
|
||||||
|
|
|
||||||
|
|
@ -614,6 +614,21 @@ suite "ttimes":
|
||||||
doAssert x + between(x, y) == y
|
doAssert x + between(x, y) == y
|
||||||
doAssert between(x, y) == 1.months + 1.weeks
|
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)
|
||||||
|
doAssert a.monthday == 0
|
||||||
|
|
||||||
|
doAssertRaises(AssertionError): discard getDayOfWeek(a.monthday, a.month, a.year)
|
||||||
|
doAssertRaises(AssertionError): discard a.toTime
|
||||||
|
|
||||||
test "inX procs":
|
test "inX procs":
|
||||||
doAssert initDuration(seconds = 1).inSeconds == 1
|
doAssert initDuration(seconds = 1).inSeconds == 1
|
||||||
doAssert initDuration(seconds = -1).inSeconds == -1
|
doAssert initDuration(seconds = -1).inSeconds == -1
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue