From 8b6405441a2fdbfa462ba37fc62cb1c7f1845cf2 Mon Sep 17 00:00:00 2001 From: Jonathan Bernard Date: Mon, 3 Jan 2022 08:57:43 -0600 Subject: [PATCH] Support additional date and time formats (particularly YYYY-MM-DD). --- private/models.nim | 13 +++++++++++-- private/version.nim | 2 +- ptk.nimble | 2 +- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/private/models.nim b/private/models.nim index 652d467..6557107 100644 --- a/private/models.nim +++ b/private/models.nim @@ -22,10 +22,18 @@ const ISO_TIME_FORMAT* = "yyyy-MM-dd'T'HH:mm:ss" ## The canonical time format used by PTK. const TIME_FORMATS* = @[ + (fmtStr: "yyyy-MM-dd'T'HH:mm:sszzz", offsetFrom: OffsetFrom.None), + (fmtStr: "yyyy-MM-dd HH:mm:sszzz", offsetFrom: OffsetFrom.None), + (fmtStr: "yyyy-MM-dd'T'HH:mm:sszz", offsetFrom: OffsetFrom.None), + (fmtStr: "yyyy-MM-dd HH:mm:sszz", offsetFrom: OffsetFrom.None), + (fmtStr: "yyyy-MM-dd'T'HH:mm:ssz", offsetFrom: OffsetFrom.None), + (fmtStr: "yyyy-MM-dd HH:mm:ssz", offsetFrom: OffsetFrom.None), (fmtStr: "yyyy-MM-dd'T'HH:mm:ss", offsetFrom: OffsetFrom.None), (fmtStr: "yyyy-MM-dd HH:mm:ss", offsetFrom: OffsetFrom.None), (fmtStr: "yyyy-MM-dd'T'HH:mm", offsetFrom: OffsetFrom.None), (fmtStr: "yyyy-MM-dd HH:mm", offsetFrom: OffsetFrom.None), + (fmtStr: "yyyy-MM-dd", offsetFrom: OffsetFrom.None), + (fmtStr: "yyyy-MM", offsetFrom: OffsetFrom.None), (fmtStr: "MM-dd'T'HH:mm:ss", offsetFrom: OffsetFrom.Year), (fmtStr: "MM-dd HH:mm:ss", offsetFrom: OffsetFrom.Year), (fmtStr: "MM-dd'T'HH:mm", offsetFrom: OffsetFrom.Year), @@ -48,15 +56,16 @@ proc getIfExists*(n: JsonNode, key: string): JsonNode = proc parseTime*(timeStr: string): DateTime = ## Helper to parse time strings trying multiple known formats. + let now = now() + for fmt in TIME_FORMATS: try: - let now = now() let parsed = parse(timeStr, fmt.fmtStr) case fmt.offsetFrom: of OffsetFrom.None: return parsed of OffsetFrom.Year: - return initDateTime(parsed.monthday, parsed.month, now.year, + return dateTime(now.year, parsed.month, parsed.monthday, parsed.hour, parsed.minute, parsed.second, parsed.nanosecond, now.timezone) of OffsetFrom.Month: diff --git a/private/version.nim b/private/version.nim index 8e686b0..dca5a95 100644 --- a/private/version.nim +++ b/private/version.nim @@ -1 +1 @@ -const PTK_VERSION* = "1.0.11" \ No newline at end of file +const PTK_VERSION* = "1.0.12" \ No newline at end of file diff --git a/ptk.nimble b/ptk.nimble index ad1fa79..4f70315 100644 --- a/ptk.nimble +++ b/ptk.nimble @@ -1,6 +1,6 @@ # Package -version = "1.0.11" +version = "1.0.12" author = "Jonathan Bernard" description = "Personal Time Keeper" license = "MIT"