From 2b78727356bcd471fd242b6b3478e2d83331955e Mon Sep 17 00:00:00 2001 From: Jonathan Bernard Date: Mon, 5 Jul 2021 11:24:06 -0500 Subject: [PATCH] Fix for PostgreSQL timestamp with timezone fields. The previous fix for PostgreSQL timestamp fields matched fields with and without timezones, but did not properly parse values from fields that included the timezone. Now we check for the presence of the timezone in the date string and choose a format string to parse it correctly. --- fiber_orm.nimble | 2 +- src/fiber_orm/util.nim | 11 +++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/fiber_orm.nimble b/fiber_orm.nimble index 3fadd3a..ab2ca19 100644 --- a/fiber_orm.nimble +++ b/fiber_orm.nimble @@ -1,6 +1,6 @@ # Package -version = "0.3.1" +version = "0.3.2" author = "Jonathan Bernard" description = "Lightweight Postgres ORM for Nim." license = "GPL-3.0" diff --git a/src/fiber_orm/util.nim b/src/fiber_orm/util.nim index d4b5771..c563a89 100644 --- a/src/fiber_orm/util.nim +++ b/src/fiber_orm/util.nim @@ -90,11 +90,14 @@ proc parsePGDatetime*(val: string): DateTime = if match.isSome: let c = match.get.captures try: - let corrected = c[0] & alignLeft(c[1], 3, '0') & c[2] - return corrected.parse(PG_TIMESTAMP_FORMATS[1]) + if c.toSeq.len == 2: + let corrected = c[0] & alignLeft(c[1], 3, '0') + return corrected.parse(PG_TIMESTAMP_FORMATS[2]) + else: + let corrected = c[0] & alignLeft(c[1], 3, '0') & c[2] + return corrected.parse(PG_TIMESTAMP_FORMATS[3]) except: - errStr &= "\n\t" & PG_TIMESTAMP_FORMATS[1] & - " after padding out milliseconds to full 3-digits" + errStr &= "\n\t" & getCurrentExceptionMsg() raise newException(ValueError, "Cannot parse PG date. Tried:" & errStr)