refs #4347, add ZZZ and ZZZZ patterns for timezone offsets without colons (#17318)

This commit is contained in:
Miran 2021-03-11 16:32:50 +01:00 • committed by GitHub
commit e922d73dd6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 65 additions and 25 deletions

View file

@ -110,8 +110,12 @@
| `UTC-5 -> -05`
`zzz` Same as above but with `:mm` where *mm* represents minutes. | `UTC+7 -> +07:00`
| `UTC-5 -> -05:00`
`ZZZ` Same as above but with `mm` where *mm* represents minutes. | `UTC+7 -> +0700`
| `UTC-5 -> -0500`
`zzzz` Same as above but with `:ss` where *ss* represents seconds. | `UTC+7 -> +07:00:00`
| `UTC-5 -> -05:00:00`
`ZZZZ` Same as above but with `ss` where *ss* represents seconds. | `UTC+7 -> +070000`
| `UTC-5 -> -050000`
`g` Era: AD or BC | `300 AD -> AD`
| `300 BC -> BC`
`fff` Milliseconds display | `1000000 nanoseconds -> 1`
@ -1469,6 +1473,7 @@ type
uuuu
UUUU
z, zz, zzz, zzzz
ZZZ, ZZZZ
g
# This is a special value used to mark literal format values.
@ -1621,6 +1626,8 @@ proc stringToPattern(str: string): FormatPattern =
of "zz": result = zz
of "zzz": result = zzz
of "zzzz": result = zzzz
of "ZZZ": result = ZZZ
of "ZZZZ": result = ZZZZ
of "g": result = g
else: raise newException(TimeFormatParseError,
"'" & str & "' is not a valid pattern")
@ -1727,7 +1734,7 @@ proc formatPattern(dt: DateTime, pattern: FormatPattern, result: var string,
result.add '+' & $year
of UUUU:
result.add $dt.year
of z, zz, zzz, zzzz:
of z, zz, zzz, zzzz, ZZZ, ZZZZ:
if dt.timezone != nil and dt.timezone.name == "Etc/UTC":
result.add 'Z'
else:
@ -1738,16 +1745,18 @@ proc formatPattern(dt: DateTime, pattern: FormatPattern, result: var string,
result.add $(absOffset div 3600)
of zz:
result.add (absOffset div 3600).intToStr(2)
of zzz:
of zzz, ZZZ:
let h = (absOffset div 3600).intToStr(2)
let m = ((absOffset div 60) mod 60).intToStr(2)
result.add h & ":" & m
of zzzz:
let sep = if pattern == zzz: ":" else: ""
result.add h & sep & m
of zzzz, ZZZZ:
let absOffset = abs(dt.utcOffset)
let h = (absOffset div 3600).intToStr(2)
let m = ((absOffset div 60) mod 60).intToStr(2)
let s = (absOffset mod 60).intToStr(2)
result.add h & ":" & m & ":" & s
let sep = if pattern == zzzz: ":" else: ""
result.add h & sep & m & sep & s
else: assert false
of g:
result.add if dt.year < 1: "BC" else: "AD"
@ -1881,7 +1890,7 @@ proc parsePattern(input: string, pattern: FormatPattern, i: var int,
parsed.year = some(year)
of UUUU:
parsed.year = some(takeInt(1..high(int), allowSign = true))
of z, zz, zzz, zzzz:
of z, zz, zzz, zzzz, ZZZ, ZZZZ:
case input[i]
of '+', '-':
let sign = if input[i] == '-': 1 else: -1
@ -1892,21 +1901,24 @@ proc parsePattern(input: string, pattern: FormatPattern, i: var int,
offset = takeInt(1..2) * 3600
of zz:
offset = takeInt(2..2) * 3600
of zzz:
of zzz, ZZZ:
offset.inc takeInt(2..2) * 3600
if input[i] != ':':
return false
i.inc
if pattern == zzz:
if input[i] != ':':
return false
i.inc
offset.inc takeInt(2..2) * 60
of zzzz:
of zzzz, ZZZZ:
offset.inc takeInt(2..2) * 3600
if input[i] != ':':
return false
i.inc
if pattern == zzzz:
if input[i] != ':':
return false
i.inc
offset.inc takeInt(2..2) * 60
if input[i] != ':':
return false
i.inc
if pattern == zzzz:
if input[i] != ':':
return false
i.inc
offset.inc takeInt(2..2)
else: assert false
parsed.utcOffset = some(offset * sign)