mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
time: vfmt time/parse.v
This commit is contained in:
parent
c3626bf2e6
commit
1123b8259a
@ -18,7 +18,6 @@ pub fn parse(s string) ?Time {
|
|||||||
hour := hms[0][1..]
|
hour := hms[0][1..]
|
||||||
minute := hms[1]
|
minute := hms[1]
|
||||||
second := hms[2]
|
second := hms[2]
|
||||||
|
|
||||||
res := new_time(Time{
|
res := new_time(Time{
|
||||||
year: ymd[0].int()
|
year: ymd[0].int()
|
||||||
month: ymd[1].int()
|
month: ymd[1].int()
|
||||||
@ -41,10 +40,11 @@ pub fn parse_rfc2822(s string) ?Time {
|
|||||||
}
|
}
|
||||||
mm := pos / 3 + 1
|
mm := pos / 3 + 1
|
||||||
mut tmstr := byteptr(0)
|
mut tmstr := byteptr(0)
|
||||||
unsafe { tmstr = malloc(s.len * 2) }
|
unsafe {
|
||||||
count := unsafe {C.snprintf(charptr(tmstr), (s.len * 2), '%s-%02d-%s %s', fields[3].str, mm,
|
tmstr = malloc(s.len * 2)
|
||||||
fields[1].str, fields[4].str)}
|
}
|
||||||
|
count := unsafe {C.snprintf(charptr(tmstr), (s.len * 2), '%s-%02d-%s %s', fields[3].str,
|
||||||
|
mm, fields[1].str, fields[4].str)}
|
||||||
return parse(tos(tmstr, count))
|
return parse(tos(tmstr, count))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -52,38 +52,31 @@ pub fn parse_rfc2822(s string) ?Time {
|
|||||||
// the fraction part is difference in milli seconds and the last part is offset
|
// the fraction part is difference in milli seconds and the last part is offset
|
||||||
// from UTC time and can be both +/- HH:mm
|
// from UTC time and can be both +/- HH:mm
|
||||||
// remarks: not all iso8601 is supported only the 'yyyy-MM-ddTHH:mm:ss.dddddd+dd:dd'
|
// remarks: not all iso8601 is supported only the 'yyyy-MM-ddTHH:mm:ss.dddddd+dd:dd'
|
||||||
// also checks and support for leapseconds should be added in future PR
|
// also checks and support for leapseconds should be added in future PR
|
||||||
pub fn parse_iso8601(s string) ?Time {
|
pub fn parse_iso8601(s string) ?Time {
|
||||||
|
year := 0
|
||||||
year := 0
|
month := 0
|
||||||
month := 0
|
day := 0
|
||||||
day := 0
|
hour := 0
|
||||||
hour := 0
|
minute := 0
|
||||||
minute := 0
|
second := 0
|
||||||
second := 0
|
mic_second := 0
|
||||||
mic_second := 0
|
time_char := `a`
|
||||||
time_char := `a`
|
plus_min := `a`
|
||||||
plus_min := `a`
|
|
||||||
offset_hour := 0
|
offset_hour := 0
|
||||||
offset_min := 0
|
offset_min := 0
|
||||||
|
count := unsafe {C.sscanf(charptr(s.str), '%4d-%2d-%2d%c%2d:%2d:%2d.%6d%c%2d:%2d',
|
||||||
count := unsafe {C.sscanf(charptr(s.str), "%4d-%2d-%2d%c%2d:%2d:%2d.%6d%c%2d:%2d", &year, &month, &day,
|
&year, &month, &day, &time_char, &hour, &minute, &second, &mic_second, &plus_min, &offset_hour,
|
||||||
&time_char, &hour, &minute,
|
&offset_min)}
|
||||||
&second, &mic_second, &plus_min,
|
|
||||||
&offset_hour, &offset_min)}
|
|
||||||
|
|
||||||
if count != 11 {
|
if count != 11 {
|
||||||
return error('Invalid 8601 format')
|
return error('Invalid 8601 format')
|
||||||
}
|
}
|
||||||
if time_char != `T` && time_char != ` ` {
|
if time_char != `T` && time_char != ` ` {
|
||||||
return error('Invalid 8601 format, expected space or `T` as time separator')
|
return error('Invalid 8601 format, expected space or `T` as time separator')
|
||||||
}
|
}
|
||||||
|
|
||||||
if plus_min != `+` && plus_min != `-` {
|
if plus_min != `+` && plus_min != `-` {
|
||||||
return error('Invalid 8601 format, expected `+` or `-` as time separator' )
|
return error('Invalid 8601 format, expected `+` or `-` as time separator')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
mut t := new_time(Time{
|
mut t := new_time(Time{
|
||||||
year: year
|
year: year
|
||||||
month: month
|
month: month
|
||||||
@ -93,17 +86,14 @@ pub fn parse_iso8601(s string) ?Time {
|
|||||||
second: second
|
second: second
|
||||||
microsecond: mic_second
|
microsecond: mic_second
|
||||||
})
|
})
|
||||||
|
mut unix_time := t.unix
|
||||||
mut unix_time := t.unix
|
|
||||||
mut unix_offset := int(0)
|
mut unix_offset := int(0)
|
||||||
|
|
||||||
if offset_hour > 0 {
|
if offset_hour > 0 {
|
||||||
unix_offset += 3600*offset_hour
|
unix_offset += 3600 * offset_hour
|
||||||
}
|
}
|
||||||
if offset_min > 0 {
|
if offset_min > 0 {
|
||||||
unix_offset += 60*offset_min
|
unix_offset += 60 * offset_min
|
||||||
}
|
}
|
||||||
|
|
||||||
if unix_offset != 0 {
|
if unix_offset != 0 {
|
||||||
if plus_min == `+` {
|
if plus_min == `+` {
|
||||||
unix_time -= u64(unix_offset)
|
unix_time -= u64(unix_offset)
|
||||||
@ -112,8 +102,6 @@ pub fn parse_iso8601(s string) ?Time {
|
|||||||
}
|
}
|
||||||
t = unix2(int(unix_time), t.microsecond)
|
t = unix2(int(unix_time), t.microsecond)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert the time to local time
|
// Convert the time to local time
|
||||||
|
|
||||||
return to_local_time(t)
|
return to_local_time(t)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user