Update API to handle the multiple formats of datetime Postgres might use.

This commit is contained in:
2019-05-18 12:18:35 -05:00
parent 4c60c30b7d
commit 5026e3963c
4 changed files with 22 additions and 7 deletions

View File

@ -2,7 +2,10 @@ import json, macros, options, sequtils, strutils, times, timeutils, unicode,
uuids
const UNDERSCORE_RUNE = "_".toRunes[0]
const PG_TIMESTAMP_FORMAT = "yyyy-MM-dd HH:mm:sszz"
const PG_TIMESTAMP_FORMATS = [
"yyyy-MM-dd HH:mm:sszz",
"yyyy-MM-dd HH:mm:ss'.'fffzz"
]
type
MutateClauses* = object
@ -62,6 +65,13 @@ proc dbFormat*[T](item: T): string = return $item
type DbArrayParseState = enum
expectStart, inQuote, inVal, expectEnd
proc parsePGDatetime*(val: string): DateTime =
var errStr = ""
for df in PG_TIMESTAMP_FORMATS:
try: return val.parse(df)
except: errStr &= "\n" & getCurrentExceptionMsg()
raise newException(ValueError, "Cannot parse PG date. Tried:" & errStr)
proc parseDbArray*(val: string): seq[string] =
result = newSeq[string]()
@ -129,7 +139,7 @@ proc createParseStmt*(t, value: NimNode): NimNode =
result = quote do: parseUUID(`value`)
elif t.getType == DateTime.getType:
result = quote do: `value`.parse(PG_TIMESTAMP_FORMAT)
result = quote do: parsePGDatetime(`value`)
elif t.getTypeInst == Option.getType:
let innerType = t.getTypeImpl[2][0][0][1]