Preserve DateTime microsecond precision

This commit is contained in:
jdb
2026-08-31 17:52:09 -05:00
parent 2301da8143
commit f2f41782d4
5 changed files with 123 additions and 14 deletions
+53
View File
@@ -0,0 +1,53 @@
import std/[times, unittest]
import fiber_orm/util
proc utcDateTime(nanosecond: NanosecondRange): DateTime =
dateTime(
2026, mAug, 26, 14, 32, 10, nanosecond,
utc())
suite "PostgreSQL DateTime conversion":
test "formats DateTime values with microsecond precision":
check dbFormat(utcDateTime(123_457_000)) ==
"2026-08-26T14:32:10.123457Z"
test "parses whole-second, millisecond, and microsecond values":
let cases = [
("2026-08-26 14:32:10+00", 0),
("2026-08-26 14:32:10.123+00", 123_000_000),
("2026-08-26 14:32:10.9007+00", 900_700_000),
("2026-08-26 14:32:10.12345+00", 123_450_000),
("2026-08-26 14:32:10.123457+00", 123_457_000),
]
for (value, expectedNanosecond) in cases:
let parsed = parsePGDatetime(value)
check parsed.nanosecond == expectedNanosecond
check parsed.utc.format("yyyy-MM-dd'T'HH:mm:ss") ==
"2026-08-26T14:32:10"
test "preserves supported separators and timezone representations":
let timezoneCases = [
"2026-08-26 14:32:10+00",
"2026-08-26T14:32:10+00",
"2026-08-26T14:32:10+00:00",
"2026-08-26 14:32:10.123+00",
"2026-08-26T14:32:10.123457+00:00",
"2026-08-26T14:32:10.123457Z",
]
for value in timezoneCases:
check parsePGDatetime(value).utc.format("yyyy-MM-dd'T'HH:mm:ss") ==
"2026-08-26T14:32:10"
let localCases = [
"2026-08-26 14:32:10",
"2026-08-26T14:32:10",
"2026-08-26 14:32:10.1",
"2026-08-26T14:32:10.12",
]
for value in localCases:
check parsePGDatetime(value).format("yyyy-MM-dd'T'HH:mm:ss") ==
"2026-08-26T14:32:10"