2021-01-18 13:20:06 +01:00
|
|
|
// 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
|
|
|
|
|
|
|
|
// 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 {
|
2021-06-15 13:47:11 +02:00
|
|
|
tmstr := malloc_noscan(s.len * 2)
|
2021-04-04 16:43:32 +02:00
|
|
|
count := C.snprintf(&char(tmstr), (s.len * 2), c'%s-%02d-%s %s', fields[3].str,
|
2021-02-14 19:31:42 +01:00
|
|
|
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
|
|
|
}
|
2020-06-07 15:19:09 +02:00
|
|
|
|
2020-12-16 12:10:02 +01: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) }
|
2020-12-16 12:10:02 +01:00
|
|
|
if count != 3 {
|
2021-10-09 09:46:04 +02:00
|
|
|
return error_invalid_time(10)
|
2020-12-16 12:10:02 +01:00
|
|
|
}
|
|
|
|
return year, month, day
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_iso8601_time(s string) ?(int, int, int, int, i64, bool) {
|
2021-01-28 18:34:55 +01:00
|
|
|
hour_ := 0
|
|
|
|
minute_ := 0
|
|
|
|
second_ := 0
|
|
|
|
microsecond_ := 0
|
2020-11-29 09:38:14 +01:00
|
|
|
plus_min_z := `a`
|
2020-09-22 05:28:29 +02:00
|
|
|
offset_hour := 0
|
2020-12-16 12:10:02 +01:00
|
|
|
offset_minute := 0
|
2020-12-25 15:52:16 +01:00
|
|
|
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_,
|
2021-04-04 16:43:32 +02:00
|
|
|
µsecond_, &char(&plus_min_z), &offset_hour, &offset_minute)
|
2020-12-25 15:52:16 +01:00
|
|
|
}
|
2020-12-16 12:10:02 +01:00
|
|
|
// Missread microsecond ([Sec Hour Minute].len == 3 < 4)
|
|
|
|
if count < 4 {
|
2020-12-25 15:52:16 +01:00
|
|
|
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_,
|
2021-04-04 16:43:32 +02:00
|
|
|
&char(&plus_min_z), &offset_hour, &offset_minute)
|
2020-12-25 15:52:16 +01:00
|
|
|
}
|
2020-12-16 12:10:02 +01:00
|
|
|
count++ // Increment count because skipped microsecond
|
2020-12-08 17:53:54 +01:00
|
|
|
}
|
2020-12-16 12:10:02 +01:00
|
|
|
if count < 4 {
|
2021-10-09 09:46:04 +02:00
|
|
|
return error_invalid_time(10)
|
2020-06-07 15:19:09 +02:00
|
|
|
}
|
2020-12-16 12:10:02 +01: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-10-09 09:46:04 +02:00
|
|
|
return error_invalid_time(11)
|
2020-06-07 15:19:09 +02:00
|
|
|
}
|
2020-12-16 12:10:02 +01:00
|
|
|
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)
|
2020-06-07 15:19:09 +02:00
|
|
|
}
|
2020-12-16 12:10:02 +01: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 18:34:55 +01:00
|
|
|
return hour_, minute_, second_, microsecond_, unix_offset, is_local_time
|
2020-12-16 12:10:02 +01:00
|
|
|
}
|