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

time: do not panic for time.parse("2020-02-02 20.02.20")!, just return an error instead (fix #16779)

This commit is contained in:
Delyan Angelov 2022-12-27 12:58:43 +02:00
parent 6b3f8f519d
commit 508bfbf892
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
2 changed files with 13 additions and 7 deletions

View File

@ -70,6 +70,9 @@ pub fn parse(s string) !Time {
}
shms := s[pos..]
hms := shms.split(':')
if hms.len != 3 {
return error_invalid_time(9)
}
hour_ := hms[0][1..]
minute_ := hms[1]
second_ := hms[2]

View File

@ -13,12 +13,15 @@ fn test_parse() {
}
fn test_parse_invalid() {
s := 'Invalid time string'
time.parse(s) or {
assert true
return
if x := time.parse('Invalid time string') {
assert false
}
assert false
assert true
if x := time.parse('2020-02-02 02.20.02') {
assert false
}
assert true
}
fn test_parse_rfc2822() {
@ -186,14 +189,14 @@ fn test_parse_rfc3339() {
}
fn test_ad_second_to_parse_result_in_2001() {
now_tm := time.parse('2001-01-01 04:00:00')?
now_tm := time.parse('2001-01-01 04:00:00')!
future_tm := now_tm.add_seconds(60)
assert future_tm.str() == '2001-01-01 04:01:00'
assert now_tm.unix < future_tm.unix
}
fn test_ad_second_to_parse_result_pre_2001() {
now_tm := time.parse('2000-01-01 04:00:00')?
now_tm := time.parse('2000-01-01 04:00:00')!
future_tm := now_tm.add_seconds(60)
assert future_tm.str() == '2000-01-01 04:01:00'
assert now_tm.unix < future_tm.unix