Support additional date and time formats (particularly YYYY-MM-DD).
This commit is contained in:
parent
aff927b4f4
commit
8b6405441a
@ -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:
|
||||
|
@ -1 +1 @@
|
||||
const PTK_VERSION* = "1.0.11"
|
||||
const PTK_VERSION* = "1.0.12"
|
@ -1,6 +1,6 @@
|
||||
# Package
|
||||
|
||||
version = "1.0.11"
|
||||
version = "1.0.12"
|
||||
author = "Jonathan Bernard"
|
||||
description = "Personal Time Keeper"
|
||||
license = "MIT"
|
||||
|
Loading…
x
Reference in New Issue
Block a user