Better times module (#6552)
* First work on better timezones * Update tests to new api. Removed tests for checking that `isDst` was included when formatting, since `isDst` no longer affects utc offset (the entire utc offset is stored directly in `utcOffset` instead). * Deprecate getLocaltime & getGmTime * Add `now()` as a shorthand for GetTIme().inZone(Local) * Add FedericoCeratto's timezone tests (#6548) * Run more tests in all timezones * Make month enum start at 1 instead of 0 * Deprecate getDayOfWeekJulian * Fix issues with gc safety * Rename TimeInfo => DateTime * Fixes #6465 * Improve isLeapYear * FIx handling negative adjTime * Cleanup: - deprecated toSeconds and fromSeconds, added fromUnix and toUnix instead (that returns int64 instead of float) - added missing doc comments - removed some unnecessary JS specific implementations * Fix misstake in JS `-` for Time * Update usage of TimeEffect * Removed unecessary use of `difftime` * JS fix for local tz * Fix subtraction of months * Fix `days` field in `toTimeInterval` * Style and docs * Fix getDayOfYear for real this time... * Fix handling of adding/subtracting time across dst transitions * Fix some bad usage of the times module in the stdlib * Revert to use proper time resoultion for seeding in random.nim * Move deprecated procs to bottom of file * Always use `epochTime` in `randomize` * Remove TimeInterval normalization * Fixes #6905 * Fix getDayOfWeek for year < 1 * Export toEpochDay/fromEpochDay and change year/month/monthday order * Add asserts for checking that the monthday is valid * Fix some remaining ambiguous references to `Time` * Fix ambiguous reference to Time
This commit is contained in:
parent
07fe1aa655
commit
a879973081
13 changed files with 1153 additions and 1014 deletions
|
|
@ -609,11 +609,12 @@ proc clock_nanosleep*(a1: ClockId, a2: cint, a3: var Timespec,
|
||||||
proc clock_settime*(a1: ClockId, a2: var Timespec): cint {.
|
proc clock_settime*(a1: ClockId, a2: var Timespec): cint {.
|
||||||
importc, header: "<time.h>".}
|
importc, header: "<time.h>".}
|
||||||
|
|
||||||
|
proc `==`*(a, b: Time): bool {.borrow.}
|
||||||
|
proc `-`*(a, b: Time): Time {.borrow.}
|
||||||
proc ctime*(a1: var Time): cstring {.importc, header: "<time.h>".}
|
proc ctime*(a1: var Time): cstring {.importc, header: "<time.h>".}
|
||||||
proc ctime_r*(a1: var Time, a2: cstring): cstring {.importc, header: "<time.h>".}
|
proc ctime_r*(a1: var Time, a2: cstring): cstring {.importc, header: "<time.h>".}
|
||||||
proc difftime*(a1, a2: Time): cdouble {.importc, header: "<time.h>".}
|
proc difftime*(a1, a2: Time): cdouble {.importc, header: "<time.h>".}
|
||||||
proc getdate*(a1: cstring): ptr Tm {.importc, header: "<time.h>".}
|
proc getdate*(a1: cstring): ptr Tm {.importc, header: "<time.h>".}
|
||||||
|
|
||||||
proc gmtime*(a1: var Time): ptr Tm {.importc, header: "<time.h>".}
|
proc gmtime*(a1: var Time): ptr Tm {.importc, header: "<time.h>".}
|
||||||
proc gmtime_r*(a1: var Time, a2: var Tm): ptr Tm {.importc, header: "<time.h>".}
|
proc gmtime_r*(a1: var Time, a2: var Tm): ptr Tm {.importc, header: "<time.h>".}
|
||||||
proc localtime*(a1: var Time): ptr Tm {.importc, header: "<time.h>".}
|
proc localtime*(a1: var Time): ptr Tm {.importc, header: "<time.h>".}
|
||||||
|
|
|
||||||
|
|
@ -12,8 +12,6 @@
|
||||||
|
|
||||||
# To be included from posix.nim!
|
# To be included from posix.nim!
|
||||||
|
|
||||||
from times import Time
|
|
||||||
|
|
||||||
const
|
const
|
||||||
hasSpawnH = not defined(haiku) # should exist for every Posix system nowadays
|
hasSpawnH = not defined(haiku) # should exist for every Posix system nowadays
|
||||||
hasAioH = defined(linux)
|
hasAioH = defined(linux)
|
||||||
|
|
@ -40,6 +38,8 @@ type
|
||||||
const SIG_HOLD* = cast[SigHandler](2)
|
const SIG_HOLD* = cast[SigHandler](2)
|
||||||
|
|
||||||
type
|
type
|
||||||
|
Time* {.importc: "time_t", header: "<time.h>".} = distinct clong
|
||||||
|
|
||||||
Timespec* {.importc: "struct timespec",
|
Timespec* {.importc: "struct timespec",
|
||||||
header: "<time.h>", final, pure.} = object ## struct timespec
|
header: "<time.h>", final, pure.} = object ## struct timespec
|
||||||
tv_sec*: Time ## Seconds.
|
tv_sec*: Time ## Seconds.
|
||||||
|
|
|
||||||
|
|
@ -9,8 +9,6 @@
|
||||||
|
|
||||||
{.deadCodeElim:on.}
|
{.deadCodeElim:on.}
|
||||||
|
|
||||||
from times import Time
|
|
||||||
|
|
||||||
const
|
const
|
||||||
hasSpawnH = not defined(haiku) # should exist for every Posix system nowadays
|
hasSpawnH = not defined(haiku) # should exist for every Posix system nowadays
|
||||||
hasAioH = defined(linux)
|
hasAioH = defined(linux)
|
||||||
|
|
@ -36,6 +34,8 @@ type
|
||||||
{.deprecated: [TSocketHandle: SocketHandle].}
|
{.deprecated: [TSocketHandle: SocketHandle].}
|
||||||
|
|
||||||
type
|
type
|
||||||
|
Time* {.importc: "time_t", header: "<time.h>".} = distinct int
|
||||||
|
|
||||||
Timespec* {.importc: "struct timespec",
|
Timespec* {.importc: "struct timespec",
|
||||||
header: "<time.h>", final, pure.} = object ## struct timespec
|
header: "<time.h>", final, pure.} = object ## struct timespec
|
||||||
tv_sec*: Time ## Seconds.
|
tv_sec*: Time ## Seconds.
|
||||||
|
|
|
||||||
|
|
@ -51,7 +51,7 @@ proc setCookie*(key, value: string, domain = "", path = "",
|
||||||
if secure: result.add("; Secure")
|
if secure: result.add("; Secure")
|
||||||
if httpOnly: result.add("; HttpOnly")
|
if httpOnly: result.add("; HttpOnly")
|
||||||
|
|
||||||
proc setCookie*(key, value: string, expires: TimeInfo,
|
proc setCookie*(key, value: string, expires: DateTime,
|
||||||
domain = "", path = "", noName = false,
|
domain = "", path = "", noName = false,
|
||||||
secure = false, httpOnly = false): string =
|
secure = false, httpOnly = false): string =
|
||||||
## Creates a command in the format of
|
## Creates a command in the format of
|
||||||
|
|
@ -63,9 +63,9 @@ proc setCookie*(key, value: string, expires: TimeInfo,
|
||||||
noname, secure, httpOnly)
|
noname, secure, httpOnly)
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
var tim = Time(int(getTime()) + 76 * (60 * 60 * 24))
|
var tim = fromUnix(getTime().toUnix + 76 * (60 * 60 * 24))
|
||||||
|
|
||||||
let cookie = setCookie("test", "value", tim.getGMTime())
|
let cookie = setCookie("test", "value", tim.utc)
|
||||||
when not defined(testing):
|
when not defined(testing):
|
||||||
echo cookie
|
echo cookie
|
||||||
let start = "Set-Cookie: test=value; Expires="
|
let start = "Set-Cookie: test=value; Expires="
|
||||||
|
|
|
||||||
|
|
@ -277,15 +277,16 @@ proc registerTimer*[T](s: Selector[T], timeout: int, oneshot: bool,
|
||||||
var events = {Event.Timer}
|
var events = {Event.Timer}
|
||||||
var epv = EpollEvent(events: EPOLLIN or EPOLLRDHUP)
|
var epv = EpollEvent(events: EPOLLIN or EPOLLRDHUP)
|
||||||
epv.data.u64 = fdi.uint
|
epv.data.u64 = fdi.uint
|
||||||
|
|
||||||
if oneshot:
|
if oneshot:
|
||||||
new_ts.it_interval.tv_sec = 0.Time
|
new_ts.it_interval.tv_sec = posix.Time(0)
|
||||||
new_ts.it_interval.tv_nsec = 0
|
new_ts.it_interval.tv_nsec = 0
|
||||||
new_ts.it_value.tv_sec = (timeout div 1_000).Time
|
new_ts.it_value.tv_sec = posix.Time(timeout div 1_000)
|
||||||
new_ts.it_value.tv_nsec = (timeout %% 1_000) * 1_000_000
|
new_ts.it_value.tv_nsec = (timeout %% 1_000) * 1_000_000
|
||||||
incl(events, Event.Oneshot)
|
incl(events, Event.Oneshot)
|
||||||
epv.events = epv.events or EPOLLONESHOT
|
epv.events = epv.events or EPOLLONESHOT
|
||||||
else:
|
else:
|
||||||
new_ts.it_interval.tv_sec = (timeout div 1000).Time
|
new_ts.it_interval.tv_sec = posix.Time(timeout div 1000)
|
||||||
new_ts.it_interval.tv_nsec = (timeout %% 1_000) * 1_000_000
|
new_ts.it_interval.tv_nsec = (timeout %% 1_000) * 1_000_000
|
||||||
new_ts.it_value.tv_sec = new_ts.it_interval.tv_sec
|
new_ts.it_value.tv_sec = new_ts.it_interval.tv_sec
|
||||||
new_ts.it_value.tv_nsec = new_ts.it_interval.tv_nsec
|
new_ts.it_value.tv_nsec = new_ts.it_interval.tv_nsec
|
||||||
|
|
|
||||||
|
|
@ -452,10 +452,10 @@ proc selectInto*[T](s: Selector[T], timeout: int,
|
||||||
|
|
||||||
if timeout != -1:
|
if timeout != -1:
|
||||||
if timeout >= 1000:
|
if timeout >= 1000:
|
||||||
tv.tv_sec = (timeout div 1_000).Time
|
tv.tv_sec = posix.Time(timeout div 1_000)
|
||||||
tv.tv_nsec = (timeout %% 1_000) * 1_000_000
|
tv.tv_nsec = (timeout %% 1_000) * 1_000_000
|
||||||
else:
|
else:
|
||||||
tv.tv_sec = 0.Time
|
tv.tv_sec = posix.Time(0)
|
||||||
tv.tv_nsec = timeout * 1_000_000
|
tv.tv_nsec = timeout * 1_000_000
|
||||||
else:
|
else:
|
||||||
ptv = nil
|
ptv = nil
|
||||||
|
|
|
||||||
|
|
@ -88,7 +88,7 @@ proc generatedTime*(oid: Oid): Time =
|
||||||
var tmp: int32
|
var tmp: int32
|
||||||
var dummy = oid.time
|
var dummy = oid.time
|
||||||
bigEndian32(addr(tmp), addr(dummy))
|
bigEndian32(addr(tmp), addr(dummy))
|
||||||
result = Time(tmp)
|
result = fromUnix(tmp)
|
||||||
|
|
||||||
when not defined(testing) and isMainModule:
|
when not defined(testing) and isMainModule:
|
||||||
let xo = genOid()
|
let xo = genOid()
|
||||||
|
|
|
||||||
|
|
@ -173,33 +173,33 @@ proc findExe*(exe: string, followSymlinks: bool = true;
|
||||||
return x
|
return x
|
||||||
result = ""
|
result = ""
|
||||||
|
|
||||||
proc getLastModificationTime*(file: string): Time {.rtl, extern: "nos$1".} =
|
proc getLastModificationTime*(file: string): times.Time {.rtl, extern: "nos$1".} =
|
||||||
## Returns the `file`'s last modification time.
|
## Returns the `file`'s last modification time.
|
||||||
when defined(posix):
|
when defined(posix):
|
||||||
var res: Stat
|
var res: Stat
|
||||||
if stat(file, res) < 0'i32: raiseOSError(osLastError())
|
if stat(file, res) < 0'i32: raiseOSError(osLastError())
|
||||||
return res.st_mtime
|
return fromUnix(res.st_mtime.int64)
|
||||||
else:
|
else:
|
||||||
var f: WIN32_FIND_DATA
|
var f: WIN32_FIND_DATA
|
||||||
var h = findFirstFile(file, f)
|
var h = findFirstFile(file, f)
|
||||||
if h == -1'i32: raiseOSError(osLastError())
|
if h == -1'i32: raiseOSError(osLastError())
|
||||||
result = winTimeToUnixTime(rdFileTime(f.ftLastWriteTime))
|
result = fromUnix(winTimeToUnixTime(rdFileTime(f.ftLastWriteTime)).int64)
|
||||||
findClose(h)
|
findClose(h)
|
||||||
|
|
||||||
proc getLastAccessTime*(file: string): Time {.rtl, extern: "nos$1".} =
|
proc getLastAccessTime*(file: string): times.Time {.rtl, extern: "nos$1".} =
|
||||||
## Returns the `file`'s last read or write access time.
|
## Returns the `file`'s last read or write access time.
|
||||||
when defined(posix):
|
when defined(posix):
|
||||||
var res: Stat
|
var res: Stat
|
||||||
if stat(file, res) < 0'i32: raiseOSError(osLastError())
|
if stat(file, res) < 0'i32: raiseOSError(osLastError())
|
||||||
return res.st_atime
|
return fromUnix(res.st_atime.int64)
|
||||||
else:
|
else:
|
||||||
var f: WIN32_FIND_DATA
|
var f: WIN32_FIND_DATA
|
||||||
var h = findFirstFile(file, f)
|
var h = findFirstFile(file, f)
|
||||||
if h == -1'i32: raiseOSError(osLastError())
|
if h == -1'i32: raiseOSError(osLastError())
|
||||||
result = winTimeToUnixTime(rdFileTime(f.ftLastAccessTime))
|
result = fromUnix(winTimeToUnixTime(rdFileTime(f.ftLastAccessTime)).int64)
|
||||||
findClose(h)
|
findClose(h)
|
||||||
|
|
||||||
proc getCreationTime*(file: string): Time {.rtl, extern: "nos$1".} =
|
proc getCreationTime*(file: string): times.Time {.rtl, extern: "nos$1".} =
|
||||||
## Returns the `file`'s creation time.
|
## Returns the `file`'s creation time.
|
||||||
##
|
##
|
||||||
## **Note:** Under POSIX OS's, the returned time may actually be the time at
|
## **Note:** Under POSIX OS's, the returned time may actually be the time at
|
||||||
|
|
@ -208,12 +208,12 @@ proc getCreationTime*(file: string): Time {.rtl, extern: "nos$1".} =
|
||||||
when defined(posix):
|
when defined(posix):
|
||||||
var res: Stat
|
var res: Stat
|
||||||
if stat(file, res) < 0'i32: raiseOSError(osLastError())
|
if stat(file, res) < 0'i32: raiseOSError(osLastError())
|
||||||
return res.st_ctime
|
return fromUnix(res.st_ctime.int64)
|
||||||
else:
|
else:
|
||||||
var f: WIN32_FIND_DATA
|
var f: WIN32_FIND_DATA
|
||||||
var h = findFirstFile(file, f)
|
var h = findFirstFile(file, f)
|
||||||
if h == -1'i32: raiseOSError(osLastError())
|
if h == -1'i32: raiseOSError(osLastError())
|
||||||
result = winTimeToUnixTime(rdFileTime(f.ftCreationTime))
|
result = fromUnix(winTimeToUnixTime(rdFileTime(f.ftCreationTime)).int64)
|
||||||
findClose(h)
|
findClose(h)
|
||||||
|
|
||||||
proc fileNewer*(a, b: string): bool {.rtl, extern: "nos$1".} =
|
proc fileNewer*(a, b: string): bool {.rtl, extern: "nos$1".} =
|
||||||
|
|
@ -1443,7 +1443,7 @@ proc sleep*(milsecs: int) {.rtl, extern: "nos$1", tags: [TimeEffect].} =
|
||||||
winlean.sleep(int32(milsecs))
|
winlean.sleep(int32(milsecs))
|
||||||
else:
|
else:
|
||||||
var a, b: Timespec
|
var a, b: Timespec
|
||||||
a.tv_sec = Time(milsecs div 1000)
|
a.tv_sec = posix.Time(milsecs div 1000)
|
||||||
a.tv_nsec = (milsecs mod 1000) * 1000 * 1000
|
a.tv_nsec = (milsecs mod 1000) * 1000 * 1000
|
||||||
discard posix.nanosleep(a, b)
|
discard posix.nanosleep(a, b)
|
||||||
|
|
||||||
|
|
@ -1481,16 +1481,17 @@ type
|
||||||
size*: BiggestInt # Size of file.
|
size*: BiggestInt # Size of file.
|
||||||
permissions*: set[FilePermission] # File permissions
|
permissions*: set[FilePermission] # File permissions
|
||||||
linkCount*: BiggestInt # Number of hard links the file object has.
|
linkCount*: BiggestInt # Number of hard links the file object has.
|
||||||
lastAccessTime*: Time # Time file was last accessed.
|
lastAccessTime*: times.Time # Time file was last accessed.
|
||||||
lastWriteTime*: Time # Time file was last modified/written to.
|
lastWriteTime*: times.Time # Time file was last modified/written to.
|
||||||
creationTime*: Time # Time file was created. Not supported on all systems!
|
creationTime*: times.Time # Time file was created. Not supported on all systems!
|
||||||
|
|
||||||
template rawToFormalFileInfo(rawInfo, path, formalInfo): untyped =
|
template rawToFormalFileInfo(rawInfo, path, formalInfo): untyped =
|
||||||
## Transforms the native file info structure into the one nim uses.
|
## Transforms the native file info structure into the one nim uses.
|
||||||
## 'rawInfo' is either a 'TBY_HANDLE_FILE_INFORMATION' structure on Windows,
|
## 'rawInfo' is either a 'TBY_HANDLE_FILE_INFORMATION' structure on Windows,
|
||||||
## or a 'Stat' structure on posix
|
## or a 'Stat' structure on posix
|
||||||
when defined(Windows):
|
when defined(Windows):
|
||||||
template toTime(e: FILETIME): untyped {.gensym.} = winTimeToUnixTime(rdFileTime(e)) # local templates default to bind semantics
|
template toTime(e: FILETIME): untyped {.gensym.} =
|
||||||
|
fromUnix(winTimeToUnixTime(rdFileTime(e)).int64) # local templates default to bind semantics
|
||||||
template merge(a, b): untyped = a or (b shl 32)
|
template merge(a, b): untyped = a or (b shl 32)
|
||||||
formalInfo.id.device = rawInfo.dwVolumeSerialNumber
|
formalInfo.id.device = rawInfo.dwVolumeSerialNumber
|
||||||
formalInfo.id.file = merge(rawInfo.nFileIndexLow, rawInfo.nFileIndexHigh)
|
formalInfo.id.file = merge(rawInfo.nFileIndexLow, rawInfo.nFileIndexHigh)
|
||||||
|
|
@ -1522,9 +1523,9 @@ template rawToFormalFileInfo(rawInfo, path, formalInfo): untyped =
|
||||||
formalInfo.id = (rawInfo.st_dev, rawInfo.st_ino)
|
formalInfo.id = (rawInfo.st_dev, rawInfo.st_ino)
|
||||||
formalInfo.size = rawInfo.st_size
|
formalInfo.size = rawInfo.st_size
|
||||||
formalInfo.linkCount = rawInfo.st_Nlink.BiggestInt
|
formalInfo.linkCount = rawInfo.st_Nlink.BiggestInt
|
||||||
formalInfo.lastAccessTime = rawInfo.st_atime
|
formalInfo.lastAccessTime = fromUnix(rawInfo.st_atime.int64)
|
||||||
formalInfo.lastWriteTime = rawInfo.st_mtime
|
formalInfo.lastWriteTime = fromUnix(rawInfo.st_mtime.int64)
|
||||||
formalInfo.creationTime = rawInfo.st_ctime
|
formalInfo.creationTime = fromUnix(rawInfo.st_ctime.int64)
|
||||||
|
|
||||||
result.permissions = {}
|
result.permissions = {}
|
||||||
checkAndIncludeMode(S_IRUSR, fpUserRead)
|
checkAndIncludeMode(S_IRUSR, fpUserRead)
|
||||||
|
|
|
||||||
|
|
@ -1060,10 +1060,10 @@ elif not defined(useNimRtl):
|
||||||
var tmspec: Timespec
|
var tmspec: Timespec
|
||||||
|
|
||||||
if timeout >= 1000:
|
if timeout >= 1000:
|
||||||
tmspec.tv_sec = (timeout div 1_000).Time
|
tmspec.tv_sec = posix.Time(timeout div 1_000)
|
||||||
tmspec.tv_nsec = (timeout %% 1_000) * 1_000_000
|
tmspec.tv_nsec = (timeout %% 1_000) * 1_000_000
|
||||||
else:
|
else:
|
||||||
tmspec.tv_sec = 0.Time
|
tmspec.tv_sec = posix.Time(0)
|
||||||
tmspec.tv_nsec = (timeout * 1_000_000)
|
tmspec.tv_nsec = (timeout * 1_000_000)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
@ -1109,20 +1109,20 @@ elif not defined(useNimRtl):
|
||||||
var b: Timespec
|
var b: Timespec
|
||||||
b.tv_sec = e.tv_sec
|
b.tv_sec = e.tv_sec
|
||||||
b.tv_nsec = e.tv_nsec
|
b.tv_nsec = e.tv_nsec
|
||||||
e.tv_sec = (e.tv_sec - s.tv_sec).Time
|
e.tv_sec = e.tv_sec - s.tv_sec
|
||||||
if e.tv_nsec >= s.tv_nsec:
|
if e.tv_nsec >= s.tv_nsec:
|
||||||
e.tv_nsec -= s.tv_nsec
|
e.tv_nsec -= s.tv_nsec
|
||||||
else:
|
else:
|
||||||
if e.tv_sec == 0.Time:
|
if e.tv_sec == posix.Time(0):
|
||||||
raise newException(ValueError, "System time was modified")
|
raise newException(ValueError, "System time was modified")
|
||||||
else:
|
else:
|
||||||
diff = s.tv_nsec - e.tv_nsec
|
diff = s.tv_nsec - e.tv_nsec
|
||||||
e.tv_nsec = 1_000_000_000 - diff
|
e.tv_nsec = 1_000_000_000 - diff
|
||||||
t.tv_sec = (t.tv_sec - e.tv_sec).Time
|
t.tv_sec = t.tv_sec - e.tv_sec
|
||||||
if t.tv_nsec >= e.tv_nsec:
|
if t.tv_nsec >= e.tv_nsec:
|
||||||
t.tv_nsec -= e.tv_nsec
|
t.tv_nsec -= e.tv_nsec
|
||||||
else:
|
else:
|
||||||
t.tv_sec = (int(t.tv_sec) - 1).Time
|
t.tv_sec = t.tv_sec - posix.Time(1)
|
||||||
diff = e.tv_nsec - t.tv_nsec
|
diff = e.tv_nsec - t.tv_nsec
|
||||||
t.tv_nsec = 1_000_000_000 - diff
|
t.tv_nsec = 1_000_000_000 - diff
|
||||||
s.tv_sec = b.tv_sec
|
s.tv_sec = b.tv_sec
|
||||||
|
|
@ -1154,10 +1154,10 @@ elif not defined(useNimRtl):
|
||||||
raiseOSError(osLastError())
|
raiseOSError(osLastError())
|
||||||
|
|
||||||
if timeout >= 1000:
|
if timeout >= 1000:
|
||||||
tmspec.tv_sec = (timeout div 1_000).Time
|
tmspec.tv_sec = posix.Time(timeout div 1_000)
|
||||||
tmspec.tv_nsec = (timeout %% 1_000) * 1_000_000
|
tmspec.tv_nsec = (timeout %% 1_000) * 1_000_000
|
||||||
else:
|
else:
|
||||||
tmspec.tv_sec = 0.Time
|
tmspec.tv_sec = posix.Time(0)
|
||||||
tmspec.tv_nsec = (timeout * 1_000_000)
|
tmspec.tv_nsec = (timeout * 1_000_000)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
|
||||||
|
|
@ -190,10 +190,6 @@ when not defined(nimscript):
|
||||||
proc randomize*() {.benign.} =
|
proc randomize*() {.benign.} =
|
||||||
## Initializes the random number generator with a "random"
|
## Initializes the random number generator with a "random"
|
||||||
## number, i.e. a tickcount. Note: Does not work for NimScript.
|
## number, i.e. a tickcount. Note: Does not work for NimScript.
|
||||||
when defined(JS):
|
|
||||||
proc getMil(t: Time): int {.importcpp: "getTime", nodecl.}
|
|
||||||
randomize(getMil times.getTime())
|
|
||||||
else:
|
|
||||||
let time = int64(times.epochTime() * 1_000_000_000)
|
let time = int64(times.epochTime() * 1_000_000_000)
|
||||||
randomize(time)
|
randomize(time)
|
||||||
|
|
||||||
|
|
|
||||||
1611
lib/pure/times.nim
1611
lib/pure/times.nim
File diff suppressed because it is too large
Load diff
|
|
@ -1,4 +1,3 @@
|
||||||
# test times module with js
|
|
||||||
discard """
|
discard """
|
||||||
action: run
|
action: run
|
||||||
"""
|
"""
|
||||||
|
|
@ -9,21 +8,37 @@ import times
|
||||||
# Tue 19 Jan 03:14:07 GMT 2038
|
# Tue 19 Jan 03:14:07 GMT 2038
|
||||||
|
|
||||||
block yeardayTest:
|
block yeardayTest:
|
||||||
# check if yearday attribute is properly set on TimeInfo creation
|
doAssert fromUnix(2147483647).utc.yearday == 18
|
||||||
doAssert fromSeconds(2147483647).getGMTime().yearday == 18
|
|
||||||
|
|
||||||
block localTimezoneTest:
|
block localTime:
|
||||||
# check if timezone is properly set during Time to TimeInfo conversion
|
var local = now()
|
||||||
doAssert fromSeconds(2147483647).getLocalTime().timezone == getTimezone()
|
let utc = local.utc
|
||||||
|
doAssert local.toTime == utc.toTime
|
||||||
|
|
||||||
block timestampPersistenceTest:
|
let a = fromUnix(1_000_000_000)
|
||||||
# check if timestamp persists during TimeInfo to Time conversion
|
let b = fromUnix(1_500_000_000)
|
||||||
const
|
doAssert b - a == 500_000_000
|
||||||
timeString = "2017-03-21T12:34:56+03:00"
|
|
||||||
timeStringGmt = "2017-03-21T09:34:56+00:00"
|
|
||||||
timeStringGmt2 = "2017-03-21T08:34:56+00:00"
|
|
||||||
fmt = "yyyy-MM-dd'T'HH:mm:sszzz"
|
|
||||||
# XXX Check which one is the right solution here:
|
|
||||||
|
|
||||||
let x = $timeString.parse(fmt).toTime().getGMTime()
|
# Because we can't change the timezone JS uses, we define a simple static timezone for testing.
|
||||||
doAssert x == timeStringGmt or x == timeStringGmt2
|
|
||||||
|
proc staticZoneInfoFromUtc(time: Time): ZonedTime =
|
||||||
|
result.utcOffset = -7200
|
||||||
|
result.isDst = false
|
||||||
|
result.adjTime = (time.toUnix + 7200).Time
|
||||||
|
|
||||||
|
proc staticZoneInfoFromTz(adjTime: Time): ZonedTIme =
|
||||||
|
result.utcOffset = -7200
|
||||||
|
result.isDst = false
|
||||||
|
result.adjTime = adjTime
|
||||||
|
|
||||||
|
let utcPlus2 = Timezone(zoneInfoFromUtc: staticZoneInfoFromUtc, zoneInfoFromTz: staticZoneInfoFromTz, name: "")
|
||||||
|
|
||||||
|
block timezoneTests:
|
||||||
|
let dt = initDateTime(01, mJan, 2017, 12, 00, 00, utcPlus2)
|
||||||
|
doAssert $dt == "2017-01-01T12:00:00+02:00"
|
||||||
|
doAssert $dt.utc == "2017-01-01T10:00:00+00:00"
|
||||||
|
doAssert $dt.utc.inZone(utcPlus2) == $dt
|
||||||
|
|
||||||
|
doAssert $initDateTime(01, mJan, 1911, 12, 00, 00, utc()) == "1911-01-01T12:00:00+00:00"
|
||||||
|
# See #6752
|
||||||
|
# doAssert $initDateTime(01, mJan, 1900, 12, 00, 00, utc()) == "0023-01-01T12:00:00+00:00"
|
||||||
|
|
@ -1,16 +1,17 @@
|
||||||
# test the new time module
|
# test the new time module
|
||||||
discard """
|
discard """
|
||||||
file: "ttimes.nim"
|
file: "ttimes.nim"
|
||||||
action: "run"
|
output: '''[Suite] ttimes
|
||||||
|
'''
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import
|
import
|
||||||
times, strutils
|
times, os, strutils, unittest
|
||||||
|
|
||||||
# $ date --date='@2147483647'
|
# $ date --date='@2147483647'
|
||||||
# Tue 19 Jan 03:14:07 GMT 2038
|
# Tue 19 Jan 03:14:07 GMT 2038
|
||||||
|
|
||||||
proc checkFormat(t: TimeInfo, format, expected: string) =
|
proc checkFormat(t: DateTime, format, expected: string) =
|
||||||
let actual = t.format(format)
|
let actual = t.format(format)
|
||||||
if actual != expected:
|
if actual != expected:
|
||||||
echo "Formatting failure!"
|
echo "Formatting failure!"
|
||||||
|
|
@ -18,7 +19,7 @@ proc checkFormat(t: TimeInfo, format, expected: string) =
|
||||||
echo "actual : ", actual
|
echo "actual : ", actual
|
||||||
doAssert false
|
doAssert false
|
||||||
|
|
||||||
let t = getGMTime(fromSeconds(2147483647))
|
let t = fromUnix(2147483647).utc
|
||||||
t.checkFormat("ddd dd MMM hh:mm:ss yyyy", "Tue 19 Jan 03:14:07 2038")
|
t.checkFormat("ddd dd MMM hh:mm:ss yyyy", "Tue 19 Jan 03:14:07 2038")
|
||||||
t.checkFormat("ddd ddMMMhh:mm:ssyyyy", "Tue 19Jan03:14:072038")
|
t.checkFormat("ddd ddMMMhh:mm:ssyyyy", "Tue 19Jan03:14:072038")
|
||||||
t.checkFormat("d dd ddd dddd h hh H HH m mm M MM MMM MMMM s" &
|
t.checkFormat("d dd ddd dddd h hh H HH m mm M MM MMM MMMM s" &
|
||||||
|
|
@ -27,34 +28,115 @@ t.checkFormat("d dd ddd dddd h hh H HH m mm M MM MMM MMMM s" &
|
||||||
|
|
||||||
t.checkFormat("yyyyMMddhhmmss", "20380119031407")
|
t.checkFormat("yyyyMMddhhmmss", "20380119031407")
|
||||||
|
|
||||||
let t2 = getGMTime(fromSeconds(160070789)) # Mon 27 Jan 16:06:29 GMT 1975
|
let t2 = fromUnix(160070789).utc # Mon 27 Jan 16:06:29 GMT 1975
|
||||||
t2.checkFormat("d dd ddd dddd h hh H HH m mm M MM MMM MMMM s" &
|
t2.checkFormat("d dd ddd dddd h hh H HH m mm M MM MMM MMMM s" &
|
||||||
" ss t tt y yy yyy yyyy yyyyy z zz zzz",
|
" ss t tt y yy yyy yyyy yyyyy z zz zzz",
|
||||||
"27 27 Mon Monday 4 04 16 16 6 06 1 01 Jan January 29 29 P PM 5 75 975 1975 01975 +0 +00 +00:00")
|
"27 27 Mon Monday 4 04 16 16 6 06 1 01 Jan January 29 29 P PM 5 75 975 1975 01975 +0 +00 +00:00")
|
||||||
|
|
||||||
var t4 = getGMTime(fromSeconds(876124714)) # Mon 6 Oct 08:58:34 BST 1997
|
var t4 = fromUnix(876124714).utc # Mon 6 Oct 08:58:34 BST 1997
|
||||||
t4.checkFormat("M MM MMM MMMM", "10 10 Oct October")
|
t4.checkFormat("M MM MMM MMMM", "10 10 Oct October")
|
||||||
|
|
||||||
# Interval tests
|
# Interval tests
|
||||||
(t4 - initInterval(years = 2)).checkFormat("yyyy", "1995")
|
(t4 - initInterval(years = 2)).checkFormat("yyyy", "1995")
|
||||||
(t4 - initInterval(years = 7, minutes = 34, seconds = 24)).checkFormat("yyyy mm ss", "1990 24 10")
|
(t4 - initInterval(years = 7, minutes = 34, seconds = 24)).checkFormat("yyyy mm ss", "1990 24 10")
|
||||||
|
|
||||||
proc parseTest(s, f, sExpected: string, ydExpected: int) =
|
# checking dayOfWeek matches known days
|
||||||
|
doAssert getDayOfWeek(01, mJan, 0000) == dSat
|
||||||
|
doAssert getDayOfWeek(01, mJan, -0023) == dSat
|
||||||
|
doAssert getDayOfWeek(21, mSep, 1900) == dFri
|
||||||
|
doAssert getDayOfWeek(01, mJan, 1970) == dThu
|
||||||
|
doAssert getDayOfWeek(21, mSep, 1970) == dMon
|
||||||
|
doAssert getDayOfWeek(01, mJan, 2000) == dSat
|
||||||
|
doAssert getDayOfWeek(01, mJan, 2021) == dFri
|
||||||
|
|
||||||
|
# toUnix tests with GM timezone
|
||||||
|
let t4L = fromUnix(876124714).utc
|
||||||
|
doAssert toUnix(toTime(t4L)) == 876124714
|
||||||
|
doAssert toUnix(toTime(t4L)) + t4L.utcOffset == toUnix(toTime(t4))
|
||||||
|
|
||||||
|
# adding intervals
|
||||||
|
var
|
||||||
|
a1L = toUnix(toTime(t4L + initInterval(hours = 1))) + t4L.utcOffset
|
||||||
|
a1G = toUnix(toTime(t4)) + 60 * 60
|
||||||
|
doAssert a1L == a1G
|
||||||
|
|
||||||
|
# subtracting intervals
|
||||||
|
a1L = toUnix(toTime(t4L - initInterval(hours = 1))) + t4L.utcOffset
|
||||||
|
a1G = toUnix(toTime(t4)) - (60 * 60)
|
||||||
|
doAssert a1L == a1G
|
||||||
|
|
||||||
|
# add/subtract TimeIntervals and Time/TimeInfo
|
||||||
|
doAssert getTime() - 1.seconds == getTime() - 3.seconds + 2.seconds
|
||||||
|
doAssert getTime() + 65.seconds == getTime() + 1.minutes + 5.seconds
|
||||||
|
doAssert getTime() + 60.minutes == getTime() + 1.hours
|
||||||
|
doAssert getTime() + 24.hours == getTime() + 1.days
|
||||||
|
doAssert getTime() + 13.months == getTime() + 1.years + 1.months
|
||||||
|
var
|
||||||
|
ti1 = getTime() + 1.years
|
||||||
|
ti1 -= 1.years
|
||||||
|
doAssert ti1 == getTime()
|
||||||
|
ti1 += 1.days
|
||||||
|
doAssert ti1 == getTime() + 1.days
|
||||||
|
|
||||||
|
# Bug with adding a day to a Time
|
||||||
|
let day = 24.hours
|
||||||
|
let tomorrow = getTime() + day
|
||||||
|
doAssert tomorrow - getTime() == 60*60*24
|
||||||
|
|
||||||
|
# Comparison between Time objects should be detected by compiler
|
||||||
|
# as 'noSideEffect'.
|
||||||
|
proc cmpTimeNoSideEffect(t1: Time, t2: Time): bool {.noSideEffect.} =
|
||||||
|
result = t1 == t2
|
||||||
|
doAssert cmpTimeNoSideEffect(0.fromUnix, 0.fromUnix)
|
||||||
|
# Additionally `==` generic for seq[T] has explicit 'noSideEffect' pragma
|
||||||
|
# so we can check above condition by comparing seq[Time] sequences
|
||||||
|
let seqA: seq[Time] = @[]
|
||||||
|
let seqB: seq[Time] = @[]
|
||||||
|
doAssert seqA == seqB
|
||||||
|
|
||||||
|
for tz in [
|
||||||
|
(0, "+0", "+00", "+00:00"), # UTC
|
||||||
|
(-3600, "+1", "+01", "+01:00"), # CET
|
||||||
|
(-39600, "+11", "+11", "+11:00"), # two digits
|
||||||
|
(-1800, "+0", "+00", "+00:30"), # half an hour
|
||||||
|
(7200, "-2", "-02", "-02:00"), # positive
|
||||||
|
(38700, "-10", "-10", "-10:45")]: # positive with three quaters hour
|
||||||
|
let ti = DateTime(month: mJan, monthday: 1, utcOffset: tz[0])
|
||||||
|
doAssert ti.format("z") == tz[1]
|
||||||
|
doAssert ti.format("zz") == tz[2]
|
||||||
|
doAssert ti.format("zzz") == tz[3]
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
block timezoneConversion:
|
||||||
|
var l = now()
|
||||||
|
let u = l.utc
|
||||||
|
l = u.local
|
||||||
|
|
||||||
|
doAssert l.timezone == local()
|
||||||
|
doAssert u.timezone == utc()
|
||||||
|
|
||||||
|
template parseTest(s, f, sExpected: string, ydExpected: int) =
|
||||||
let
|
let
|
||||||
parsed = s.parse(f)
|
parsed = s.parse(f, utc())
|
||||||
parsedStr = $getGMTime(toTime(parsed))
|
parsedStr = $parsed
|
||||||
if parsedStr != sExpected:
|
check parsedStr == sExpected
|
||||||
echo "Parsing failure!"
|
if parsed.yearday != ydExpected:
|
||||||
echo "expected: ", sExpected
|
echo s
|
||||||
echo "actual : ", parsedStr
|
echo parsed.repr
|
||||||
doAssert false
|
echo parsed.yearday, " exp: ", ydExpected
|
||||||
doAssert(parsed.yearday == ydExpected)
|
check(parsed.yearday == ydExpected)
|
||||||
proc parseTestTimeOnly(s, f, sExpected: string) =
|
|
||||||
doAssert(sExpected in $s.parse(f))
|
template parseTestTimeOnly(s, f, sExpected: string) =
|
||||||
|
check sExpected in $s.parse(f, utc())
|
||||||
|
|
||||||
# because setting a specific timezone for testing is platform-specific, we use
|
# because setting a specific timezone for testing is platform-specific, we use
|
||||||
# explicit timezone offsets in all tests.
|
# explicit timezone offsets in all tests.
|
||||||
|
template runTimezoneTests() =
|
||||||
parseTest("Tuesday at 09:04am on Dec 15, 2015 +0",
|
parseTest("Tuesday at 09:04am on Dec 15, 2015 +0",
|
||||||
"dddd at hh:mmtt on MMM d, yyyy z", "2015-12-15T09:04:00+00:00", 348)
|
"dddd at hh:mmtt on MMM d, yyyy z", "2015-12-15T09:04:00+00:00", 348)
|
||||||
# ANSIC = "Mon Jan _2 15:04:05 2006"
|
# ANSIC = "Mon Jan _2 15:04:05 2006"
|
||||||
|
|
@ -102,65 +184,6 @@ parseTestTimeOnly("3:04PM", "h:mmtt", "15:04:00")
|
||||||
# var tint = timeToTimeInterval(getTime())
|
# var tint = timeToTimeInterval(getTime())
|
||||||
# echo "Todays date after decoding to interval: ", tint
|
# echo "Todays date after decoding to interval: ", tint
|
||||||
|
|
||||||
# checking dayOfWeek matches known days
|
|
||||||
doAssert getDayOfWeek(21, 9, 1900) == dFri
|
|
||||||
doAssert getDayOfWeek(1, 1, 1970) == dThu
|
|
||||||
doAssert getDayOfWeek(21, 9, 1970) == dMon
|
|
||||||
doAssert getDayOfWeek(1, 1, 2000) == dSat
|
|
||||||
doAssert getDayOfWeek(1, 1, 2021) == dFri
|
|
||||||
# Julian tests
|
|
||||||
doAssert getDayOfWeekJulian(21, 9, 1900) == dFri
|
|
||||||
doAssert getDayOfWeekJulian(21, 9, 1970) == dMon
|
|
||||||
doAssert getDayOfWeekJulian(1, 1, 2000) == dSat
|
|
||||||
doAssert getDayOfWeekJulian(1, 1, 2021) == dFri
|
|
||||||
|
|
||||||
# toSeconds tests with GM timezone
|
|
||||||
let t4L = getGMTime(fromSeconds(876124714))
|
|
||||||
doAssert toSeconds(toTime(t4L)) == 876124714
|
|
||||||
doAssert toSeconds(toTime(t4L)) + t4L.timezone.float == toSeconds(toTime(t4))
|
|
||||||
|
|
||||||
# adding intervals
|
|
||||||
var
|
|
||||||
a1L = toSeconds(toTime(t4L + initInterval(hours = 1))) + t4L.timezone.float
|
|
||||||
a1G = toSeconds(toTime(t4)) + 60.0 * 60.0
|
|
||||||
doAssert a1L == a1G
|
|
||||||
|
|
||||||
# subtracting intervals
|
|
||||||
a1L = toSeconds(toTime(t4L - initInterval(hours = 1))) + t4L.timezone.float
|
|
||||||
a1G = toSeconds(toTime(t4)) - (60.0 * 60.0)
|
|
||||||
doAssert a1L == a1G
|
|
||||||
|
|
||||||
# add/subtract TimeIntervals and Time/TimeInfo
|
|
||||||
doAssert getTime() - 1.seconds == getTime() - 3.seconds + 2.seconds
|
|
||||||
doAssert getTime() + 65.seconds == getTime() + 1.minutes + 5.seconds
|
|
||||||
doAssert getTime() + 60.minutes == getTime() + 1.hours
|
|
||||||
doAssert getTime() + 24.hours == getTime() + 1.days
|
|
||||||
doAssert getTime() + 13.months == getTime() + 1.years + 1.months
|
|
||||||
var
|
|
||||||
ti1 = getTime() + 1.years
|
|
||||||
ti1 -= 1.years
|
|
||||||
doAssert ti1 == getTime()
|
|
||||||
ti1 += 1.days
|
|
||||||
doAssert ti1 == getTime() + 1.days
|
|
||||||
|
|
||||||
# overflow of TimeIntervals on initalisation
|
|
||||||
doAssert initInterval(milliseconds = 25000) == initInterval(seconds = 25)
|
|
||||||
doAssert initInterval(seconds = 65) == initInterval(seconds = 5, minutes = 1)
|
|
||||||
doAssert initInterval(hours = 25) == initInterval(hours = 1, days = 1)
|
|
||||||
doAssert initInterval(months = 13) == initInterval(months = 1, years = 1)
|
|
||||||
|
|
||||||
# Bug with adding a day to a Time
|
|
||||||
let day = 24.hours
|
|
||||||
let tomorrow = getTime() + day
|
|
||||||
doAssert tomorrow - getTime() == 60*60*24
|
|
||||||
|
|
||||||
doAssert milliseconds(1000 * 60) == minutes(1)
|
|
||||||
doAssert milliseconds(1000 * 60 * 60) == hours(1)
|
|
||||||
doAssert milliseconds(1000 * 60 * 60 * 24) == days(1)
|
|
||||||
doAssert seconds(60 * 60) == hours(1)
|
|
||||||
doAssert seconds(60 * 60 * 24) == days(1)
|
|
||||||
doAssert seconds(60 * 60 + 65) == (hours(1) + minutes(1) + seconds(5))
|
|
||||||
|
|
||||||
# Bug with parse not setting DST properly if the current local DST differs from
|
# Bug with parse not setting DST properly if the current local DST differs from
|
||||||
# the date being parsed. Need to test parse dates both in and out of DST. We
|
# the date being parsed. Need to test parse dates both in and out of DST. We
|
||||||
# are testing that be relying on the fact that tranforming a TimeInfo to a Time
|
# are testing that be relying on the fact that tranforming a TimeInfo to a Time
|
||||||
|
|
@ -168,76 +191,107 @@ doAssert seconds(60 * 60 + 65) == (hours(1) + minutes(1) + seconds(5))
|
||||||
# behavior this will introduce a one hour offset from the named time and the
|
# behavior this will introduce a one hour offset from the named time and the
|
||||||
# parsed time if the DST value differs between the current time and the date we
|
# parsed time if the DST value differs between the current time and the date we
|
||||||
# are parsing.
|
# are parsing.
|
||||||
#
|
|
||||||
# Unfortunately these tests depend on the locale of the system in which they
|
|
||||||
# are run. They will not be meaningful when run in a locale without DST. They
|
|
||||||
# also assume that Jan. 1 and Jun. 1 will have differing isDST values.
|
|
||||||
let dstT1 = parse("2016-01-01 00:00:00", "yyyy-MM-dd HH:mm:ss")
|
let dstT1 = parse("2016-01-01 00:00:00", "yyyy-MM-dd HH:mm:ss")
|
||||||
let dstT2 = parse("2016-06-01 00:00:00", "yyyy-MM-dd HH:mm:ss")
|
let dstT2 = parse("2016-06-01 00:00:00", "yyyy-MM-dd HH:mm:ss")
|
||||||
doAssert dstT1 == getLocalTime(toTime(dstT1))
|
check dstT1 == toTime(dstT1).local
|
||||||
doAssert dstT2 == getLocalTime(toTime(dstT2))
|
check dstT2 == toTime(dstT2).local
|
||||||
|
|
||||||
# Comparison between Time objects should be detected by compiler
|
|
||||||
# as 'noSideEffect'.
|
|
||||||
proc cmpTimeNoSideEffect(t1: Time, t2: Time): bool {.noSideEffect.} =
|
|
||||||
result = t1 == t2
|
|
||||||
doAssert cmpTimeNoSideEffect(0.fromSeconds, 0.fromSeconds)
|
|
||||||
# Additionally `==` generic for seq[T] has explicit 'noSideEffect' pragma
|
|
||||||
# so we can check above condition by comparing seq[Time] sequences
|
|
||||||
let seqA: seq[Time] = @[]
|
|
||||||
let seqB: seq[Time] = @[]
|
|
||||||
doAssert seqA == seqB
|
|
||||||
|
|
||||||
for tz in [
|
|
||||||
(0, "+0", "+00", "+00:00"), # UTC
|
|
||||||
(-3600, "+1", "+01", "+01:00"), # CET
|
|
||||||
(-39600, "+11", "+11", "+11:00"), # two digits
|
|
||||||
(-1800, "+0", "+00", "+00:30"), # half an hour
|
|
||||||
(7200, "-2", "-02", "-02:00"), # positive
|
|
||||||
(38700, "-10", "-10", "-10:45")]: # positive with three quaters hour
|
|
||||||
let ti = TimeInfo(monthday: 1, timezone: tz[0])
|
|
||||||
doAssert ti.format("z") == tz[1]
|
|
||||||
doAssert ti.format("zz") == tz[2]
|
|
||||||
doAssert ti.format("zzz") == tz[3]
|
|
||||||
|
|
||||||
block formatDst:
|
|
||||||
var ti = TimeInfo(monthday: 1, isDst: true)
|
|
||||||
|
|
||||||
# BST
|
|
||||||
ti.timezone = 0
|
|
||||||
doAssert ti.format("z") == "+1"
|
|
||||||
doAssert ti.format("zz") == "+01"
|
|
||||||
doAssert ti.format("zzz") == "+01:00"
|
|
||||||
|
|
||||||
# EDT
|
|
||||||
ti.timezone = 5 * 60 * 60
|
|
||||||
doAssert ti.format("z") == "-4"
|
|
||||||
doAssert ti.format("zz") == "-04"
|
|
||||||
doAssert ti.format("zzz") == "-04:00"
|
|
||||||
|
|
||||||
block dstTest:
|
block dstTest:
|
||||||
let nonDst = TimeInfo(year: 2015, month: mJan, monthday: 01, yearday: 0,
|
|
||||||
weekday: dThu, hour: 00, minute: 00, second: 00, isDST: false, timezone: 0)
|
|
||||||
var dst = nonDst
|
|
||||||
dst.isDst = true
|
|
||||||
# note that both isDST == true and isDST == false are valid here because
|
|
||||||
# DST is in effect on January 1st in some southern parts of Australia.
|
|
||||||
# FIXME: Fails in UTC
|
|
||||||
# doAssert nonDst.toTime() - dst.toTime() == 3600
|
|
||||||
doAssert nonDst.format("z") == "+0"
|
|
||||||
doAssert dst.format("z") == "+1"
|
|
||||||
|
|
||||||
# parsing will set isDST in relation to the local time. We take a date in
|
# parsing will set isDST in relation to the local time. We take a date in
|
||||||
# January and one in July to maximize the probability to hit one date with DST
|
# January and one in July to maximize the probability to hit one date with DST
|
||||||
# and one without on the local machine. However, this is not guaranteed.
|
# and one without on the local machine. However, this is not guaranteed.
|
||||||
let
|
let
|
||||||
parsedJan = parse("2016-01-05 04:00:00+01:00", "yyyy-MM-dd HH:mm:sszzz")
|
parsedJan = parse("2016-01-05 04:00:00+01:00", "yyyy-MM-dd HH:mm:sszzz")
|
||||||
parsedJul = parse("2016-07-01 04:00:00+01:00", "yyyy-MM-dd HH:mm:sszzz")
|
parsedJul = parse("2016-07-01 04:00:00+01:00", "yyyy-MM-dd HH:mm:sszzz")
|
||||||
doAssert toTime(parsedJan) == fromSeconds(1451962800)
|
doAssert toTime(parsedJan) == fromUnix(1451962800)
|
||||||
doAssert toTime(parsedJul) == fromSeconds(1467342000)
|
doAssert toTime(parsedJul) == fromUnix(1467342000)
|
||||||
|
|
||||||
block countLeapYears:
|
suite "ttimes":
|
||||||
# 1920, 2004 and 2020 are leap years, and should be counted starting at the following year
|
|
||||||
doAssert countLeapYears(1920) + 1 == countLeapYears(1921)
|
# Generate tests for multiple timezone files where available
|
||||||
doAssert countLeapYears(2004) + 1 == countLeapYears(2005)
|
# Set the TZ env var for each test
|
||||||
doAssert countLeapYears(2020) + 1 == countLeapYears(2021)
|
when defined(Linux) or defined(macosx):
|
||||||
|
const tz_dir = "/usr/share/zoneinfo"
|
||||||
|
const f = "yyyy-MM-dd HH:mm zzz"
|
||||||
|
|
||||||
|
let orig_tz = getEnv("TZ")
|
||||||
|
var tz_cnt = 0
|
||||||
|
for tz_fn in walkFiles(tz_dir & "/*"):
|
||||||
|
if symlinkExists(tz_fn) or tz_fn.endsWith(".tab") or
|
||||||
|
tz_fn.endsWith(".list"):
|
||||||
|
continue
|
||||||
|
|
||||||
|
test "test for " & tz_fn:
|
||||||
|
tz_cnt.inc
|
||||||
|
putEnv("TZ", tz_fn)
|
||||||
|
runTimezoneTests()
|
||||||
|
|
||||||
|
test "enough timezone files tested":
|
||||||
|
check tz_cnt > 10
|
||||||
|
|
||||||
|
test "dst handling":
|
||||||
|
putEnv("TZ", "Europe/Stockholm")
|
||||||
|
# In case of an impossible time, the time is moved to after the impossible time period
|
||||||
|
check initDateTime(26, mMar, 2017, 02, 30, 00).format(f) == "2017-03-26 03:30 +02:00"
|
||||||
|
# In case of an ambiguous time, the earlier time is choosen
|
||||||
|
check initDateTime(29, mOct, 2017, 02, 00, 00).format(f) == "2017-10-29 02:00 +02:00"
|
||||||
|
# These are just dates on either side of the dst switch
|
||||||
|
check initDateTime(29, mOct, 2017, 01, 00, 00).format(f) == "2017-10-29 01:00 +02:00"
|
||||||
|
check initDateTime(29, mOct, 2017, 01, 00, 00).isDst
|
||||||
|
check initDateTime(29, mOct, 2017, 03, 01, 00).format(f) == "2017-10-29 03:01 +01:00"
|
||||||
|
check (not initDateTime(29, mOct, 2017, 03, 01, 00).isDst)
|
||||||
|
|
||||||
|
check initDateTime(21, mOct, 2017, 01, 00, 00).format(f) == "2017-10-21 01:00 +02:00"
|
||||||
|
|
||||||
|
test "issue #6520":
|
||||||
|
putEnv("TZ", "Europe/Stockholm")
|
||||||
|
var local = fromUnix(1469275200).local
|
||||||
|
var utc = fromUnix(1469275200).utc
|
||||||
|
|
||||||
|
let claimedOffset = local.utcOffset
|
||||||
|
local.utcOffset = 0
|
||||||
|
check claimedOffset == utc.toTime - local.toTime
|
||||||
|
|
||||||
|
test "issue #5704":
|
||||||
|
putEnv("TZ", "Asia/Seoul")
|
||||||
|
let diff = parse("19700101-000000", "yyyyMMdd-hhmmss").toTime - parse("19000101-000000", "yyyyMMdd-hhmmss").toTime
|
||||||
|
check diff == 2208986872
|
||||||
|
|
||||||
|
test "issue #6465":
|
||||||
|
putEnv("TZ", "Europe/Stockholm")
|
||||||
|
let dt = parse("2017-03-25 12:00", "yyyy-MM-dd hh:mm")
|
||||||
|
check $(dt + 1.days) == "2017-03-26T12:00:00+02:00"
|
||||||
|
|
||||||
|
test "datetime before epoch":
|
||||||
|
check $fromUnix(-2147483648).utc == "1901-12-13T20:45:52+00:00"
|
||||||
|
|
||||||
|
test "adding/subtracting time across dst":
|
||||||
|
putenv("TZ", "Europe/Stockholm")
|
||||||
|
|
||||||
|
let dt1 = initDateTime(26, mMar, 2017, 03, 00, 00)
|
||||||
|
check $(dt1 - 1.seconds) == "2017-03-26T01:59:59+01:00"
|
||||||
|
|
||||||
|
var dt2 = initDateTime(29, mOct, 2017, 02, 59, 59)
|
||||||
|
check $(dt2 + 1.seconds) == "2017-10-29T02:00:00+01:00"
|
||||||
|
|
||||||
|
putEnv("TZ", orig_tz)
|
||||||
|
|
||||||
|
else:
|
||||||
|
# not on Linux or macosx: run one parseTest only
|
||||||
|
test "parseTest":
|
||||||
|
runTimezoneTests()
|
||||||
|
|
||||||
|
test "isLeapYear":
|
||||||
|
check isLeapYear(2016)
|
||||||
|
check (not isLeapYear(2015))
|
||||||
|
check isLeapYear(2000)
|
||||||
|
check (not isLeapYear(1900))
|
||||||
|
|
||||||
|
test "subtract months":
|
||||||
|
var dt = initDateTime(1, mFeb, 2017, 00, 00, 00, utc())
|
||||||
|
check $(dt - 1.months) == "2017-01-01T00:00:00+00:00"
|
||||||
|
dt = initDateTime(15, mMar, 2017, 00, 00, 00, utc())
|
||||||
|
check $(dt - 1.months) == "2017-02-15T00:00:00+00:00"
|
||||||
|
dt = initDateTime(31, mMar, 2017, 00, 00, 00, utc())
|
||||||
|
# This happens due to monthday overflow. It's consistent with Phobos.
|
||||||
|
check $(dt - 1.months) == "2017-03-03T00:00:00+00:00"
|
||||||
Loading…
Add table
Add a link
Reference in a new issue