75 lines
1.9 KiB
Nim
75 lines
1.9 KiB
Nim
import times
|
|
|
|
const zeroTime = fromUnix(0)
|
|
const ISO_8601_FORMATS = @[
|
|
"yyyy-MM-dd",
|
|
"yyyy-MM-dd'T'HH:mm:ssz",
|
|
"yyyy-MM-dd'T'HH:mm:sszzz",
|
|
"yyyy-MM-dd'T'HH:mm:ss'.'fffzzz",
|
|
"yyyy-MM-dd HH:mm:ssz",
|
|
"yyyy-MM-dd HH:mm:sszzz",
|
|
"yyyy-MM-dd HH:mm:ss'.'fffzzz"
|
|
]
|
|
|
|
proc format*(ti: TimeInterval, fmt: string): string =
|
|
let info = (zeroTime + ti).utc
|
|
return info.format(fmt)
|
|
|
|
proc format*(d: Duration, fmt: string): string =
|
|
let info = (fromUnix(0) + d).utc
|
|
return info.format(fmt)
|
|
|
|
proc `<`*(a, b: TimeInterval): bool =
|
|
return (zeroTime + a) < (zeroTime + b)
|
|
|
|
proc `<=`*(a, b: TimeInterval): bool =
|
|
return (zeroTime + a) <= (zeroTime + b)
|
|
|
|
proc between*(a, s, e: DateTime): bool =
|
|
return times.`<=`(s, a) and times.`<`(a, e)
|
|
|
|
proc cmp*(a, b: DateTime): int =
|
|
if b == a: return 0
|
|
elif times.`<`(a, b): return -1
|
|
else: return 1
|
|
|
|
proc startOfDay*(dt: DateTime): DateTime =
|
|
result = initDateTime(
|
|
dt.monthday,
|
|
dt.month,
|
|
dt.year,
|
|
0, 0, 0, 0, # hour, minute, second, nanosecond
|
|
dt.timezone)
|
|
|
|
proc trimNanoSec*(dt: DateTime): DateTime =
|
|
result = dt
|
|
result = initDateTime(
|
|
dt.monthday,
|
|
dt.month,
|
|
dt.year,
|
|
dt.hour,
|
|
dt.minute,
|
|
dt.second,
|
|
0,
|
|
dt.timezone)
|
|
|
|
proc startOfWeek*(ti: DateTime, startDay = dMon): DateTime =
|
|
var diff = (ti.weekday.ord - startDay.ord)
|
|
if diff < 0: diff += 7
|
|
return (ti - diff.days).startOfDay
|
|
|
|
proc parseIso8601*(val: string): DateTime =
|
|
var errString = ""
|
|
for df in ISO_8601_FORMATS:
|
|
try: return val.parse(df)
|
|
except: errString &= "\n" & getCurrentExceptionMsg()
|
|
raise newException(Exception, "Could not parse date. Tried:" & errString)
|
|
|
|
proc formatIso8601*(d: DateTime): string =
|
|
return d.format(ISO_8601_FORMATS[2])
|
|
|
|
# This is a workaround needed due a bug in Nim's times.parse procedure.
|
|
# see: https://github.com/nim-lang/Nim/issues/4922
|
|
template fixedParse*(value, format: string): DateTime {.deprecated.} =
|
|
parse(value, format)
|