2020-02-06 16:19:44 +03:00
|
|
|
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
|
|
|
// 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(':')
|
|
|
|
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()
|
|
|
|
hour: hour.int()
|
|
|
|
minute: minute.int()
|
|
|
|
second: second.int()
|
|
|
|
})
|
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
|
|
|
|
mut tmstr := byteptr(0)
|
2020-10-08 17:59:43 +03:00
|
|
|
unsafe {
|
|
|
|
tmstr = malloc(s.len * 2)
|
|
|
|
}
|
|
|
|
count := unsafe {C.snprintf(charptr(tmstr), (s.len * 2), '%s-%02d-%s %s', fields[3].str,
|
|
|
|
mm, fields[1].str, fields[4].str)}
|
2020-04-11 19:56:39 +03:00
|
|
|
return parse(tos(tmstr, count))
|
2020-02-06 16:19:44 +03:00
|
|
|
}
|
2020-06-07 16:19:09 +03:00
|
|
|
|
2020-06-10 12:14:55 +03:00
|
|
|
// parse_iso8601 parses rfc8601 time format yyyy-MM-ddTHH:mm:ss.dddddd+dd:dd as local time
|
2020-06-07 16:19:09 +03:00
|
|
|
// the fraction part is difference in milli seconds and the last part is offset
|
|
|
|
// from UTC time and can be both +/- HH:mm
|
2020-12-08 19:53:54 +03:00
|
|
|
// remarks: not all iso8601 is supported
|
2020-10-08 17:59:43 +03:00
|
|
|
// also checks and support for leapseconds should be added in future PR
|
2020-06-10 12:14:55 +03:00
|
|
|
pub fn parse_iso8601(s string) ?Time {
|
2020-10-08 17:59:43 +03:00
|
|
|
year := 0
|
|
|
|
month := 0
|
|
|
|
day := 0
|
|
|
|
hour := 0
|
|
|
|
minute := 0
|
|
|
|
second := 0
|
|
|
|
mic_second := 0
|
|
|
|
time_char := `a`
|
2020-11-29 11:38:14 +03:00
|
|
|
plus_min_z := `a`
|
2020-09-22 06:28:29 +03:00
|
|
|
offset_hour := 0
|
2020-10-08 17:59:43 +03:00
|
|
|
offset_min := 0
|
2020-12-08 19:53:54 +03:00
|
|
|
mut count := unsafe {C.sscanf(charptr(s.str), '%4d-%2d-%2d%c%2d:%2d:%2d.%6d%c%2d:%2d',
|
2020-12-06 17:19:39 +03:00
|
|
|
&year, &month, &day, charptr(&time_char), &hour, &minute, &second, &mic_second, charptr(&plus_min_z),
|
|
|
|
&offset_hour, &offset_min)}
|
2020-12-08 19:53:54 +03:00
|
|
|
// Missread microsec ([Year Month Day T Sec Hour Minute].len == 7 < 8)
|
|
|
|
if count < 8 {
|
|
|
|
count = unsafe {C.sscanf(charptr(s.str), '%4d-%2d-%2d%c%2d:%2d:%2d%c%2d:%2d',
|
|
|
|
&year, &month, &day, charptr(&time_char), &hour, &minute, &second, charptr(&plus_min_z),
|
|
|
|
&offset_hour, &offset_min)}
|
|
|
|
count++ // Increment count because skipped microsec
|
|
|
|
}
|
2020-11-29 11:38:14 +03:00
|
|
|
is_local_time := plus_min_z == `a` && count == 8
|
|
|
|
is_utc := plus_min_z == `Z` && count == 9
|
|
|
|
if count != 11 && !is_local_time && !is_utc {
|
2020-06-07 16:19:09 +03:00
|
|
|
return error('Invalid 8601 format')
|
|
|
|
}
|
|
|
|
if time_char != `T` && time_char != ` ` {
|
|
|
|
return error('Invalid 8601 format, expected space or `T` as time separator')
|
|
|
|
}
|
2020-11-29 11:38:14 +03:00
|
|
|
if plus_min_z != `+` && plus_min_z != `-` && plus_min_z != `Z` && !is_local_time {
|
|
|
|
return error('Invalid 8601 format, expected `Z` or `+` or `-` as time separator')
|
2020-06-07 16:19:09 +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
|
|
|
|
hour: hour
|
|
|
|
minute: minute
|
|
|
|
second: second
|
|
|
|
microsecond: mic_second
|
|
|
|
})
|
2020-12-09 16:48:21 +03:00
|
|
|
if is_utc {
|
|
|
|
return t
|
|
|
|
}
|
2020-11-29 11:38:14 +03:00
|
|
|
if is_local_time {
|
|
|
|
return to_local_time(t)
|
|
|
|
}
|
2020-10-08 17:59:43 +03:00
|
|
|
mut unix_time := t.unix
|
2020-06-07 16:19:09 +03:00
|
|
|
mut unix_offset := int(0)
|
|
|
|
if offset_hour > 0 {
|
2020-10-08 17:59:43 +03:00
|
|
|
unix_offset += 3600 * offset_hour
|
2020-06-07 16:19:09 +03:00
|
|
|
}
|
|
|
|
if offset_min > 0 {
|
2020-10-08 17:59:43 +03:00
|
|
|
unix_offset += 60 * offset_min
|
2020-06-07 16:19:09 +03:00
|
|
|
}
|
|
|
|
if unix_offset != 0 {
|
2020-11-29 11:38:14 +03:00
|
|
|
if plus_min_z == `+` {
|
2020-06-10 12:14:55 +03:00
|
|
|
unix_time -= u64(unix_offset)
|
2020-06-07 16:19:09 +03:00
|
|
|
} else {
|
2020-06-10 12:14:55 +03:00
|
|
|
unix_time += u64(unix_offset)
|
2020-06-07 16:19:09 +03:00
|
|
|
}
|
2020-06-10 12:14:55 +03:00
|
|
|
t = unix2(int(unix_time), t.microsecond)
|
2020-06-07 16:19:09 +03:00
|
|
|
}
|
2020-06-10 12:14:55 +03:00
|
|
|
// Convert the time to local time
|
|
|
|
return to_local_time(t)
|
2020-06-07 16:19:09 +03:00
|
|
|
}
|