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

time: return optional value in parse and parse_iso functions

This commit is contained in:
Alexey 2020-02-06 12:22:00 +03:00 committed by GitHub
parent 1618596218
commit 91e181b14a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 18 deletions

View File

@ -248,8 +248,12 @@ fn (gen_vc mut GenVC) generate() {
ts_vc := git_log_vc.find_between('Date:', '\n').trim_space() ts_vc := git_log_vc.find_between('Date:', '\n').trim_space()
// parse time as string to time.Time // parse time as string to time.Time
last_commit_time_v := time.parse(ts_v) last_commit_time_v := time.parse(ts_v) or {
last_commit_time_vc := time.parse(ts_vc) panic(err)
}
last_commit_time_vc := time.parse(ts_vc) or {
panic(err)
}
// git dates are in users local timezone and v time.parse does not parse // git dates are in users local timezone and v time.parse does not parse
// timezones at the moment, so for now get unix timestamp from output also // timezones at the moment, so for now get unix timestamp from output also

View File

@ -177,24 +177,21 @@ pub fn (t Time) clean12() string {
// return fmt.Sprintf("%4d/%02d/%02d", t.Year(), t.Month(), t.Day()) + " " + hm // return fmt.Sprintf("%4d/%02d/%02d", t.Year(), t.Month(), t.Day()) + " " + hm
} }
// `parse` parses time in the following format: "2018-01-27 12:48:34" // `parse` parses time in the following format: "2018-01-27 12:48:34"
pub fn parse(s string) Time { pub fn parse(s string) ?Time {
// println('parse="$s"')
pos := s.index(' ') or { pos := s.index(' ') or {
println('bad time format') return error('Invalid time format: $s')
return now()
} }
symd := s[..pos] symd := s[..pos]
ymd := symd.split('-') ymd := symd.split('-')
if ymd.len != 3 { if ymd.len != 3 {
println('bad time format') return error('Invalid time format: $s')
return now()
} }
shms := s[pos..] shms := s[pos..]
hms := shms.split(':') hms := shms.split(':')
hour := hms[0][1..] hour := hms[0][1..]
minute := hms[1] minute := hms[1]
second := hms[2] second := hms[2]
// //////////
return new_time(Time{ return new_time(Time{
year: ymd[0].int() year: ymd[0].int()
month: ymd[1].int() month: ymd[1].int()
@ -206,19 +203,25 @@ pub fn parse(s string) Time {
} }
// `parse_iso` parses time in the following format: "Thu, 12 Dec 2019 06:07:45 GMT" // `parse_iso` parses time in the following format: "Thu, 12 Dec 2019 06:07:45 GMT"
pub fn parse_iso(s string) Time { pub fn parse_iso(s string) ?Time {
fields := s.split(' ') fields := s.split(' ')
if fields.len < 5 { if fields.len < 5 {
return Time{} return error('Invalid time format: $s')
} }
pos := months_string.index(fields[2]) or { pos := months_string.index(fields[2]) or {
return Time{} return error('Invalid time format: $s')
} }
mm := pos / 3 + 1 mm := pos / 3 + 1
tmstr := malloc(s.len * 2) tmstr := malloc(s.len * 2)
count := C.sprintf(charptr(tmstr), '%s-%02d-%s %s'.str, fields[3].str, mm, count := C.sprintf(charptr(tmstr), '%s-%02d-%s %s'.str, fields[3].str, mm,
fields[1].str, fields[4].str) fields[1].str, fields[4].str)
return parse(tos(tmstr, count))
t := parse(tos(tmstr, count)) or {
// FIXME Remove this when optional forwarding is fixed.
return error('Invalid time format: $s')
}
return t
} }
pub fn new_time(t Time) Time { pub fn new_time(t Time) Time {

View File

@ -221,23 +221,47 @@ fn test_get_fmt_str() {
fn test_parse() { fn test_parse() {
s := '2018-01-27 12:48:34' s := '2018-01-27 12:48:34'
t := time.parse(s) t := time.parse(s) or {
assert false
return
}
assert t.year == 2018 && t.month == 1 && t.day == 27 && t.hour == 12 && t.minute == 48 && t.second == 34 assert t.year == 2018 && t.month == 1 && t.day == 27 && t.hour == 12 && t.minute == 48 && t.second == 34
assert t.unix == 1517057314 assert t.unix == 1517057314
} }
fn test_parse_invalid() {
s := 'Invalid time string'
t := time.parse(s) or {
assert true
return
}
assert false
}
fn test_parse_iso() { fn test_parse_iso() {
s1 := 'Thu, 12 Dec 2019 06:07:45 GMT' s1 := 'Thu, 12 Dec 2019 06:07:45 GMT'
t1 := time.parse_iso(s1) t1 := time.parse_iso(s1) or {
assert false
return
}
assert t1.year == 2019 && t1.month == 12 && t1.day == 12 && t1.hour == 6 && t1.minute == 7 && t1.second == 45 assert t1.year == 2019 && t1.month == 12 && t1.day == 12 && t1.hour == 6 && t1.minute == 7 && t1.second == 45
assert t1.unix == 1576130865 assert t1.unix == 1576130865
s2 := 'Thu 12 Dec 2019 06:07:45 +0800' s2 := 'Thu 12 Dec 2019 06:07:45 +0800'
t2 := time.parse_iso(s2) t2 := time.parse_iso(s2) or {
assert false
return
}
assert t2.year == 2019 && t2.month == 12 && t2.day == 12 && t2.hour == 6 && t2.minute == 7 && t2.second == 45 assert t2.year == 2019 && t2.month == 12 && t2.day == 12 && t2.hour == 6 && t2.minute == 7 && t2.second == 45
assert t2.unix == 1576130865 assert t2.unix == 1576130865
}
fn test_parse_iso_invalid() {
s3 := 'Thu 12 Foo 2019 06:07:45 +0800' s3 := 'Thu 12 Foo 2019 06:07:45 +0800'
t3 := time.parse_iso(s3) t3 := time.parse_iso(s3) or {
assert t3.year == 0 && t3.month == 0 && t3.day == 0 && t3.hour == 0 && t3.minute == 0 && t3.second == 0 assert true
return
}
assert false
} }
fn test_str() { fn test_str() {