v/vlib/time/json_decode.c.v
Larsimusrex d249bfb04e
Some checks failed
wasm backend CI / wasm-backend (windows-2022) (push) Has been cancelled
json decoder benchmark CI / json-encode-benchmark (push) Has been cancelled
Graphics CI / gg-regressions (push) Has been cancelled
vlib modules CI / build-module-docs (push) Has been cancelled
native backend CI / native-backend-ubuntu (push) Has been cancelled
native backend CI / native-backend-windows (push) Has been cancelled
Shy and PV CI / v-compiles-puzzle-vibes (push) Has been cancelled
Sanitized CI / sanitize-undefined-clang (push) Has been cancelled
Sanitized CI / sanitize-undefined-gcc (push) Has been cancelled
Sanitized CI / tests-sanitize-address-clang (push) Has been cancelled
Sanitized CI / sanitize-address-msvc (push) Has been cancelled
Sanitized CI / sanitize-address-gcc (push) Has been cancelled
Sanitized CI / sanitize-memory-clang (push) Has been cancelled
sdl CI / v-compiles-sdl-examples (push) Has been cancelled
Time CI / time-linux (push) Has been cancelled
Time CI / time-macos (push) Has been cancelled
Time CI / time-windows (push) Has been cancelled
toml CI / toml-module-pass-external-test-suites (push) Has been cancelled
Tools CI / tools-linux (clang) (push) Has been cancelled
Tools CI / tools-linux (gcc) (push) Has been cancelled
Tools CI / tools-linux (tcc) (push) Has been cancelled
Tools CI / tools-macos (clang) (push) Has been cancelled
Tools CI / tools-windows (gcc) (push) Has been cancelled
Tools CI / tools-windows (msvc) (push) Has been cancelled
Tools CI / tools-windows (tcc) (push) Has been cancelled
Tools CI / tools-docker-ubuntu-musl (push) Has been cancelled
vab CI / vab-compiles-v-examples (push) Has been cancelled
vab CI / v-compiles-os-android (push) Has been cancelled
wasm backend CI / wasm-backend (ubuntu-22.04) (push) Has been cancelled
decoder2: fix remaining json2 discrepancies (#25029)
2025-08-03 10:27:43 +03:00

36 lines
876 B
V

module time
// from_json_string implements a custom decoder for json2 (unix)
pub fn (mut t Time) from_json_number(raw_number string) ! {
t = unix(raw_number.i64())
}
// from_json_string implements a custom decoder for json2 (iso8601/rfc3339/unix)
pub fn (mut t Time) from_json_string(raw_string string) ! {
is_iso8601 := raw_string[4] == `-` && raw_string[7] == `-`
if is_iso8601 {
t = parse_iso8601(raw_string)!
return
}
is_rfc3339 := raw_string.len == 24 && raw_string[23] == `Z` && raw_string[10] == `T`
if is_rfc3339 {
t = parse_rfc3339(raw_string)!
return
}
mut is_unix_timestamp := true
for c in raw_string {
if c == `-` || (c >= `0` && c <= `9`) {
continue
}
is_unix_timestamp = false
break
}
if is_unix_timestamp {
t = unix(raw_string.i64())
return
}
return error('Expected iso8601/rfc3339/unix time but got: ${raw_string}')
}