1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

time, x.json2: improve iso8601 time decoding (#18496)

This commit is contained in:
squidink7 2023-06-21 19:04:50 +09:30 committed by GitHub
parent d7a50b4a22
commit 5006ffb304
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 5 deletions

View File

@ -252,7 +252,7 @@ fn parse_iso8601_time(s string) !(int, int, int, int, i64, bool) {
microsecond_ = nanosecond_ / 1000 microsecond_ = nanosecond_ / 1000
} else { } else {
count = unsafe { 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) &microsecond_, &char(&plus_min_z), &offset_hour, &offset_minute)
} }
// Missread microsecond ([Sec Hour Minute].len == 3 < 4) // Missread microsecond ([Sec Hour Minute].len == 3 < 4)

View File

@ -117,10 +117,10 @@ pub fn decode[T](src string) !T {
typ.$(field.name) = res[json_name]!.str() typ.$(field.name) = res[json_name]!.str()
} }
} $else $if field.typ is time.Time { } $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 { } $else $if field.typ is ?time.Time {
if json_name in res { 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 { } $else $if field.is_array {
// typ.$(field.name) = res[field.name]!.arr() // typ.$(field.name) = res[field.name]!.arr()
@ -393,8 +393,8 @@ pub fn (f Any) to_time() !time.Time {
return time.unix(f) return time.unix(f)
} }
string { string {
if f.len == 10 && f[4] == `-` && f[7] == `-` { is_iso8601 := f[4] == `-` && f[7] == `-`
// just a date in the format `2001-01-01` if is_iso8601 {
return time.parse_iso8601(f)! return time.parse_iso8601(f)!
} }
is_rfc3339 := f.len == 24 && f[23] == `Z` && f[10] == `T` is_rfc3339 := f.len == 24 && f[23] == `Z` && f[10] == `T`