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 {
|
|
|
|
pos := s.index(' ') or {
|
|
|
|
return error('Invalid time format: $s')
|
|
|
|
}
|
|
|
|
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')
|
|
|
|
}
|
|
|
|
pos := months_string.index(fields[2]) or {
|
|
|
|
return error('Invalid time format: $s')
|
|
|
|
}
|
|
|
|
mm := pos / 3 + 1
|
|
|
|
mut tmstr := byteptr(0)
|
|
|
|
unsafe { tmstr = malloc(s.len * 2) }
|
|
|
|
count := C.sprintf(charptr(tmstr), '%s-%02d-%s %s'.str, fields[3].str, mm,
|
|
|
|
fields[1].str, fields[4].str)
|
|
|
|
|
|
|
|
t := parse(tos(tmstr, count)) or {
|
|
|
|
// FIXME Remove this when optional forwarding is fixed.
|
|
|
|
return error('Invalid time format: $s')
|
|
|
|
}
|
|
|
|
|
|
|
|
return t
|
|
|
|
}
|