chj_rustbin/time/
excel.rs

1//! Translate between unixtime and Excel date-time values (days since
2//! Excel's epoch)
3
4const DAYS_AT_EPOCH: f64 = 25569.;
5
6/// offset_hours: `1.0` represents `+01:00`.
7pub fn exceldays_from_unixtime(unixtime: f64, offset_hours: f64) -> f64 {
8    unixtime / 86400. + (DAYS_AT_EPOCH + offset_hours / 24.)
9}
10
11/// offset_hours: `1.0` represents `+01:00`.
12pub fn unixtime_from_exceldays(exceldays: f64, offset_hours: f64) -> f64 {
13    (exceldays - (DAYS_AT_EPOCH + offset_hours / 24.)) * 86400.
14}
15
16#[cfg(test)]
17mod tests {
18    use super::*;
19    use approx::assert_relative_eq;
20
21    #[test]
22    fn t_() {
23        fn t(ut: f64, et: f64) {
24            assert_relative_eq!(exceldays_from_unixtime(ut, 0.), et);
25            assert_relative_eq!(ut, unixtime_from_exceldays(et, 0.));
26            assert_relative_eq!(exceldays_from_unixtime(ut, 1.), et + 1. / 24.);
27            assert_relative_eq!(ut, unixtime_from_exceldays(et + 1. / 24., 1.));
28        }
29        t(1538352000., 43374.);
30    }
31}