melib/datetime: convert date to utc before converting to unix epoch
parent
86d8419ce7
commit
6835968d9a
|
@ -74,25 +74,165 @@ pub fn timestamp_to_string(timestamp: UnixTimestamp, fmt: Option<&str>) -> Strin
|
|||
s.to_string_lossy().to_string()
|
||||
}
|
||||
|
||||
fn tm_to_secs(tm: ::libc::tm) -> UnixTimestamp {
|
||||
let mut is_leap = false;
|
||||
let mut year = tm.tm_year;
|
||||
let mut month = tm.tm_mon;
|
||||
if month >= 12 || month < 0 {
|
||||
let mut adj = month / 12;
|
||||
month %= 12;
|
||||
if month < 0 {
|
||||
adj -= 1;
|
||||
month += 12;
|
||||
}
|
||||
year += adj;
|
||||
}
|
||||
let mut t = year_to_secs(year as u64, &mut is_leap);
|
||||
t += month_to_secs(month as usize, is_leap);
|
||||
t += 86400 * (tm.tm_mday as u64 - 1);
|
||||
t += 3600 * (tm.tm_hour as u64);
|
||||
t += 60 * (tm.tm_min as u64);
|
||||
t += tm.tm_sec as u64;
|
||||
t
|
||||
}
|
||||
|
||||
fn year_to_secs(year: u64, is_leap: &mut bool) -> u64 {
|
||||
if year < 100 {
|
||||
/* Sorry time travelers. */
|
||||
return 0;
|
||||
}
|
||||
|
||||
if year - 2 <= 136 {
|
||||
let y = year;
|
||||
let mut leaps = (y - 68) >> 2;
|
||||
if (y - 68) & 3 == 0 {
|
||||
leaps -= 1;
|
||||
*is_leap = true;
|
||||
} else {
|
||||
*is_leap = false;
|
||||
}
|
||||
return 31536000 * (y - 70) + 86400 * leaps;
|
||||
}
|
||||
|
||||
let cycles = (year - 100) / 400;
|
||||
let centuries;
|
||||
let mut leaps;
|
||||
let mut rem;
|
||||
|
||||
rem = (year - 100) % 400;
|
||||
|
||||
if rem == 0 {
|
||||
*is_leap = true;
|
||||
centuries = 0;
|
||||
leaps = 0;
|
||||
} else {
|
||||
if rem >= 200 {
|
||||
if rem >= 300 {
|
||||
centuries = 3;
|
||||
rem -= 300;
|
||||
} else {
|
||||
centuries = 2;
|
||||
rem -= 200;
|
||||
}
|
||||
} else {
|
||||
if rem >= 100 {
|
||||
centuries = 1;
|
||||
rem -= 100;
|
||||
} else {
|
||||
centuries = 0;
|
||||
}
|
||||
}
|
||||
if rem == 0 {
|
||||
*is_leap = false;
|
||||
leaps = 0;
|
||||
} else {
|
||||
leaps = rem / 4;
|
||||
rem %= 4;
|
||||
*is_leap = rem == 0;
|
||||
}
|
||||
}
|
||||
|
||||
leaps += 97 * cycles + 24 * centuries - if *is_leap { 1 } else { 0 };
|
||||
|
||||
return (year - 100) * 31536000 + leaps * 86400 + 946684800 + 86400;
|
||||
}
|
||||
|
||||
fn month_to_secs(month: usize, is_leap: bool) -> u64 {
|
||||
const SECS_THROUGH_MONTH: [u64; 12] = [
|
||||
0,
|
||||
31 * 86400,
|
||||
59 * 86400,
|
||||
90 * 86400,
|
||||
120 * 86400,
|
||||
151 * 86400,
|
||||
181 * 86400,
|
||||
212 * 86400,
|
||||
243 * 86400,
|
||||
273 * 86400,
|
||||
304 * 86400,
|
||||
334 * 86400,
|
||||
];
|
||||
let mut t = SECS_THROUGH_MONTH[month as usize];
|
||||
if is_leap && month >= 2 {
|
||||
t += 86400;
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
pub fn rfc822_to_timestamp<T>(s: T) -> UnixTimestamp
|
||||
where
|
||||
T: Into<Vec<u8>>,
|
||||
{
|
||||
let s = CString::new(s).unwrap();
|
||||
let mut new_tm: ::libc::tm = unsafe { std::mem::zeroed() };
|
||||
unsafe {
|
||||
let fmt = CStr::from_bytes_with_nul_unchecked(b"%a, %e %h %Y %H:%M:%S %z\0");
|
||||
let ret = strptime(
|
||||
CString::new(s).unwrap().as_ptr(),
|
||||
fmt.as_ptr(),
|
||||
&mut new_tm as *mut _,
|
||||
);
|
||||
if ret.is_null() {
|
||||
return 0;
|
||||
for fmt in &[
|
||||
&b"%a, %e %h %Y %H:%M:%S \0"[..],
|
||||
&b"%e %h %Y %H:%M:%S \0"[..],
|
||||
] {
|
||||
unsafe {
|
||||
let fmt = CStr::from_bytes_with_nul_unchecked(fmt);
|
||||
let ret = strptime(s.as_ptr(), fmt.as_ptr(), &mut new_tm as *mut _);
|
||||
if ret.is_null() {
|
||||
continue;
|
||||
}
|
||||
let rest = CStr::from_ptr(ret);
|
||||
let tm_gmtoff = if rest.to_bytes().len() > 4
|
||||
&& rest.to_bytes().is_ascii()
|
||||
&& rest.to_bytes()[1..5].iter().all(u8::is_ascii_digit)
|
||||
{
|
||||
let offset = std::str::from_utf8_unchecked(&rest.to_bytes()[0..5]);
|
||||
if let (Ok(mut hr_offset), Ok(mut min_offset)) =
|
||||
(offset[1..3].parse::<i64>(), offset[3..5].parse::<i64>())
|
||||
{
|
||||
if rest.to_bytes()[0] == b'-' {
|
||||
hr_offset = -hr_offset;
|
||||
min_offset = -min_offset;
|
||||
}
|
||||
hr_offset * 60 * 60 + min_offset * 60
|
||||
} else {
|
||||
0
|
||||
}
|
||||
} else {
|
||||
let rest = if rest.to_bytes().starts_with(b"(") && rest.to_bytes().ends_with(b")") {
|
||||
&rest.to_bytes()[1..rest.to_bytes().len() - 1]
|
||||
} else {
|
||||
rest.to_bytes()
|
||||
};
|
||||
|
||||
if let Ok(idx) = TIMEZONE_ABBR.binary_search_by(|probe| probe.0.cmp(rest)) {
|
||||
let (hr_offset, min_offset) = debug!(TIMEZONE_ABBR[idx]).1;
|
||||
(hr_offset as i64) * 60 * 60 + (min_offset as i64) * 60
|
||||
} else {
|
||||
0
|
||||
}
|
||||
};
|
||||
return (tm_to_secs(new_tm) as i64 - tm_gmtoff) as u64;
|
||||
}
|
||||
return mktime(&new_tm as *const _) as u64;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// FIXME: Handle non-local timezone?
|
||||
pub fn timestamp_from_string<T>(s: T, fmt: &str) -> Option<UnixTimestamp>
|
||||
where
|
||||
T: Into<Vec<u8>>,
|
||||
|
@ -127,5 +267,301 @@ pub fn now() -> UnixTimestamp {
|
|||
|
||||
#[test]
|
||||
fn test_timestamp() {
|
||||
timestamp_to_string(0);
|
||||
timestamp_to_string(0, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rfcs() {
|
||||
/* Some tests were lazily stolen from https://rachelbythebay.com/w/2013/06/11/time/ */
|
||||
|
||||
assert_eq!(
|
||||
rfc822_to_timestamp("Wed, 8 Jan 2020 10:44:03 -0800"),
|
||||
1578509043
|
||||
);
|
||||
|
||||
/*
|
||||
macro_rules! mkt {
|
||||
($year:literal, $month:literal, $day:literal, $hour:literal, $minute:literal, $second:literal) => {
|
||||
::libc::tm {
|
||||
tm_sec: $second,
|
||||
tm_min: $minute,
|
||||
tm_hour: $hour,
|
||||
tm_mday: $day,
|
||||
tm_mon: $month - 1,
|
||||
tm_year: $year - 1900,
|
||||
tm_wday: 0,
|
||||
tm_yday: 0,
|
||||
tm_isdst: 0,
|
||||
tm_gmtoff: 0,
|
||||
tm_zone: std::ptr::null(),
|
||||
}
|
||||
};
|
||||
}
|
||||
*/
|
||||
//unsafe { __tm_to_secs(&mkt!(2009, 02, 13, 23, 31, 30) as *const _) },
|
||||
assert_eq!(
|
||||
rfc822_to_timestamp("Fri, 13 Feb 2009 15:31:30 -0800"),
|
||||
1234567890
|
||||
);
|
||||
|
||||
//unsafe { __tm_to_secs(&mkt!(2931, 05, 05, 00, 33, 09) as *const _) },
|
||||
assert_eq!(
|
||||
rfc822_to_timestamp("Sat, 05 May 2931 00:33:09 +0000"),
|
||||
30336942789
|
||||
);
|
||||
//2214-11-06 20:05:12 = 7726651512 [OK]
|
||||
assert_eq!(
|
||||
rfc822_to_timestamp("Sun, 06 Nov 2214 17:05:12 -0300"), //2214-11-06 20:05:12
|
||||
7726651512
|
||||
);
|
||||
assert_eq!(
|
||||
rfc822_to_timestamp("Sun, 06 Nov 2214 17:05:12 -0300"), //2214-11-06 20:05:12
|
||||
rfc822_to_timestamp("Sun, 06 Nov 2214 17:05:12 (ADT)"), //2214-11-06 20:05:12
|
||||
);
|
||||
//2661-11-06 06:38:02 = 21832612682 [OK]
|
||||
assert_eq!(
|
||||
rfc822_to_timestamp("Wed, 06 Nov 2661 06:38:02 +0000"), //2661-11-06 06:38:02
|
||||
21832612682
|
||||
);
|
||||
//2508-12-09 04:27:08 = 17007251228 [OK]
|
||||
assert_eq!(
|
||||
rfc822_to_timestamp("Sun, 09 Dec 2508 04:27:08 +0000"), //2508-12-09 04:27:08
|
||||
17007251228
|
||||
);
|
||||
//2375-11-07 05:08:24 = 12807349704 [OK]
|
||||
assert_eq!(
|
||||
rfc822_to_timestamp("Fri, 07 Nov 2375 05:08:24 +0000"), //2375-11-07 05:08:24
|
||||
12807349704
|
||||
);
|
||||
//2832-09-03 02:46:10 = 27223353970 [OK]
|
||||
assert_eq!(
|
||||
rfc822_to_timestamp("Fri, 03 Sep 2832 02:46:10 +0000"), //2832-09-03 02:46:10
|
||||
27223353970
|
||||
);
|
||||
//2983-02-25 12:47:17 = 31972020437 [OK]
|
||||
assert_eq!(
|
||||
rfc822_to_timestamp("Tue, 25 Feb 2983 15:47:17 +0300"), //2983-02-25 12:47:17
|
||||
31972020437
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
rfc822_to_timestamp("Thu, 30 Mar 2017 17:32:06 +0300 (EEST)"),
|
||||
1490884326
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
rfc822_to_timestamp("Mon, 24 Apr 2017 17:36:34 +0530"),
|
||||
1493035594
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
rfc822_to_timestamp("Mon, 24 Apr 2017 17:36:34 +0530"),
|
||||
rfc822_to_timestamp("Mon, 24 Apr 2017 12:06:34 +0000"),
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
rfc822_to_timestamp("Mon, 24 Apr 2017 17:36:34 +0530"),
|
||||
rfc822_to_timestamp("Mon, 24 Apr 2017 17:36:34 (SLST)"),
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
rfc822_to_timestamp("Mon, 24 Apr 2017 17:36:34 +0530"),
|
||||
rfc822_to_timestamp("Mon, 24 Apr 2017 17:36:34 SLST"),
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
rfc822_to_timestamp("27 Dec 2019 14:42:46 +0100"),
|
||||
1577454166
|
||||
);
|
||||
}
|
||||
const TIMEZONE_ABBR: &'static [(&'static [u8], (i8, i8))] = &[
|
||||
(b"ACDT", (10, 30)),
|
||||
(b"ACST", (09, 30)),
|
||||
(b"ACT", (-05, 0)),
|
||||
(b"ACWST", (08, 45)),
|
||||
(b"ADT", (-03, 0)),
|
||||
(b"AEDT", (11, 0)),
|
||||
(b"AEST", (10, 0)),
|
||||
(b"AFT", (04, 30)),
|
||||
(b"AKDT", (-08, 0)),
|
||||
(b"AKST", (-09, 0)),
|
||||
(b"ALMT", (06, 0)),
|
||||
(b"AMST", (-03, 0)),
|
||||
(b"AMT", (-04, 0)), /* Amazon Time */
|
||||
(b"ANAT", (12, 0)),
|
||||
(b"AQTT", (05, 0)),
|
||||
(b"ART", (-03, 0)),
|
||||
(b"AST", (-04, 0)),
|
||||
(b"AST", (03, 0)),
|
||||
(b"AWST", (08, 0)),
|
||||
(b"AZOST", (0, 0)),
|
||||
(b"AZOT", (-01, 0)),
|
||||
(b"AZT", (04, 0)),
|
||||
(b"BDT", (08, 0)),
|
||||
(b"BIOT", (06, 0)),
|
||||
(b"BIT", (-12, 0)),
|
||||
(b"BOT", (-04, 0)),
|
||||
(b"BRST", (-02, 0)),
|
||||
(b"BRT", (-03, 0)),
|
||||
(b"BST", (06, 0)),
|
||||
(b"BTT", (06, 0)),
|
||||
(b"CAT", (02, 0)),
|
||||
(b"CCT", (06, 30)),
|
||||
(b"CDT", (-05, 0)),
|
||||
(b"CEST", (02, 0)),
|
||||
(b"CET", (01, 0)),
|
||||
(b"CHADT", (13, 45)),
|
||||
(b"CHAST", (12, 45)),
|
||||
(b"CHOST", (09, 0)),
|
||||
(b"CHOT", (08, 0)),
|
||||
(b"CHST", (10, 0)),
|
||||
(b"CHUT", (10, 0)),
|
||||
(b"CIST", (-08, 0)),
|
||||
(b"CIT", (08, 0)),
|
||||
(b"CKT", (-10, 0)),
|
||||
(b"CLST", (-03, 0)),
|
||||
(b"CLT", (-04, 0)),
|
||||
(b"COST", (-04, 0)),
|
||||
(b"COT", (-05, 0)),
|
||||
(b"CST", (-06, 0)),
|
||||
(b"CT", (08, 0)),
|
||||
(b"CVT", (-01, 0)),
|
||||
(b"CWST", (08, 45)),
|
||||
(b"CXT", (07, 0)),
|
||||
(b"DAVT", (07, 0)),
|
||||
(b"DDUT", (10, 0)),
|
||||
(b"DFT", (01, 0)),
|
||||
(b"EASST", (-05, 0)),
|
||||
(b"EAST", (-06, 0)),
|
||||
(b"EAT", (03, 0)),
|
||||
(b"ECT", (-05, 0)),
|
||||
(b"EDT", (-04, 0)),
|
||||
(b"EEST", (03, 0)),
|
||||
(b"EET", (02, 0)),
|
||||
(b"EGST", (0, 0)),
|
||||
(b"EGT", (-01, 0)),
|
||||
(b"EIT", (09, 0)),
|
||||
(b"EST", (-05, 0)),
|
||||
(b"FET", (03, 0)),
|
||||
(b"FJT", (12, 0)),
|
||||
(b"FKST", (-03, 0)),
|
||||
(b"FKT", (-04, 0)),
|
||||
(b"FNT", (-02, 0)),
|
||||
(b"GALT", (-06, 0)),
|
||||
(b"GAMT", (-09, 0)),
|
||||
(b"GET", (04, 0)),
|
||||
(b"GFT", (-03, 0)),
|
||||
(b"GILT", (12, 0)),
|
||||
(b"GIT", (-09, 0)),
|
||||
(b"GMT", (0, 0)),
|
||||
(b"GST", (04, 0)),
|
||||
(b"GYT", (-04, 0)),
|
||||
(b"HAEC", (02, 0)),
|
||||
(b"HDT", (-09, 0)),
|
||||
(b"HKT", (08, 0)),
|
||||
(b"HMT", (05, 0)),
|
||||
(b"HOVST", (08, 0)),
|
||||
(b"HOVT", (07, 0)),
|
||||
(b"HST", (-10, 0)),
|
||||
(b"ICT", (07, 0)),
|
||||
(b"IDLW", (-12, 0)),
|
||||
(b"IDT", (03, 0)),
|
||||
(b"IOT", (03, 0)),
|
||||
(b"IRDT", (04, 30)),
|
||||
(b"IRKT", (08, 0)),
|
||||
(b"IRST", (03, 30)),
|
||||
(b"IST", (05, 30)),
|
||||
(b"JST", (09, 0)),
|
||||
(b"KALT", (02, 0)),
|
||||
(b"KGT", (06, 0)),
|
||||
(b"KOST", (11, 0)),
|
||||
(b"KRAT", (07, 0)),
|
||||
(b"KST", (09, 0)),
|
||||
(b"LHST", (10, 30)),
|
||||
(b"LINT", (14, 0)),
|
||||
(b"MAGT", (12, 0)),
|
||||
(b"MART", (-09, -30)),
|
||||
(b"MAWT", (05, 0)),
|
||||
(b"MDT", (-06, 0)),
|
||||
(b"MEST", (02, 0)),
|
||||
(b"MET", (01, 0)),
|
||||
(b"MHT", (12, 0)),
|
||||
(b"MIST", (11, 0)),
|
||||
(b"MIT", (-09, -30)),
|
||||
(b"MMT", (06, 30)),
|
||||
(b"MSK", (03, 0)),
|
||||
(b"MST", (08, 0)),
|
||||
(b"MUT", (04, 0)),
|
||||
(b"MVT", (05, 0)),
|
||||
(b"MYT", (08, 0)),
|
||||
(b"NCT", (11, 0)),
|
||||
(b"NDT", (-02, -30)),
|
||||
(b"NFT", (11, 0)),
|
||||
(b"NOVT", (07, 0)),
|
||||
(b"NPT", (05, 45)),
|
||||
(b"NST", (-03, -30)),
|
||||
(b"NT", (-03, -30)),
|
||||
(b"NUT", (-11, 0)),
|
||||
(b"NZDT", (13, 0)),
|
||||
(b"NZST", (12, 0)),
|
||||
(b"OMST", (06, 0)),
|
||||
(b"ORAT", (05, 0)),
|
||||
(b"PDT", (-07, 0)),
|
||||
(b"PET", (-05, 0)),
|
||||
(b"PETT", (12, 0)),
|
||||
(b"PGT", (10, 0)),
|
||||
(b"PHOT", (13, 0)),
|
||||
(b"PHT", (08, 0)),
|
||||
(b"PKT", (05, 0)),
|
||||
(b"PMDT", (-02, 0)),
|
||||
(b"PMST", (-03, 0)),
|
||||
(b"PONT", (11, 0)),
|
||||
(b"PST", (-08, 0)),
|
||||
(b"PST", (08, 0)),
|
||||
(b"PYST", (-03, 0)),
|
||||
(b"PYT", (-04, 0)),
|
||||
(b"RET", (04, 0)),
|
||||
(b"ROTT", (-03, 0)),
|
||||
(b"SAKT", (11, 0)),
|
||||
(b"SAMT", (04, 0)),
|
||||
(b"SAST", (02, 0)),
|
||||
(b"SBT", (11, 0)),
|
||||
(b"SCT", (04, 0)),
|
||||
(b"SDT", (-10, 0)),
|
||||
(b"SGT", (08, 0)),
|
||||
(b"SLST", (05, 30)),
|
||||
(b"SRET", (11, 0)),
|
||||
(b"SRT", (-03, 0)),
|
||||
(b"SST", (08, 0)),
|
||||
(b"SYOT", (03, 0)),
|
||||
(b"TAHT", (-10, 0)),
|
||||
(b"TFT", (05, 0)),
|
||||
(b"THA", (07, 0)),
|
||||
(b"TJT", (05, 0)),
|
||||
(b"TKT", (13, 0)),
|
||||
(b"TLT", (09, 0)),
|
||||
(b"TMT", (05, 0)),
|
||||
(b"TOT", (13, 0)),
|
||||
(b"TRT", (03, 0)),
|
||||
(b"TVT", (12, 0)),
|
||||
(b"ULAST", (09, 0)),
|
||||
(b"ULAT", (08, 0)),
|
||||
(b"UTC", (0, 0)),
|
||||
(b"UYST", (-02, 0)),
|
||||
(b"UYT", (-03, 0)),
|
||||
(b"UZT", (05, 0)),
|
||||
(b"VET", (-04, 0)),
|
||||
(b"VLAT", (10, 0)),
|
||||
(b"VOLT", (04, 0)),
|
||||
(b"VOST", (06, 0)),
|
||||
(b"VUT", (11, 0)),
|
||||
(b"WAKT", (12, 0)),
|
||||
(b"WAST", (02, 0)),
|
||||
(b"WAT", (01, 0)),
|
||||
(b"WEST", (01, 0)),
|
||||
(b"WET", (0, 0)),
|
||||
(b"WIT", (07, 0)),
|
||||
(b"WST", (08, 0)),
|
||||
(b"YAKT", (09, 0)),
|
||||
(b"YEKT", (05, 0)),
|
||||
];
|
||||
|
|
Loading…
Reference in New Issue