v/vlib/time/parse.c.v

234 lines
6.2 KiB
V
Raw Normal View History

// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
2020-02-06 14:19:44 +01:00
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module time
2021-10-09 09:46:04 +02:00
pub struct TimeParseError {
msg string
code int
}
fn error_invalid_time(code int) IError {
return TimeParseError{
msg: 'Invalid time format code: $code'
code: code
}
}
2020-02-06 14:19:44 +01:00
// parse returns time from a date string in "YYYY-MM-DD HH:MM:SS" format.
pub fn parse(s string) ?Time {
2021-10-09 09:46:04 +02:00
if s == '' {
return error_invalid_time(0)
}
pos := s.index(' ') or { return error_invalid_time(1) }
2020-02-06 14:19:44 +01:00
symd := s[..pos]
ymd := symd.split('-')
if ymd.len != 3 {
2021-10-09 09:46:04 +02:00
return error_invalid_time(2)
2020-02-06 14:19:44 +01:00
}
shms := s[pos..]
hms := shms.split(':')
hour_ := hms[0][1..]
minute_ := hms[1]
second_ := hms[2]
2021-10-09 09:46:04 +02:00
//
iyear := ymd[0].int()
imonth := ymd[1].int()
iday := ymd[2].int()
ihour := hour_.int()
iminute := minute_.int()
isecond := second_.int()
// eprintln('>> iyear: $iyear | imonth: $imonth | iday: $iday | ihour: $ihour | iminute: $iminute | isecond: $isecond')
if iyear > 9999 || iyear < -9999 {
return error_invalid_time(3)
}
if imonth > 12 || imonth < 1 {
return error_invalid_time(4)
}
if iday > 31 || iday < 1 {
return error_invalid_time(5)
}
if ihour > 23 || ihour < 0 {
return error_invalid_time(6)
}
if iminute > 59 || iminute < 0 {
return error_invalid_time(7)
}
if isecond > 59 || isecond < 0 {
return error_invalid_time(8)
}
2020-03-19 11:23:02 +01:00
res := new_time(Time{
2021-10-09 09:46:04 +02:00
year: iyear
month: imonth
day: iday
hour: ihour
minute: iminute
second: isecond
2020-02-06 14:19:44 +01:00
})
2020-03-19 11:23:02 +01:00
return res
2020-02-06 14:19:44 +01:00
}
// parse_rfc2822 returns time from a date string in RFC 2822 datetime format.
pub fn parse_rfc2822(s string) ?Time {
2021-10-09 09:46:04 +02:00
if s == '' {
return error_invalid_time(0)
}
2020-02-06 14:19:44 +01:00
fields := s.split(' ')
if fields.len < 5 {
2021-10-09 09:46:04 +02:00
return error_invalid_time(1)
2020-02-06 14:19:44 +01:00
}
2021-10-09 09:46:04 +02:00
pos := months_string.index(fields[2]) or { return error_invalid_time(2) }
2020-02-06 14:19:44 +01:00
mm := pos / 3 + 1
2020-10-08 16:59:43 +02:00
unsafe {
tmstr := malloc_noscan(s.len * 2)
count := C.snprintf(&char(tmstr), (s.len * 2), c'%s-%02d-%s %s', fields[3].str,
mm, fields[1].str, fields[4].str)
return parse(tos(tmstr, count))
2020-10-08 16:59:43 +02:00
}
2020-02-06 14:19:44 +01:00
}
2021-09-24 20:13:52 +02:00
// parse_rfc3339 returns time from a date string in RFC 3339 datetime format.
pub fn parse_rfc3339(s string) ?Time {
if s == '' {
2021-10-09 09:46:04 +02:00
return error_invalid_time(0)
2021-09-24 20:13:52 +02:00
}
mut t := parse_iso8601(s) or { Time{} }
// If parse_iso8601 DID NOT result in default values (i.e. date was parsed correctly)
if t != Time{} {
return t
}
t_i := s.index('T') or { -1 }
parts := if t_i != -1 { [s[..t_i], s[t_i + 1..]] } else { s.split(' ') }
// Check if s is date only
if !parts[0].contains_any(' Z') && parts[0].contains('-') {
year, month, day := parse_iso8601_date(s) ?
t = new_time(Time{
year: year
month: month
day: day
})
return t
}
// Check if s is time only
if !parts[0].contains('-') && parts[0].contains(':') {
mut hour_, mut minute_, mut second_, mut microsecond_, mut unix_offset, mut is_local_time := 0, 0, 0, 0, i64(0), true
hour_, minute_, second_, microsecond_, unix_offset, is_local_time = parse_iso8601_time(parts[0]) ?
t = new_time(Time{
hour: hour_
minute: minute_
second: second_
microsecond: microsecond_
})
if is_local_time {
return t // Time is already local time
}
mut unix_time := t.unix
if unix_offset < 0 {
unix_time -= (-unix_offset)
} else if unix_offset > 0 {
unix_time += unix_offset
}
t = unix2(i64(unix_time), t.microsecond)
return t
}
2021-10-09 09:46:04 +02:00
return error_invalid_time(9)
2021-09-24 20:13:52 +02:00
}
// ----- iso8601 -----
fn parse_iso8601_date(s string) ?(int, int, int) {
year, month, day, dummy := 0, 0, 0, byte(0)
2021-04-05 20:39:32 +02:00
count := unsafe { C.sscanf(&char(s.str), c'%4d-%2d-%2d%c', &year, &month, &day, &dummy) }
if count != 3 {
2021-10-09 09:46:04 +02:00
return error_invalid_time(10)
}
return year, month, day
}
fn parse_iso8601_time(s string) ?(int, int, int, int, i64, bool) {
hour_ := 0
minute_ := 0
second_ := 0
microsecond_ := 0
2020-11-29 09:38:14 +01:00
plus_min_z := `a`
offset_hour := 0
offset_minute := 0
mut count := unsafe {
2021-04-05 20:39:32 +02:00
C.sscanf(&char(s.str), c'%2d:%2d:%2d.%6d%c%2d:%2d', &hour_, &minute_, &second_,
&microsecond_, &char(&plus_min_z), &offset_hour, &offset_minute)
}
// Missread microsecond ([Sec Hour Minute].len == 3 < 4)
if count < 4 {
count = unsafe {
2021-04-05 20:39:32 +02:00
C.sscanf(&char(s.str), c'%2d:%2d:%2d%c%2d:%2d', &hour_, &minute_, &second_,
&char(&plus_min_z), &offset_hour, &offset_minute)
}
count++ // Increment count because skipped microsecond
}
if count < 4 {
2021-10-09 09:46:04 +02:00
return error_invalid_time(10)
}
is_local_time := plus_min_z == `a` && count == 4
is_utc := plus_min_z == `Z` && count == 5
if !(count == 7 || is_local_time || is_utc) {
2021-10-09 09:46:04 +02:00
return error_invalid_time(11)
}
if plus_min_z != `+` && plus_min_z != `-` && !is_utc && !is_local_time {
2021-10-09 09:46:04 +02:00
return error_invalid_time(12)
}
mut unix_offset := 0
if offset_hour > 0 {
unix_offset += 3600 * offset_hour
}
if offset_minute > 0 {
unix_offset += 60 * offset_minute
}
if plus_min_z == `+` {
unix_offset *= -1
}
return hour_, minute_, second_, microsecond_, unix_offset, is_local_time
}
// parse_iso8601 parses rfc8601 time format yyyy-MM-ddTHH:mm:ss.dddddd+dd:dd as local time
// the fraction part is difference in milli seconds and the last part is offset
// from UTC time and can be both +/- HH:mm
// remarks: not all iso8601 is supported
// also checks and support for leapseconds should be added in future PR
pub fn parse_iso8601(s string) ?Time {
2021-10-09 09:46:04 +02:00
if s == '' {
return error_invalid_time(0)
}
t_i := s.index('T') or { -1 }
parts := if t_i != -1 { [s[..t_i], s[t_i + 1..]] } else { s.split(' ') }
if !(parts.len == 1 || parts.len == 2) {
2021-10-09 09:46:04 +02:00
return error_invalid_time(12)
}
year, month, day := parse_iso8601_date(parts[0]) ?
mut hour_, mut minute_, mut second_, mut microsecond_, mut unix_offset, mut is_local_time := 0, 0, 0, 0, i64(0), true
if parts.len == 2 {
hour_, minute_, second_, microsecond_, unix_offset, is_local_time = parse_iso8601_time(parts[1]) ?
}
2020-06-10 11:14:55 +02:00
mut t := new_time(Time{
year: year
month: month
day: day
hour: hour_
minute: minute_
second: second_
microsecond: microsecond_
})
2020-11-29 09:38:14 +01:00
if is_local_time {
return t // Time already local time
2020-11-29 09:38:14 +01:00
}
2020-10-08 16:59:43 +02:00
mut unix_time := t.unix
if unix_offset < 0 {
unix_time -= (-unix_offset)
} else if unix_offset > 0 {
unix_time += unix_offset
}
t = unix2(i64(unix_time), t.microsecond)
return t
}