api: Update parsing code for dates to handle truncated millisecond values coming from PostreSQL.
This commit is contained in:
parent
690c50754b
commit
9bcf0a8b12
@ -1,11 +1,11 @@
|
|||||||
import json, macros, options, sequtils, strutils, times, timeutils, unicode,
|
import json, macros, options, sequtils, strutils, times, timeutils, unicode,
|
||||||
uuids
|
uuids
|
||||||
|
|
||||||
|
import nre except toSeq
|
||||||
|
|
||||||
const UNDERSCORE_RUNE = "_".toRunes[0]
|
const UNDERSCORE_RUNE = "_".toRunes[0]
|
||||||
const PG_TIMESTAMP_FORMATS = [
|
const PG_TIMESTAMP_FORMATS = [
|
||||||
"yyyy-MM-dd HH:mm:sszz",
|
"yyyy-MM-dd HH:mm:sszz",
|
||||||
"yyyy-MM-dd HH:mm:ss'.'fzz",
|
|
||||||
"yyyy-MM-dd HH:mm:ss'.'ffzz",
|
|
||||||
"yyyy-MM-dd HH:mm:ss'.'fffzz"
|
"yyyy-MM-dd HH:mm:ss'.'fffzz"
|
||||||
]
|
]
|
||||||
|
|
||||||
@ -72,6 +72,19 @@ proc parsePGDatetime*(val: string): DateTime =
|
|||||||
for df in PG_TIMESTAMP_FORMATS:
|
for df in PG_TIMESTAMP_FORMATS:
|
||||||
try: return val.parse(df)
|
try: return val.parse(df)
|
||||||
except: errStr &= "\n" & getCurrentExceptionMsg()
|
except: errStr &= "\n" & getCurrentExceptionMsg()
|
||||||
|
|
||||||
|
# PostgreSQL does not pad the millisecond value in a datetime out to three
|
||||||
|
# decimal points. If this is a value like `2019-09-29 12:00:00.5Z` We need to
|
||||||
|
# manually catch this and pad it out to something like
|
||||||
|
# `2019-09-29 12:00:00.500Z` so that we can parse it.
|
||||||
|
const millisTruncDatePattern = "(\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}\\.)(\\d{1,2})(.*)"
|
||||||
|
let match = val.match(re(millisTruncDatePattern))
|
||||||
|
if match.isSome:
|
||||||
|
let captures = match.get.captures
|
||||||
|
let reformatted = captures[0] & captures[1].alignLeft(3, '0') & captures[2]
|
||||||
|
try: return reformatted.parse(PG_TIMESTAMP_FORMATS[1])
|
||||||
|
except: errStr &= "\n" & getCurrentExceptionMsg()
|
||||||
|
|
||||||
raise newException(ValueError, "Cannot parse PG date. Tried:" & errStr)
|
raise newException(ValueError, "Cannot parse PG date. Tried:" & errStr)
|
||||||
|
|
||||||
proc parseDbArray*(val: string): seq[string] =
|
proc parseDbArray*(val: string): seq[string] =
|
||||||
|
@ -1 +1 @@
|
|||||||
const PM_API_VERSION* = "0.5.0"
|
const PM_API_VERSION* = "0.6.0"
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "personal-measure-web",
|
"name": "personal-measure-web",
|
||||||
"version": "0.5.0",
|
"version": "0.6.0",
|
||||||
"private": true,
|
"private": true,
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"serve": "vue-cli-service serve",
|
"serve": "vue-cli-service serve",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user