2021-01-18 15:20:06 +03:00
|
|
|
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
|
2020-02-06 16:19:44 +03:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
module time
|
|
|
|
|
|
|
|
// parse returns time from a date string in "YYYY-MM-DD HH:MM:SS" format.
|
|
|
|
pub fn parse(s string) ?Time {
|
2020-12-09 16:48:21 +03:00
|
|
|
pos := s.index(' ') or { return error('Invalid time format: $s') }
|
2020-02-06 16:19:44 +03:00
|
|
|
symd := s[..pos]
|
|
|
|
ymd := symd.split('-')
|
|
|
|
if ymd.len != 3 {
|
|
|
|
return error('Invalid time format: $s')
|
|
|
|
}
|
|
|
|
shms := s[pos..]
|
|
|
|
hms := shms.split(':')
|
2021-01-28 20:34:55 +03:00
|
|
|
hour_ := hms[0][1..]
|
|
|
|
minute_ := hms[1]
|
|
|
|
second_ := hms[2]
|
2020-03-19 13:23:02 +03:00
|
|
|
res := new_time(Time{
|
2020-02-06 16:19:44 +03:00
|
|
|
year: ymd[0].int()
|
|
|
|
month: ymd[1].int()
|
|
|
|
day: ymd[2].int()
|
2021-01-28 20:34:55 +03:00
|
|
|
hour: hour_.int()
|
|
|
|
minute: minute_.int()
|
|
|
|
second: second_.int()
|
2020-02-06 16:19:44 +03:00
|
|
|
})
|
2020-03-19 13:23:02 +03:00
|
|
|
return res
|
2020-02-06 16:19:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// parse_rfc2822 returns time from a date string in RFC 2822 datetime format.
|
|
|
|
pub fn parse_rfc2822(s string) ?Time {
|
|
|
|
fields := s.split(' ')
|
|
|
|
if fields.len < 5 {
|
|
|
|
return error('Invalid time format: $s')
|
|
|
|
}
|
2020-12-09 16:48:21 +03:00
|
|
|
pos := months_string.index(fields[2]) or { return error('Invalid time format: $s') }
|
2020-02-06 16:19:44 +03:00
|
|
|
mm := pos / 3 + 1
|
2020-10-08 17:59:43 +03:00
|
|
|
unsafe {
|
2021-06-15 14:47:11 +03:00
|
|
|
tmstr := malloc_noscan(s.len * 2)
|
2021-04-04 17:43:32 +03:00
|
|
|
count := C.snprintf(&char(tmstr), (s.len * 2), c'%s-%02d-%s %s', fields[3].str,
|
2021-02-14 21:31:42 +03:00
|
|
|
mm, fields[1].str, fields[4].str)
|
|
|
|
return parse(tos(tmstr, count))
|
2020-10-08 17:59:43 +03:00
|
|
|
}
|
2020-02-06 16:19:44 +03:00
|
|
|
}
|
2020-06-07 16:19:09 +03:00
|
|
|
|
2020-12-16 14:10:02 +03:00
|
|
|
// ----- iso8601 -----
|
|
|
|
const (
|
2021-05-18 12:32:09 +03:00
|
|
|
err_invalid_8601 = 'Invalid 8601 Format'
|
2020-12-16 14:10:02 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
fn parse_iso8601_date(s string) ?(int, int, int) {
|
|
|
|
year, month, day, dummy := 0, 0, 0, byte(0)
|
2021-04-05 21:39:32 +03:00
|
|
|
count := unsafe { C.sscanf(&char(s.str), c'%4d-%2d-%2d%c', &year, &month, &day, &dummy) }
|
2020-12-16 14:10:02 +03:00
|
|
|
if count != 3 {
|
2021-05-18 12:32:09 +03:00
|
|
|
return error(time.err_invalid_8601)
|
2020-12-16 14:10:02 +03:00
|
|
|
}
|
|
|
|
return year, month, day
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_iso8601_time(s string) ?(int, int, int, int, i64, bool) {
|
2021-01-28 20:34:55 +03:00
|
|
|
hour_ := 0
|
|
|
|
minute_ := 0
|
|
|
|
second_ := 0
|
|
|
|
microsecond_ := 0
|
2020-11-29 11:38:14 +03:00
|
|
|
plus_min_z := `a`
|
2020-09-22 06:28:29 +03:00
|
|
|
offset_hour := 0
|
2020-12-16 14:10:02 +03:00
|
|
|
offset_minute := 0
|
2020-12-25 17:52:16 +03:00
|
|
|
mut count := unsafe {
|
2021-04-05 21:39:32 +03:00
|
|
|
C.sscanf(&char(s.str), c'%2d:%2d:%2d.%6d%c%2d:%2d', &hour_, &minute_, &second_,
|
2021-04-04 17:43:32 +03:00
|
|
|
µsecond_, &char(&plus_min_z), &offset_hour, &offset_minute)
|
2020-12-25 17:52:16 +03:00
|
|
|
}
|
2020-12-16 14:10:02 +03:00
|
|
|
// Missread microsecond ([Sec Hour Minute].len == 3 < 4)
|
|
|
|
if count < 4 {
|
2020-12-25 17:52:16 +03:00
|
|
|
count = unsafe {
|
2021-04-05 21:39:32 +03:00
|
|
|
C.sscanf(&char(s.str), c'%2d:%2d:%2d%c%2d:%2d', &hour_, &minute_, &second_,
|
2021-04-04 17:43:32 +03:00
|
|
|
&char(&plus_min_z), &offset_hour, &offset_minute)
|
2020-12-25 17:52:16 +03:00
|
|
|
}
|
2020-12-16 14:10:02 +03:00
|
|
|
count++ // Increment count because skipped microsecond
|
2020-12-08 19:53:54 +03:00
|
|
|
}
|
2020-12-16 14:10:02 +03:00
|
|
|
if count < 4 {
|
2021-05-18 12:32:09 +03:00
|
|
|
return error(time.err_invalid_8601)
|
2020-06-07 16:19:09 +03:00
|
|
|
}
|
2020-12-16 14:10:02 +03:00
|
|
|
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-05-18 12:32:09 +03:00
|
|
|
return error(time.err_invalid_8601)
|
2020-06-07 16:19:09 +03:00
|
|
|
}
|
2020-12-16 14:10:02 +03:00
|
|
|
if plus_min_z != `+` && plus_min_z != `-` && !is_utc && !is_local_time {
|
2020-11-29 11:38:14 +03:00
|
|
|
return error('Invalid 8601 format, expected `Z` or `+` or `-` as time separator')
|
2020-06-07 16:19:09 +03:00
|
|
|
}
|
2020-12-16 14:10:02 +03:00
|
|
|
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
|
|
|
|
}
|
2021-01-28 20:34:55 +03:00
|
|
|
return hour_, minute_, second_, microsecond_, unix_offset, is_local_time
|
2020-12-16 14:10:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
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-05-18 12:32:09 +03:00
|
|
|
return error(time.err_invalid_8601)
|
2020-12-16 14:10:02 +03:00
|
|
|
}
|
|
|
|
year, month, day := parse_iso8601_date(parts[0]) ?
|
2021-01-28 20:34:55 +03:00
|
|
|
mut hour_, mut minute_, mut second_, mut microsecond_, mut unix_offset, mut is_local_time := 0, 0, 0, 0, i64(0), true
|
2020-12-16 14:10:02 +03:00
|
|
|
if parts.len == 2 {
|
2021-01-28 20:34:55 +03:00
|
|
|
hour_, minute_, second_, microsecond_, unix_offset, is_local_time = parse_iso8601_time(parts[1]) ?
|
2020-12-16 14:10:02 +03:00
|
|
|
}
|
2020-06-10 12:14:55 +03:00
|
|
|
mut t := new_time(Time{
|
2020-06-07 16:19:09 +03:00
|
|
|
year: year
|
|
|
|
month: month
|
|
|
|
day: day
|
2021-01-28 20:34:55 +03:00
|
|
|
hour: hour_
|
|
|
|
minute: minute_
|
|
|
|
second: second_
|
|
|
|
microsecond: microsecond_
|
2020-06-07 16:19:09 +03:00
|
|
|
})
|
2020-11-29 11:38:14 +03:00
|
|
|
if is_local_time {
|
2020-12-12 11:19:03 +03:00
|
|
|
return t // Time already local time
|
2020-11-29 11:38:14 +03:00
|
|
|
}
|
2020-10-08 17:59:43 +03:00
|
|
|
mut unix_time := t.unix
|
2020-12-16 14:10:02 +03:00
|
|
|
if unix_offset < 0 {
|
|
|
|
unix_time -= u64(-unix_offset)
|
|
|
|
} else if unix_offset > 0 {
|
|
|
|
unix_time += u64(unix_offset)
|
2020-06-07 16:19:09 +03:00
|
|
|
}
|
2020-12-16 14:10:02 +03:00
|
|
|
t = unix2(int(unix_time), t.microsecond)
|
2021-01-04 19:26:21 +03:00
|
|
|
return t
|
2020-06-07 16:19:09 +03:00
|
|
|
}
|