2 Commits
0.4.0 ... 0.5.1

Author SHA1 Message Date
08c0962b40 Add addtional valid ISO8601 formats. 2019-12-27 10:07:04 -06:00
a537b980e6 Expand allowed ISO8601 parsing formats. 2019-04-27 23:15:14 -05:00
2 changed files with 17 additions and 6 deletions

View File

@ -1,10 +1,17 @@
import times
const zeroTime = fromUnix(0)
const ISO_8601_FULL_FORMAT = "yyyy-MM-dd'T'HH:mm:sszzz"
const ISO_8601_FORMATS = @[
"yyyy-MM-dd'T'HH:mm:ssz",
"yyyy-MM-dd'T'HH:mm:sszzz",
"yyyy-MM-dd'T'HH:mm:ss'.'fffzzz",
"yyyy-MM-dd HH:mm:ssz",
"yyyy-MM-dd HH:mm:sszzz",
"yyyy-MM-dd HH:mm:ss'.'fffzzz"
]
proc format*(ti: TimeInterval, fmt: string): string =
let info = (fromUnix(0) + ti).utc
let info = (zeroTime + ti).utc
return info.format(fmt)
# Will be deprecated in Nim 0.18.1 as it will exist in the standard times module.
@ -48,10 +55,14 @@ proc startOfWeek*(ti: DateTime, startDay = dMon): DateTime =
return (ti - diff.days).startOfDay
proc parseIso8601*(val: string): DateTime =
return val.parse(ISO_8601_FULL_FORMAT)
var errString = ""
for df in ISO_8601_FORMATS:
try: return val.parse(df)
except: errString &= "\n" & getCurrentExceptionMsg()
raise newException(Exception, "Could not parse date. Tried:" & errString)
proc formatIso8601*(d: DateTime): string =
return d.format(ISO_8601_FULL_FORMAT)
return d.format(ISO_8601_FORMATS[2])
# This is a workaround needed due a bug in Nim's times.parse procedure.
# see: https://github.com/nim-lang/Nim/issues/4922

View File

@ -1,8 +1,8 @@
# Package
version = "0.4.0"
version = "0.5.1"
author = "Jonathan Bernard"
description = "Utility methods to fill in the lacking time support in Nim\'s stdlib. This is holding me over until I can write a proper time module for the stdlib and submit it."
description = "Utility methods to fill in the lacking time support in Nim\'s stdlib."
license = "BSD3"
# Dependencies