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:
parent
c84eb29b78
commit
3c920f2ee6
@ -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
|
||||
|
@ -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)!
|
||||
|
Loading…
Reference in New Issue
Block a user