utils.rs (1157B) - raw
1 use mail_parser::DateTime; 2 use std::fs::{read, write}; 3 use std::io::prelude::*; 4 use std::path::PathBuf; 5 6 // from https://github.com/protocolbuffers/upb/blob/22182e6e/upb/json_decode.c#L982-L992 7 // TODO make rust-idiomatic 8 pub fn epoch_days(y: u32, m: u32, d: u32) -> i64 { 9 let year_base = 4800; 10 let m_adj = match m < 3 { 11 true => 0, 12 false => m - 3, 13 }; 14 let carry = match m_adj > m { 15 true => 1, 16 false => 0, 17 }; 18 let adjust = carry * 12; 19 let y_adj = m + year_base - carry; 20 let month_days = ((m_adj + adjust) * 62719 + 769) / 2048; 21 let leap_days = y_adj / 4 - y_adj / 100 + y_adj / 400; 22 y_adj as i64 * 365 + leap_days as i64 + month_days as i64 + (d as i64 - 1) - 2472632 23 } 24 25 // leap seconds? 26 // TODO test 27 pub fn epoch_time(dt: &DateTime) -> i64 { 28 let mut h = dt.hour as i64; 29 let mut m = dt.minute as i64; 30 let s = dt.second; 31 let adj = match dt.tz_before_gmt { 32 true => 1, 33 false => -1, 34 }; 35 h += dt.tz_hour as i64 * adj; 36 m += dt.tz_minute as i64 * adj; 37 38 return epoch_days(dt.year, dt.month, dt.day) * 86400 + h * 3600 + m * 60 + dt.second as i64; 39 }