Skip to content

Commit

Permalink
time, x.json2: improve iso8601 time decoding (#18496)
Browse files Browse the repository at this point in the history
  • Loading branch information
squidink7 committed Jun 21, 2023
1 parent d7a50b4 commit 5006ffb
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion vlib/time/parse.c.v
Expand Up @@ -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_,
&microsecond_, &char(&plus_min_z), &offset_hour, &offset_minute)
}
// Missread microsecond ([Sec Hour Minute].len == 3 < 4)
Expand Down
8 changes: 4 additions & 4 deletions vlib/x/json2/json2.v
Expand Up @@ -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()
Expand Down Expand Up @@ -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`
Expand Down

0 comments on commit 5006ffb

Please sign in to comment.