there is no hour 0 in am/pm time

This commit is contained in:
skilchen
2018-04-16 17:40:54 +02:00
parent 04df7f147c
commit bf6e82a861
2 changed files with 21 additions and 7 deletions

View File

@@ -1158,12 +1158,16 @@ proc formatToken(dt: DateTime, token: string, buf: var string) =
of "dddd":
buf.add($dt.weekday)
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":
let amerHour = if dt.hour > 12: dt.hour - 12 else: dt.hour
if amerHour < 10:
buf.add('0')
buf.add($amerHour)
if dt.hour == 0:
buf.add("12")
else:
let amerHour = if dt.hour > 12: dt.hour - 12 else: dt.hour
if amerHour < 10:
buf.add('0')
buf.add($amerHour)
of "H":
buf.add($dt.hour)
of "HH":
@@ -1488,11 +1492,15 @@ proc parseToken(dt: var DateTime; token, value: string; j: var int) =
dt.second = value[j..j+1].parseInt()
j += 2
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
j += 1
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
j += 2
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")
# 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
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",