From 5de034bd090b0a81d66b8d01312ae3b712aa821a Mon Sep 17 00:00:00 2001 From: "Guillaume W. Bres" Date: Sun, 28 May 2023 13:10:50 +0200 Subject: [PATCH] introducing week unit Signed-off-by: Guillaume W. Bres --- src/lib.rs | 2 ++ src/timeunits.rs | 24 +++++++++++++++++------- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 4f326037..51831319 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -37,6 +37,8 @@ pub const DAYS_PER_YEAR_NLD: f64 = 365.0; /// `DAYS_PER_CENTURY` corresponds to the number of days per century in the Julian calendar. pub const DAYS_PER_CENTURY: f64 = 36525.0; pub const DAYS_PER_CENTURY_I64: i64 = 36525; +pub const DAYS_PER_WEEK: f64 = 7.0; +pub const DAYS_PER_WEEK_I64: i64 = 7; /// `SECONDS_PER_MINUTE` defines the number of seconds per minute. pub const SECONDS_PER_MINUTE: f64 = 60.0; /// `SECONDS_PER_HOUR` defines the number of seconds per hour. diff --git a/src/timeunits.rs b/src/timeunits.rs index fc535b60..37befe51 100644 --- a/src/timeunits.rs +++ b/src/timeunits.rs @@ -17,9 +17,10 @@ use num_traits::Float; use pyo3::prelude::*; use crate::{ - Duration, DAYS_PER_CENTURY, NANOSECONDS_PER_CENTURY, NANOSECONDS_PER_DAY, NANOSECONDS_PER_HOUR, - NANOSECONDS_PER_MICROSECOND, NANOSECONDS_PER_MILLISECOND, NANOSECONDS_PER_MINUTE, - NANOSECONDS_PER_SECOND, SECONDS_PER_DAY, SECONDS_PER_HOUR, SECONDS_PER_MINUTE, + Duration, DAYS_PER_CENTURY, DAYS_PER_WEEK, DAYS_PER_WEEK_I64, NANOSECONDS_PER_CENTURY, + NANOSECONDS_PER_DAY, NANOSECONDS_PER_HOUR, NANOSECONDS_PER_MICROSECOND, + NANOSECONDS_PER_MILLISECOND, NANOSECONDS_PER_MINUTE, NANOSECONDS_PER_SECOND, SECONDS_PER_DAY, + SECONDS_PER_HOUR, SECONDS_PER_MINUTE, }; /// An Enum to perform time unit conversions. @@ -33,6 +34,7 @@ pub enum Unit { Minute, Hour, Day, + Week, /// 36525 days, is the number of days per century in the Julian calendar Century, } @@ -67,6 +69,9 @@ pub trait TimeUnits: Copy + Mul { fn centuries(self) -> Duration { self * Unit::Century } + fn weeks(self) -> Duration { + self * Unit::Week + } fn days(self) -> Duration { self * Unit::Day } @@ -158,6 +163,7 @@ impl Unit { pub fn in_seconds(&self) -> f64 { match self { Unit::Century => DAYS_PER_CENTURY * SECONDS_PER_DAY, + Unit::Week => DAYS_PER_WEEK * SECONDS_PER_DAY, Unit::Day => SECONDS_PER_DAY, Unit::Hour => SECONDS_PER_HOUR, Unit::Minute => SECONDS_PER_MINUTE, @@ -200,7 +206,8 @@ impl From for u8 { Unit::Minute => 4, Unit::Hour => 5, Unit::Day => 6, - Unit::Century => 7, + Unit::Week => 7, + Unit::Century => 8, Unit::Second => 0, } } @@ -222,7 +229,8 @@ impl From for Unit { 4 => Unit::Minute, 5 => Unit::Hour, 6 => Unit::Day, - 7 => Unit::Century, + 7 => Unit::Week, + 8 => Unit::Century, _ => Unit::Second, } } @@ -236,6 +244,7 @@ impl Mul for Unit { fn mul(self, q: i64) -> Duration { let factor = match self { Unit::Century => NANOSECONDS_PER_CENTURY as i64, + Unit::Week => NANOSECONDS_PER_DAY as i64 * DAYS_PER_WEEK_I64, Unit::Day => NANOSECONDS_PER_DAY as i64, Unit::Hour => NANOSECONDS_PER_HOUR as i64, Unit::Minute => NANOSECONDS_PER_MINUTE as i64, @@ -276,6 +285,7 @@ impl Mul for Unit { fn mul(self, q: f64) -> Duration { let factor = match self { Unit::Century => NANOSECONDS_PER_CENTURY as f64, + Unit::Week => NANOSECONDS_PER_DAY as f64 * DAYS_PER_WEEK, Unit::Day => NANOSECONDS_PER_DAY as f64, Unit::Hour => NANOSECONDS_PER_HOUR as f64, Unit::Minute => NANOSECONDS_PER_MINUTE as f64, @@ -306,8 +316,8 @@ fn test_unit_conversion() { for unit_u8 in 0..u8::MAX { let unit = Unit::from(unit_u8); let unit_u8_back: u8 = unit.into(); - // If the u8 is greater than 8, it isn't valid and necessarily encoded as Second. - if unit_u8 < 8 { + // If the u8 is greater than 9, it isn't valid and necessarily encoded as Second. + if unit_u8 < 9 { assert_eq!(unit_u8_back, unit_u8, "got {unit_u8_back} want {unit_u8}"); } else { assert_eq!(unit, Unit::Second);