45 lines
1.2 KiB
Nim
45 lines
1.2 KiB
Nim
# SPDX-FileCopyrightText: ☭ Emery Hemingway
|
|
# SPDX-License-Identifier: Unlicense
|
|
|
|
import std/times
|
|
import ../preserves
|
|
|
|
const
|
|
label = "rfc3339"
|
|
fullDateFormat = "yyyy-MM-dd"
|
|
partialTimeFormat = "HH:mm:ss"
|
|
fullTimeFormat = "HH:mm:sszzz"
|
|
dateTimeFormat = "yyyy-MM-dd'T'HH:mm:sszzz"
|
|
|
|
proc toPreserveHook*(dt: DateTime; E: typedesc): Preserve[E] =
|
|
initRecord[E](toSymbol("rfc3339", E), toPreserve($dt, E))
|
|
|
|
proc fromPreserveHook*[E](dt: var DateTime; pr: Preserve[E]): bool =
|
|
result = pr.isRecord(label, 1) and pr.record[0].isString
|
|
if result:
|
|
try:
|
|
let
|
|
s = pr.record[0].string
|
|
n = len(s)
|
|
if n == len(fullDateFormat):
|
|
dt = parse(s, fullDateFormat)
|
|
elif n == len(partialTimeFormat):
|
|
dt = parse(s, partialTimeFormat)
|
|
elif len(partialTimeFormat) < n and n <= len(fullTimeFormat):
|
|
dt = parse(s, fullTimeFormat)
|
|
elif len(fullTimeFormat) < n:
|
|
dt = parse(s, dateTimeFormat)
|
|
else:
|
|
result = false
|
|
except ValueError:
|
|
result = false
|
|
|
|
runnableExamples:
|
|
import std/[times, unittest]
|
|
import preserves
|
|
var a, b: DateTime
|
|
a = now()
|
|
var pr = a.toPreserveHook(void)
|
|
check fromPreserveHook(b, pr)
|
|
check $a == $b
|