Merge pull request #7624 from skilchen/fix-issue7620

there is no hour 0 in am/pm time (see #7620)
This commit is contained in:
Dominik Picheta 2018-05-03 16:07:27 +01:00 • committed by GitHub
commit f7f69b1abb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 7 deletions

View file

@ -1173,12 +1173,16 @@ proc formatToken(dt: DateTime, token: string, buf: var string) =
of "dddd": of "dddd":
buf.add($dt.weekday) buf.add($dt.weekday)
of "h": of "h":
buf.add($(if dt.hour > 12: dt.hour - 12 else: dt.hour)) if dt.hour == 0: buf.add("12")
else: buf.add($(if dt.hour > 12: dt.hour - 12 else: dt.hour))
of "hh": of "hh":
let amerHour = if dt.hour > 12: dt.hour - 12 else: dt.hour if dt.hour == 0:
if amerHour < 10: buf.add("12")
buf.add('0') else:
buf.add($amerHour) let amerHour = if dt.hour > 12: dt.hour - 12 else: dt.hour
if amerHour < 10:
buf.add('0')
buf.add($amerHour)
of "H": of "H":
buf.add($dt.hour) buf.add($dt.hour)
of "HH": of "HH":
@ -1503,11 +1507,15 @@ proc parseToken(dt: var DateTime; token, value: string; j: var int) =
dt.second = value[j..j+1].parseInt() dt.second = value[j..j+1].parseInt()
j += 2 j += 2
of "t": of "t":
if value[j] == 'P' and dt.hour > 0 and dt.hour < 12: if value[j] == 'A' and dt.hour == 12:
dt.hour = 0
elif value[j] == 'P' and dt.hour > 0 and dt.hour < 12:
dt.hour += 12 dt.hour += 12
j += 1 j += 1
of "tt": of "tt":
if value[j..j+1] == "PM" and dt.hour > 0 and dt.hour < 12: if value[j..j+1] == "AM" and dt.hour == 12:
dt.hour = 0
elif value[j..j+1] == "PM" and dt.hour > 0 and dt.hour < 12:
dt.hour += 12 dt.hour += 12
j += 2 j += 2
of "yy": of "yy":

View file

@ -28,6 +28,12 @@ t.checkFormat("d dd ddd dddd h hh H HH m mm M MM MMM MMMM s" &
t.checkFormat("yyyyMMddhhmmss", "20380119031407") t.checkFormat("yyyyMMddhhmmss", "20380119031407")
# issue 7620
let t7620_am = parse("4/15/2017 12:01:02 AM +0", "M/d/yyyy' 'h:mm:ss' 'tt' 'z", utc())
t7620_am.checkFormat("M/d/yyyy' 'h:mm:ss' 'tt' 'z", "4/15/2017 12:01:02 AM +0")
let t7620_pm = parse("4/15/2017 12:01:02 PM +0", "M/d/yyyy' 'h:mm:ss' 'tt' 'z", utc())
t7620_pm.checkFormat("M/d/yyyy' 'h:mm:ss' 'tt' 'z", "4/15/2017 12:01:02 PM +0")
let t2 = fromUnix(160070789).utc # Mon 27 Jan 16:06:29 GMT 1975 let t2 = fromUnix(160070789).utc # Mon 27 Jan 16:06:29 GMT 1975
t2.checkFormat("d dd ddd dddd h hh H HH m mm M MM MMM MMMM s" & t2.checkFormat("d dd ddd dddd h hh H HH m mm M MM MMM MMMM s" &
" ss t tt y yy yyy yyyy yyyyy z zz zzz", " ss t tt y yy yyy yyyy yyyyy z zz zzz",