2023-03-28 23:55:57 +03:00
|
|
|
// Copyright (c) 2019-2023 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
|
|
|
|
|
2021-10-19 20:05:27 +03:00
|
|
|
// parse_rfc3339 returns time from a date string in RFC 3339 datetime format.
|
2021-11-11 08:27:41 +03:00
|
|
|
// See also https://ijmacd.github.io/rfc3339-iso8601/ for a visual reference of
|
|
|
|
// the differences between ISO-8601 and RFC 3339.
|
2022-10-26 11:26:28 +03:00
|
|
|
pub fn parse_rfc3339(s string) !Time {
|
2021-10-19 20:05:27 +03:00
|
|
|
if s == '' {
|
2023-06-06 18:43:10 +03:00
|
|
|
return error_invalid_time(0, 'datetime string is empty')
|
2021-10-19 20:05:27 +03:00
|
|
|
}
|
2021-11-11 08:27:41 +03:00
|
|
|
// Normalize the input before parsing. Good since iso8601 doesn't permit lower case `t` and `z`.
|
|
|
|
sn := s.replace_each(['t', 'T', 'z', 'Z'])
|
|
|
|
mut t := parse_iso8601(sn) or { Time{} }
|
2021-10-19 20:05:27 +03:00
|
|
|
// If parse_iso8601 DID NOT result in default values (i.e. date was parsed correctly)
|
|
|
|
if t != Time{} {
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2021-11-11 08:27:41 +03:00
|
|
|
t_i := sn.index('T') or { -1 }
|
|
|
|
parts := if t_i != -1 { [sn[..t_i], sn[t_i + 1..]] } else { sn.split(' ') }
|
2021-10-19 20:05:27 +03:00
|
|
|
|
2021-11-11 08:27:41 +03:00
|
|
|
// Check if sn is date only
|
2021-10-19 20:05:27 +03:00
|
|
|
if !parts[0].contains_any(' Z') && parts[0].contains('-') {
|
2022-10-26 11:26:28 +03:00
|
|
|
year, month, day := parse_iso8601_date(sn)!
|
2021-10-19 20:05:27 +03:00
|
|
|
t = new_time(Time{
|
|
|
|
year: year
|
|
|
|
month: month
|
|
|
|
day: day
|
|
|
|
})
|
|
|
|
return t
|
|
|
|
}
|
2021-11-11 08:27:41 +03:00
|
|
|
// Check if sn is time only
|
2021-10-19 20:05:27 +03:00
|
|
|
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
|
2022-10-26 11:26:28 +03:00
|
|
|
hour_, minute_, second_, microsecond_, unix_offset, is_local_time = parse_iso8601_time(parts[0])!
|
2021-10-19 20:05:27 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-06-06 18:43:10 +03:00
|
|
|
return error_invalid_time(9, 'malformed date')
|
2021-10-19 20:05:27 +03:00
|
|
|
}
|
|
|
|
|
2022-05-11 16:55:19 +03:00
|
|
|
// parse returns time from a date string in "YYYY-MM-DD HH:mm:ss" format.
|
2022-10-26 11:26:28 +03:00
|
|
|
pub fn parse(s string) !Time {
|
2021-10-19 20:05:27 +03:00
|
|
|
if s == '' {
|
2023-06-06 18:43:10 +03:00
|
|
|
return error_invalid_time(0, 'datetime string is empty')
|
|
|
|
}
|
|
|
|
pos := s.index(' ') or {
|
|
|
|
return error_invalid_time(1, 'string has no space between date and time')
|
2021-10-19 20:05:27 +03:00
|
|
|
}
|
|
|
|
symd := s[..pos]
|
|
|
|
ymd := symd.split('-')
|
|
|
|
if ymd.len != 3 {
|
2023-06-06 18:43:10 +03:00
|
|
|
return error_invalid_time(2, 'date must be in the form of y-m-d')
|
2021-10-19 20:05:27 +03:00
|
|
|
}
|
|
|
|
shms := s[pos..]
|
|
|
|
hms := shms.split(':')
|
2022-12-27 13:58:43 +03:00
|
|
|
if hms.len != 3 {
|
2023-06-06 18:43:10 +03:00
|
|
|
return error_invalid_time(9, 'time must be in the form of H:i:s')
|
2022-12-27 13:58:43 +03:00
|
|
|
}
|
2021-10-19 20:05:27 +03:00
|
|
|
hour_ := hms[0][1..]
|
|
|
|
minute_ := hms[1]
|
|
|
|
second_ := hms[2]
|
|
|
|
//
|
|
|
|
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 {
|
2023-06-06 18:43:10 +03:00
|
|
|
return error_invalid_time(3, 'year must be between -10000 and 10000')
|
2021-10-19 20:05:27 +03:00
|
|
|
}
|
|
|
|
if imonth > 12 || imonth < 1 {
|
2023-06-06 18:43:10 +03:00
|
|
|
return error_invalid_time(4, 'month must be between 1 and 12')
|
2021-10-19 20:05:27 +03:00
|
|
|
}
|
|
|
|
if iday > 31 || iday < 1 {
|
2023-06-06 18:43:10 +03:00
|
|
|
return error_invalid_time(5, 'day must be between 1 and 31')
|
2021-10-19 20:05:27 +03:00
|
|
|
}
|
|
|
|
if ihour > 23 || ihour < 0 {
|
2023-06-06 18:43:10 +03:00
|
|
|
return error_invalid_time(6, 'hours must be between 0 and 24')
|
2021-10-19 20:05:27 +03:00
|
|
|
}
|
|
|
|
if iminute > 59 || iminute < 0 {
|
2023-06-06 18:43:10 +03:00
|
|
|
return error_invalid_time(7, 'minutes must be between 0 and 60')
|
2021-10-19 20:05:27 +03:00
|
|
|
}
|
|
|
|
if isecond > 59 || isecond < 0 {
|
2023-06-06 18:43:10 +03:00
|
|
|
return error_invalid_time(8, 'seconds must be between 0 and 60')
|
2021-10-19 20:05:27 +03:00
|
|
|
}
|
|
|
|
res := new_time(Time{
|
|
|
|
year: iyear
|
|
|
|
month: imonth
|
|
|
|
day: iday
|
|
|
|
hour: ihour
|
|
|
|
minute: iminute
|
|
|
|
second: isecond
|
|
|
|
})
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2023-06-06 18:43:10 +03:00
|
|
|
// parse_format parses the string `s`, as a custom `format`, containing the following specifiers:
|
|
|
|
// YYYY - 4 digit year, 0000..9999
|
|
|
|
// YY - 2 digit year, 00..99
|
|
|
|
// M - month, 1..12
|
|
|
|
// MM - month, 2 digits, 01..12
|
|
|
|
// MMMM - name of month
|
|
|
|
// D - day of the month, 1..31
|
|
|
|
// DD - day of the month, 01..31
|
|
|
|
// H - hour, 0..23
|
|
|
|
// HH - hour, 00..23
|
|
|
|
// h - hour, 0..23
|
|
|
|
// hh - hour, 0..23
|
|
|
|
// k - hour, 0..23
|
|
|
|
// kk - hour, 0..23
|
|
|
|
// m - minute, 0..59
|
|
|
|
// mm - minute, 0..59
|
|
|
|
// s - second, 0..59
|
|
|
|
// ss - second, 0..59
|
|
|
|
pub fn parse_format(s string, format string) !Time {
|
|
|
|
if s == '' {
|
|
|
|
return error_invalid_time(0, 'datetime string is empty')
|
|
|
|
}
|
|
|
|
mut p := new_date_time_parser(s, format)
|
|
|
|
return p.parse()
|
|
|
|
}
|
|
|
|
|
2023-06-24 08:01:57 +03:00
|
|
|
// parse_iso8601 parses the ISO 8601 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.
|
|
|
|
// Both can be +/- HH:mm .
|
|
|
|
// See https://en.wikipedia.org/wiki/ISO_8601 .
|
|
|
|
// Remarks: not all of ISO 8601 is supported; checks and support for leapseconds should be added.
|
2022-10-26 11:26:28 +03:00
|
|
|
pub fn parse_iso8601(s string) !Time {
|
2021-10-19 20:05:27 +03:00
|
|
|
if s == '' {
|
2023-06-06 18:43:10 +03:00
|
|
|
return error_invalid_time(0, 'datetime string is empty')
|
2021-10-19 20:05:27 +03:00
|
|
|
}
|
|
|
|
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) {
|
2023-06-06 18:43:10 +03:00
|
|
|
return error_invalid_time(12, 'malformed date')
|
2021-10-19 20:05:27 +03:00
|
|
|
}
|
2022-10-26 11:26:28 +03:00
|
|
|
year, month, day := parse_iso8601_date(parts[0])!
|
2021-10-19 20:05:27 +03:00
|
|
|
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 {
|
2022-10-26 11:26:28 +03:00
|
|
|
hour_, minute_, second_, microsecond_, unix_offset, is_local_time = parse_iso8601_time(parts[1])!
|
2021-10-19 20:05:27 +03:00
|
|
|
}
|
|
|
|
mut t := new_time(
|
|
|
|
year: year
|
|
|
|
month: month
|
|
|
|
day: day
|
|
|
|
hour: hour_
|
|
|
|
minute: minute_
|
|
|
|
second: second_
|
|
|
|
microsecond: microsecond_
|
|
|
|
)
|
|
|
|
if is_local_time {
|
|
|
|
return t // Time 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
|
|
|
|
}
|
|
|
|
|
2020-02-06 16:19:44 +03:00
|
|
|
// parse_rfc2822 returns time from a date string in RFC 2822 datetime format.
|
2022-10-26 11:26:28 +03:00
|
|
|
pub fn parse_rfc2822(s string) !Time {
|
2021-10-09 10:46:04 +03:00
|
|
|
if s == '' {
|
2023-06-06 18:43:10 +03:00
|
|
|
return error_invalid_time(0, 'datetime string is empty')
|
2021-10-09 10:46:04 +03:00
|
|
|
}
|
2020-02-06 16:19:44 +03:00
|
|
|
fields := s.split(' ')
|
|
|
|
if fields.len < 5 {
|
2023-06-06 18:43:10 +03:00
|
|
|
return error_invalid_time(1, 'datetime string must have 5 components, has: ${fields.len}')
|
|
|
|
}
|
|
|
|
pos := months_string.index(fields[2]) or {
|
|
|
|
return error_invalid_time(2, 'invalid month format')
|
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 -----
|
2022-10-26 11:26:28 +03:00
|
|
|
fn parse_iso8601_date(s string) !(int, int, int) {
|
2022-04-15 14:58:56 +03:00
|
|
|
year, month, day, dummy := 0, 0, 0, u8(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 {
|
2023-06-06 18:43:10 +03:00
|
|
|
return error_invalid_time(10, 'datetime string must have 3 components, but has ${count}')
|
2020-12-16 14:10:02 +03:00
|
|
|
}
|
2021-11-11 08:27:41 +03:00
|
|
|
if year > 9999 {
|
2023-06-06 18:43:10 +03:00
|
|
|
return error_invalid_time(13, 'year must be smaller than 10000')
|
2021-11-11 08:27:41 +03:00
|
|
|
}
|
|
|
|
if month > 12 {
|
2023-06-06 18:43:10 +03:00
|
|
|
return error_invalid_time(14, 'month must be smaller than 12')
|
2021-11-11 08:27:41 +03:00
|
|
|
}
|
|
|
|
if day > 31 {
|
2023-06-06 18:43:10 +03:00
|
|
|
return error_invalid_time(15, 'day must be smaller than 31')
|
2021-11-11 08:27:41 +03:00
|
|
|
}
|
2020-12-16 14:10:02 +03:00
|
|
|
return year, month, day
|
|
|
|
}
|
|
|
|
|
2022-10-26 11:26:28 +03:00
|
|
|
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
|
2022-05-08 13:56:50 +03:00
|
|
|
mut microsecond_ := 0
|
|
|
|
mut nanosecond_ := 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
|
2022-05-08 13:56:50 +03:00
|
|
|
mut count := 0
|
|
|
|
count = unsafe {
|
|
|
|
C.sscanf(&char(s.str), c'%2d:%2d:%2d.%9d%c', &hour_, &minute_, &second_, &nanosecond_,
|
|
|
|
&char(&plus_min_z))
|
|
|
|
}
|
|
|
|
if count == 5 && plus_min_z == `Z` {
|
|
|
|
// normalise the nanoseconds:
|
|
|
|
mut ndigits := 0
|
|
|
|
if mut pos := s.index('.') {
|
|
|
|
pos++
|
|
|
|
for ; pos < s.len && s[pos].is_digit(); pos++ {
|
|
|
|
ndigits++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for ndigits < 9 {
|
|
|
|
nanosecond_ *= 10
|
|
|
|
ndigits++
|
|
|
|
}
|
|
|
|
microsecond_ = nanosecond_ / 1000
|
|
|
|
} else {
|
2020-12-25 17:52:16 +03:00
|
|
|
count = unsafe {
|
2023-06-21 12:34:50 +03:00
|
|
|
C.sscanf(&char(s.str), c'%2d:%2d:%2d.%9d%c%2d:%2d', &hour_, &minute_, &second_,
|
2022-05-08 13:56:50 +03:00
|
|
|
µsecond_, &char(&plus_min_z), &offset_hour, &offset_minute)
|
|
|
|
}
|
|
|
|
// Missread microsecond ([Sec Hour Minute].len == 3 < 4)
|
|
|
|
if count < 4 {
|
|
|
|
count = unsafe {
|
|
|
|
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 {
|
2023-06-06 18:43:10 +03:00
|
|
|
return error_invalid_time(10, 'malformed date')
|
2020-12-25 17:52:16 +03:00
|
|
|
}
|
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) {
|
2023-06-06 18:43:10 +03:00
|
|
|
return error_invalid_time(11, 'malformed date')
|
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 {
|
2023-06-06 18:43:10 +03:00
|
|
|
return error_invalid_time(12, 'missing timezone')
|
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
|
|
|
}
|