From 5006ffb304617b52773f3e8b301059d17842bfa5 Mon Sep 17 00:00:00 2001 From: squidink7 <56238661+squidink7@users.noreply.github.com> Date: Wed, 21 Jun 2023 19:04:50 +0930 Subject: [PATCH] time, x.json2: improve iso8601 time decoding (#18496) --- vlib/time/parse.c.v | 2 +- vlib/x/json2/json2.v | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/vlib/time/parse.c.v b/vlib/time/parse.c.v index cf197f0ea88f8a..19fdd95f801da3 100644 --- a/vlib/time/parse.c.v +++ b/vlib/time/parse.c.v @@ -252,7 +252,7 @@ fn parse_iso8601_time(s string) !(int, int, int, int, i64, bool) { microsecond_ = nanosecond_ / 1000 } else { count = unsafe { - C.sscanf(&char(s.str), c'%2d:%2d:%2d.%6d%c%2d:%2d', &hour_, &minute_, &second_, + C.sscanf(&char(s.str), c'%2d:%2d:%2d.%9d%c%2d:%2d', &hour_, &minute_, &second_, µsecond_, &char(&plus_min_z), &offset_hour, &offset_minute) } // Missread microsecond ([Sec Hour Minute].len == 3 < 4) diff --git a/vlib/x/json2/json2.v b/vlib/x/json2/json2.v index c035160d2c6554..1030637af08b90 100644 --- a/vlib/x/json2/json2.v +++ b/vlib/x/json2/json2.v @@ -117,10 +117,10 @@ pub fn decode[T](src string) !T { typ.$(field.name) = res[json_name]!.str() } } $else $if field.typ is time.Time { - typ.$(field.name) = res[field.name]!.to_time()! + typ.$(field.name) = res[json_name]!.to_time()! } $else $if field.typ is ?time.Time { if json_name in res { - typ.$(field.name) = res[field.name]!.to_time()! + typ.$(field.name) = res[json_name]!.to_time()! } } $else $if field.is_array { // typ.$(field.name) = res[field.name]!.arr() @@ -393,8 +393,8 @@ pub fn (f Any) to_time() !time.Time { return time.unix(f) } string { - if f.len == 10 && f[4] == `-` && f[7] == `-` { - // just a date in the format `2001-01-01` + is_iso8601 := f[4] == `-` && f[7] == `-` + if is_iso8601 { return time.parse_iso8601(f)! } is_rfc3339 := f.len == 24 && f[23] == `Z` && f[10] == `T`