| 1 | use std::ops; |
| 2 | |
| 3 | use crate::core::{duration_from_midnight, Tz}; |
| 4 | use chrono::{NaiveDate, NaiveTime, Utc}; |
| 5 | |
| 6 | const DAY_SECS: i64 = 24 * 60 * 60; |
| 7 | |
| 8 | /// Converts number of days since unix epoch to a (naive) date. |
| 9 | pub(crate) fn date_from_ordinal(ordinal: i64) -> NaiveDate { |
| 10 | chrono::DateTime::<Utc>::from_timestamp(ordinal * DAY_SECS, 0) |
| 11 | .unwrap() |
| 12 | .date_naive() |
| 13 | } |
| 14 | |
| 15 | /// Returns number of days since unix epoch (rounded down) |
| 16 | pub(crate) fn days_since_unix_epoch(date: &chrono::DateTime<Utc>) -> i64 { |
| 17 | date.timestamp() / DAY_SECS |
| 18 | } |
| 19 | |
| 20 | /// Returns true if given year is a leap year |
| 21 | pub(crate) fn is_leap_year(year: i32) -> bool { |
| 22 | // Every 4 years, and every 100 years, |
| 23 | // but not if dividable by 400. |
| 24 | year.trailing_zeros() >= 2 && (year % 25 != 0 || year.trailing_zeros() >= 4) |
| 25 | } |
| 26 | |
| 27 | /// Returns number of days in year, |
| 28 | /// So 365 or 366 depending on the year |
| 29 | pub(crate) fn get_year_len(year: i32) -> u16 { |
| 30 | if is_leap_year(year) { |
| 31 | 366 |
| 32 | } else { |
| 33 | 365 |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | pub(crate) trait DifferentSigns { |
| 38 | fn different_sign(a: Self, b: Self) -> bool; |
| 39 | } |
| 40 | |
| 41 | macro_rules! impl_different_signs { |
| 42 | ($($ty:ty),*) => { |
| 43 | $( |
| 44 | impl DifferentSigns for $ty { |
| 45 | fn different_sign(a: Self, b: Self) -> bool { |
| 46 | a > 0 && b < 0 || a < 0 && b > 0 |
| 47 | } |
| 48 | })* |
| 49 | }; |
| 50 | (@unsigned, $($ty:ty),*) => { |
| 51 | $( |
| 52 | impl DifferentSigns for $ty { |
| 53 | fn different_sign(_: Self, _: Self) -> bool { |
| 54 | false |
| 55 | } |
| 56 | })* |
| 57 | }; |
| 58 | } |
| 59 | impl_different_signs!(isize, i32, i16); |
| 60 | impl_different_signs!(@unsigned, usize, u32, u16, u8); |
| 61 | |
| 62 | pub(crate) fn pymod<T>(a: T, b: T) -> T |
| 63 | where |
| 64 | T: DifferentSigns + Copy + ops::Rem<Output = T> + ops::Add<Output = T>, |
| 65 | { |
| 66 | let r = a % b; |
| 67 | // If r and b differ in sign, add b to wrap the result to the correct sign. |
| 68 | if T::different_sign(r, b) { |
| 69 | r + b |
| 70 | } else { |
| 71 | r |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | pub(crate) fn add_time_to_date( |
| 76 | tz: Tz, |
| 77 | date: NaiveDate, |
| 78 | time: NaiveTime, |
| 79 | ) -> Option<chrono::DateTime<Tz>> { |
| 80 | if let Some(dt) = date.and_time(time).and_local_timezone(tz).single() { |
| 81 | return Some(dt); |
| 82 | } |
| 83 | // If the day is a daylight saving time, the above code might not work, and we |
| 84 | // can try to get a valid datetime by adding the `time` as a duration instead. |
| 85 | let dt = date.and_hms_opt(0, 0, 0)?.and_local_timezone(tz).single()?; |
| 86 | let day_duration = duration_from_midnight(time); |
| 87 | dt.checked_add_signed(day_duration) |
| 88 | } |
| 89 | |
| 90 | #[cfg(test)] |
| 91 | mod test { |
| 92 | |
| 93 | use chrono::{Duration, TimeZone}; |
| 94 | |
| 95 | use super::*; |
| 96 | |
| 97 | #[test] |
| 98 | fn naive_date_from_ordinal() { |
| 99 | let tests = [ |
| 100 | (-1, NaiveDate::from_ymd_opt(1969, 12, 31).unwrap()), |
| 101 | (0, NaiveDate::from_ymd_opt(1970, 1, 1).unwrap()), |
| 102 | (1, NaiveDate::from_ymd_opt(1970, 1, 2).unwrap()), |
| 103 | (10, NaiveDate::from_ymd_opt(1970, 1, 11).unwrap()), |
| 104 | (365, NaiveDate::from_ymd_opt(1971, 1, 1).unwrap()), |
| 105 | (19877, NaiveDate::from_ymd_opt(2024, 6, 3).unwrap()), |
| 106 | ]; |
| 107 | |
| 108 | for (days, expected) in tests { |
| 109 | assert_eq!(date_from_ordinal(days), expected, "seconds: {}", days); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | #[test] |
| 114 | fn python_mod() { |
| 115 | assert_eq!(pymod(2, -3), -1); |
| 116 | assert_eq!(pymod(-2, 3), 1); |
| 117 | assert_eq!(pymod(-2, -3), -2); |
| 118 | assert_eq!(pymod(-3, -3), 0); |
| 119 | assert_eq!(pymod(0, 3), 0); |
| 120 | assert_eq!(pymod(1, 3), 1); |
| 121 | assert_eq!(pymod(2, 3), 2); |
| 122 | assert_eq!(pymod(3, 3), 0); |
| 123 | assert_eq!(pymod(4, 3), 1); |
| 124 | assert_eq!(pymod(6, 3), 0); |
| 125 | assert_eq!(pymod(-6, 3), 0); |
| 126 | assert_eq!(pymod(-6, -3), 0); |
| 127 | assert_eq!(pymod(6, -3), 0); |
| 128 | } |
| 129 | |
| 130 | #[test] |
| 131 | fn leap_year() { |
| 132 | let tests = [ |
| 133 | (2015, false), |
| 134 | (2016, true), |
| 135 | (2017, false), |
| 136 | (2018, false), |
| 137 | (2019, false), |
| 138 | (2020, true), |
| 139 | (2021, false), |
| 140 | ]; |
| 141 | |
| 142 | for (year, expected_output) in tests { |
| 143 | let res = is_leap_year(year); |
| 144 | assert_eq!(res, expected_output); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | #[test] |
| 149 | fn year_length() { |
| 150 | let tests = [(2015, 365), (2016, 366)]; |
| 151 | |
| 152 | for (year, expected_output) in tests { |
| 153 | let res = get_year_len(year); |
| 154 | assert_eq!(res, expected_output); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | #[test] |
| 159 | fn adds_time_to_date() { |
| 160 | let tests = [ |
| 161 | ( |
| 162 | Tz::UTC, |
| 163 | NaiveDate::from_ymd_opt(2017, 1, 1).unwrap(), |
| 164 | NaiveTime::from_hms_opt(1, 15, 30).unwrap(), |
| 165 | Some(Tz::UTC.with_ymd_and_hms(2017, 1, 1, 1, 15, 30).unwrap()), |
| 166 | ), |
| 167 | ( |
| 168 | Tz::America__Vancouver, |
| 169 | NaiveDate::from_ymd_opt(2021, 3, 14).unwrap(), |
| 170 | NaiveTime::from_hms_opt(2, 22, 10).unwrap(), |
| 171 | Some( |
| 172 | Tz::America__Vancouver |
| 173 | .with_ymd_and_hms(2021, 3, 14, 0, 0, 0) |
| 174 | .unwrap() |
| 175 | + Duration::hours(2) |
| 176 | + Duration::minutes(22) |
| 177 | + Duration::seconds(10), |
| 178 | ), |
| 179 | ), |
| 180 | ( |
| 181 | Tz::America__New_York, |
| 182 | NaiveDate::from_ymd_opt(1997, 10, 26).unwrap(), |
| 183 | NaiveTime::from_hms_opt(9, 0, 0).unwrap(), |
| 184 | Some( |
| 185 | Tz::America__New_York |
| 186 | .with_ymd_and_hms(1997, 10, 26, 9, 0, 0) |
| 187 | .unwrap(), |
| 188 | ), |
| 189 | ), |
| 190 | ]; |
| 191 | |
| 192 | for (tz, date, time, expected_output) in tests { |
| 193 | let res = add_time_to_date(tz, date, time); |
| 194 | assert_eq!(res, expected_output); |
| 195 | } |
| 196 | } |
| 197 | } |