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

x.json: fix parsing of time fields, that have just a single date encoded inside the parsed string (fix #16722)

This commit is contained in:
Delyan Angelov 2022-12-20 16:34:04 +02:00
parent c84eb29b78
commit 3c920f2ee6
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
2 changed files with 10 additions and 0 deletions

View File

@ -79,6 +79,12 @@ fn test_types() {
assert json.decode[StructType[int]]('{"val": false}')!.val == 0
assert json.decode[StructType[time.Time]]('{"val": "2022-03-11T13:54:25.000Z"}')!.val == fixed_time
assert json.decode[StructType[time.Time]]('{"val": "2001-01-05"}')!.val.year == 2001
assert json.decode[StructType[time.Time]]('{"val": "2001-01-05"}')!.val.month == 1
assert json.decode[StructType[time.Time]]('{"val": "2001-01-05"}')!.val.day == 5
assert json.decode[StructType[time.Time]]('{"val": "2001-01-05"}')!.val.hour == 0
assert json.decode[StructType[time.Time]]('{"val": "2001-01-05"}')!.val.minute == 0
assert json.decode[StructType[time.Time]]('{"val": "2001-01-05"}')!.val.second == 0
assert json.decode[StructType[time.Time]]('{"val": "2022-03-11 13:54:25.000"}')!.val == fixed_time
assert json.decode[StructType[time.Time]]('{"val": 1647006865}')!.val == fixed_time
assert json.decode[StructType[time.Time]]('{"val": "1647006865"}')!.val == fixed_time

View File

@ -262,6 +262,10 @@ 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`
return time.parse_iso8601(f)!
}
is_rfc3339 := f.len == 24 && f[23] == `Z` && f[10] == `T`
if is_rfc3339 {
return time.parse_rfc3339(f)!