Skip to content

Commit

Permalink
introducing week unit
Browse files Browse the repository at this point in the history
Signed-off-by: Guillaume W. Bres <[email protected]>
  • Loading branch information
gwbres committed May 28, 2023
1 parent b6580ea commit 5de034b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
24 changes: 17 additions & 7 deletions src/timeunits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
}
Expand Down Expand Up @@ -67,6 +69,9 @@ pub trait TimeUnits: Copy + Mul<Unit, Output = Duration> {
fn centuries(self) -> Duration {
self * Unit::Century
}
fn weeks(self) -> Duration {
self * Unit::Week
}
fn days(self) -> Duration {
self * Unit::Day
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -200,7 +206,8 @@ impl From<Unit> for u8 {
Unit::Minute => 4,
Unit::Hour => 5,
Unit::Day => 6,
Unit::Century => 7,
Unit::Week => 7,
Unit::Century => 8,
Unit::Second => 0,
}
}
Expand All @@ -222,7 +229,8 @@ impl From<u8> for Unit {
4 => Unit::Minute,
5 => Unit::Hour,
6 => Unit::Day,
7 => Unit::Century,
7 => Unit::Week,
8 => Unit::Century,
_ => Unit::Second,
}
}
Expand All @@ -236,6 +244,7 @@ impl Mul<i64> 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,
Expand Down Expand Up @@ -276,6 +285,7 @@ impl Mul<f64> 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,
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 5de034b

Please sign in to comment.