Further fix for ISO8601 date parsing.
Recognize versions of timestamps with 'T' as the date/time separator. For example, compare: '2021-08-01 23:14:00-05:00' '2021-08-01T23:14:00-05:00' This commit adds support for the second flavor (and it's variations).
This commit is contained in:
parent
2b78727356
commit
0d0ac46af5
@ -1,6 +1,6 @@
|
|||||||
# Package
|
# Package
|
||||||
|
|
||||||
version = "0.3.2"
|
version = "0.3.3"
|
||||||
author = "Jonathan Bernard"
|
author = "Jonathan Bernard"
|
||||||
description = "Lightweight Postgres ORM for Nim."
|
description = "Lightweight Postgres ORM for Nim."
|
||||||
license = "GPL-3.0"
|
license = "GPL-3.0"
|
||||||
|
@ -67,37 +67,38 @@ proc parsePGDatetime*(val: string): DateTime =
|
|||||||
|
|
||||||
const PG_TIMESTAMP_FORMATS = [
|
const PG_TIMESTAMP_FORMATS = [
|
||||||
"yyyy-MM-dd HH:mm:ss",
|
"yyyy-MM-dd HH:mm:ss",
|
||||||
|
"yyyy-MM-dd'T'HH:mm:ss",
|
||||||
"yyyy-MM-dd HH:mm:sszz",
|
"yyyy-MM-dd HH:mm:sszz",
|
||||||
|
"yyyy-MM-dd'T'HH:mm:sszz",
|
||||||
"yyyy-MM-dd HH:mm:ss'.'fff",
|
"yyyy-MM-dd HH:mm:ss'.'fff",
|
||||||
"yyyy-MM-dd HH:mm:ss'.'fffzz"
|
"yyyy-MM-dd'T'HH:mm:ss'.'fff",
|
||||||
|
"yyyy-MM-dd HH:mm:ss'.'fffzz",
|
||||||
|
"yyyy-MM-dd'T'HH:mm:ss'.'fffzz",
|
||||||
|
"yyyy-MM-dd HH:mm:ss'.'fffzzz",
|
||||||
|
"yyyy-MM-dd'T'HH:mm:ss'.'fffzzz",
|
||||||
]
|
]
|
||||||
|
|
||||||
let PG_PARTIAL_FORMAT_REGEX = re"(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.)(\d{1,3})(\S+)?"
|
var correctedVal = val;
|
||||||
|
|
||||||
var errStr = ""
|
|
||||||
|
|
||||||
# Try to parse directly using known format strings.
|
|
||||||
for df in PG_TIMESTAMP_FORMATS:
|
|
||||||
try: return val.parse(df)
|
|
||||||
except: errStr &= "\n\t" & getCurrentExceptionMsg()
|
|
||||||
|
|
||||||
# PostgreSQL will truncate any trailing 0's in the millisecond value leading
|
# PostgreSQL will truncate any trailing 0's in the millisecond value leading
|
||||||
# to values like `2020-01-01 16:42.3+00`. This cannot currently be parsed by
|
# to values like `2020-01-01 16:42.3+00`. This cannot currently be parsed by
|
||||||
# the standard times format as it expects exactly three digits for
|
# the standard times format as it expects exactly three digits for
|
||||||
# millisecond values. So we have to detect this and pad out the millisecond
|
# millisecond values. So we have to detect this and pad out the millisecond
|
||||||
# value to 3 digits.
|
# value to 3 digits.
|
||||||
|
let PG_PARTIAL_FORMAT_REGEX = re"(\d{4}-\d{2}-\d{2}( |'T')\d{2}:\d{2}:\d{2}\.)(\d{1,2})(\S+)?"
|
||||||
let match = val.match(PG_PARTIAL_FORMAT_REGEX)
|
let match = val.match(PG_PARTIAL_FORMAT_REGEX)
|
||||||
|
|
||||||
if match.isSome:
|
if match.isSome:
|
||||||
let c = match.get.captures
|
let c = match.get.captures
|
||||||
try:
|
if c.toSeq.len == 2: correctedVal = c[0] & alignLeft(c[2], 3, '0')
|
||||||
if c.toSeq.len == 2:
|
else: correctedVal = c[0] & alignLeft(c[2], 3, '0') & c[3]
|
||||||
let corrected = c[0] & alignLeft(c[1], 3, '0')
|
|
||||||
return corrected.parse(PG_TIMESTAMP_FORMATS[2])
|
var errStr = ""
|
||||||
else:
|
|
||||||
let corrected = c[0] & alignLeft(c[1], 3, '0') & c[2]
|
# Try to parse directly using known format strings.
|
||||||
return corrected.parse(PG_TIMESTAMP_FORMATS[3])
|
for df in PG_TIMESTAMP_FORMATS:
|
||||||
except:
|
try: return correctedVal.parse(df)
|
||||||
errStr &= "\n\t" & getCurrentExceptionMsg()
|
except: errStr &= "\n\t" & getCurrentExceptionMsg()
|
||||||
|
|
||||||
raise newException(ValueError, "Cannot parse PG date. Tried:" & errStr)
|
raise newException(ValueError, "Cannot parse PG date. Tried:" & errStr)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user