diff options
author | Pro <twisted.fall@gmail.com> | 2024-02-09 21:53:42 +0100 |
---|---|---|
committer | Pro <twisted.fall@gmail.com> | 2024-02-09 21:53:42 +0100 |
commit | 1ba7fa469d04530444eb0bc6d7c9ee2fd41457f6 (patch) | |
tree | cc732a509acad26a5f484246eefaddddde776154 /src/timetype.rs | |
parent | 3fe862d63b4be68f62f8de737c5322ce22ea8dd9 (diff) |
Bump edition to 2021 and introduce rustfmt
Diffstat (limited to 'src/timetype.rs')
-rw-r--r-- | src/timetype.rs | 1161 |
1 files changed, 590 insertions, 571 deletions
diff --git a/src/timetype.rs b/src/timetype.rs index 9f67289..897513f 100644 --- a/src/timetype.rs +++ b/src/timetype.rs @@ -1,20 +1,20 @@ //! The module for the TimeType //! -use chrono::NaiveDateTime; -use chrono::NaiveDate; -use chrono::Datelike; -use chrono::Timelike; - use std::ops::Add; use std::ops::AddAssign; use std::ops::Sub; use std::ops::SubAssign; -use error::Result; -use error::Error; -use indicator::{Day, Month}; -use util::*; +use chrono::Datelike; +use chrono::NaiveDate; +use chrono::NaiveDateTime; +use chrono::Timelike; + +use crate::error::Error; +use crate::error::Result; +use crate::indicator::{Day, Month}; +use crate::util::*; /// A Type of Time, currently based on chrono::NaiveDateTime #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] @@ -91,19 +91,21 @@ impl SubAssign for TimeType { /// full month. /// impl TimeType { - - /// Alias for `TimeType::moment(::chrono::offset::Local::now().naive_local())` + /// Alias for `TimeType::moment(chrono::offset::Local::now().naive_local())` pub fn today() -> TimeType { - TimeType::moment(::chrono::offset::Local::now().naive_local()) + TimeType::moment(chrono::offset::Local::now().naive_local()) } pub fn is_a_amount(&self) -> bool { - matches!(self, TimeType::Seconds(_) | - TimeType::Minutes(_) | - TimeType::Hours(_) | - TimeType::Days(_) | - TimeType::Months(_) | - TimeType::Years(_)) + matches!( + self, + TimeType::Seconds(_) + | TimeType::Minutes(_) + | TimeType::Hours(_) + | TimeType::Days(_) + | TimeType::Months(_) + | TimeType::Years(_) + ) } pub fn is_moment(&self) -> bool { @@ -230,11 +232,11 @@ impl TimeType { match *self { TimeType::Seconds(d) => d, TimeType::Minutes(d) => d * 60, - TimeType::Hours(d) => d * 60 * 60, - TimeType::Days(d) => d * 60 * 60 * 24, - TimeType::Months(d) => d * 60 * 60 * 24 * 30, - TimeType::Years(d) => d * 60 * 60 * 24 * 30 * 12, - _ => 0 + TimeType::Hours(d) => d * 60 * 60, + TimeType::Days(d) => d * 60 * 60 * 24, + TimeType::Months(d) => d * 60 * 60 * 24 * 30, + TimeType::Years(d) => d * 60 * 60 * 24 * 30 * 12, + _ => 0, } } @@ -256,11 +258,11 @@ impl TimeType { match *self { TimeType::Seconds(s) => s / 60, TimeType::Minutes(d) => d, - TimeType::Hours(d) => d * 60, - TimeType::Days(d) => d * 60 * 24, - TimeType::Months(d) => d * 60 * 24 * 30, - TimeType::Years(d) => d * 60 * 24 * 30 * 12, - _ => 0 + TimeType::Hours(d) => d * 60, + TimeType::Days(d) => d * 60 * 24, + TimeType::Months(d) => d * 60 * 24 * 30, + TimeType::Years(d) => d * 60 * 24 * 30 * 12, + _ => 0, } } @@ -282,11 +284,11 @@ impl TimeType { match *self { TimeType::Seconds(s) => s / 60 / 60, TimeType::Minutes(d) => d / 60, - TimeType::Hours(d) => d, - TimeType::Days(d) => d * 24, - TimeType::Months(d) => d * 24 * 30, - TimeType::Years(d) => d * 24 * 30 * 12, - _ => 0 + TimeType::Hours(d) => d, + TimeType::Days(d) => d * 24, + TimeType::Months(d) => d * 24 * 30, + TimeType::Years(d) => d * 24 * 30 * 12, + _ => 0, } } @@ -308,11 +310,11 @@ impl TimeType { match *self { TimeType::Seconds(s) => s / 24 / 60 / 60, TimeType::Minutes(d) => d / 24 / 60, - TimeType::Hours(d) => d / 24, - TimeType::Days(d) => d, - TimeType::Months(d) => d * 30, - TimeType::Years(d) => d * 30 * 12, - _ => 0 + TimeType::Hours(d) => d / 24, + TimeType::Days(d) => d, + TimeType::Months(d) => d * 30, + TimeType::Years(d) => d * 30 * 12, + _ => 0, } } @@ -334,11 +336,11 @@ impl TimeType { match *self { TimeType::Seconds(s) => s / 30 / 24 / 60 / 60, TimeType::Minutes(d) => d / 30 / 24 / 60, - TimeType::Hours(d) => d / 30 / 24, - TimeType::Days(d) => d / 30, - TimeType::Months(d) => d, - TimeType::Years(d) => d * 12, - _ => 0 + TimeType::Hours(d) => d / 30 / 24, + TimeType::Days(d) => d / 30, + TimeType::Months(d) => d, + TimeType::Years(d) => d * 12, + _ => 0, } } @@ -360,18 +362,18 @@ impl TimeType { match *self { TimeType::Seconds(s) => s / 12 / 30 / 24 / 60 / 60, TimeType::Minutes(d) => d / 12 / 30 / 24 / 60, - TimeType::Hours(d) => d / 12 / 30 / 24, - TimeType::Days(d) => d / 12 / 30, - TimeType::Months(d) => d / 12, - TimeType::Years(d) => d, - _ => 0 + TimeType::Hours(d) => d / 12 / 30 / 24, + TimeType::Days(d) => d / 12 / 30, + TimeType::Months(d) => d / 12, + TimeType::Years(d) => d, + _ => 0, } } pub fn get_moment(&self) -> Option<&NaiveDateTime> { match self { TimeType::Moment(m) => Some(m), - _ => None, + _ => None, } } @@ -382,7 +384,7 @@ impl TimeType { match *self { TT::Moment(m) => Ok(m.weekday() == d.into()), - _ => Err(Error::CannotCompareDayTo(self.name())), + _ => Err(Error::CannotCompareDayTo(self.name())), } } @@ -393,7 +395,7 @@ impl TimeType { match *self { TT::Moment(m) => Ok(m.month() == month.into()), - _ => Err(Error::CannotCompareMonthTo(self.name())), + _ => Err(Error::CannotCompareMonthTo(self.name())), } } @@ -402,27 +404,26 @@ impl TimeType { use self::TimeType as TT; match *self { - TT::Addition(..) => "Addition", - TT::Days(..) => "Days", - TT::EndOfDay(..) => "EndOfDay", - TT::EndOfHour(..) => "EndOfHour", + TT::Addition(..) => "Addition", + TT::Days(..) => "Days", + TT::EndOfDay(..) => "EndOfDay", + TT::EndOfHour(..) => "EndOfHour", TT::EndOfMinute(..) => "EndOfMinute", - TT::EndOfMonth(..) => "EndOfMonth", - TT::EndOfYear(..) => "EndOfYear", - TT::Hours(..) => "Hours", - TT::Minutes(..) => "Minutes", - TT::Moment(..) => "Moment", - TT::Months(..) => "Months", - TT::Seconds(..) => "Seconds", + TT::EndOfMonth(..) => "EndOfMonth", + TT::EndOfYear(..) => "EndOfYear", + TT::Hours(..) => "Hours", + TT::Minutes(..) => "Minutes", + TT::Moment(..) => "Moment", + TT::Months(..) => "Months", + TT::Seconds(..) => "Seconds", TT::Subtraction(..) => "Subtraction", - TT::Years(..) => "Years", + TT::Years(..) => "Years", } } pub fn calculate(self) -> Result<TimeType> { do_calculate(self) } - } /// Helper trait for converting things into a TimeType object @@ -439,17 +440,17 @@ impl IntoTimeType for TimeType { } fn do_calculate(tt: TimeType) -> Result<TimeType> { - use timetype::TimeType as TT; + use crate::timetype::TimeType as TT; match tt { - TT::Addition(a, b) => add(a, b), - TT::Subtraction(a, b) => sub(a, b), - TT::EndOfYear(inner) => end_of_year(*inner), - TT::EndOfMonth(inner) => end_of_month(*inner), - TT::EndOfDay(inner) => end_of_day(*inner), - TT::EndOfHour(inner) => end_of_hour(*inner), + TT::Addition(a, b) => add(a, b), + TT::Subtraction(a, b) => sub(a, b), + TT::EndOfYear(inner) => end_of_year(*inner), + TT::EndOfMonth(inner) => end_of_month(*inner), + TT::EndOfDay(inner) => end_of_day(*inner), + TT::EndOfHour(inner) => end_of_hour(*inner), TT::EndOfMinute(inner) => end_of_minute(*inner), - x => Ok(x) + x => Ok(x), } } @@ -458,27 +459,27 @@ fn do_calculate(tt: TimeType) -> Result<TimeType> { /// /// Calling a end-of-year on a end-of-year yields end-of-year applied only once. fn end_of_year(tt: TimeType) -> Result<TimeType> { - use timetype::TimeType as TT; + use crate::timetype::TimeType as TT; match do_calculate(tt)? { - els @ TT::Seconds(_) | - els @ TT::Minutes(_) | - els @ TT::Hours(_) | - els @ TT::Days(_) | - els @ TT::Months(_) | - els @ TT::Years(_) | - els @ TT::Addition(_, _) | - els @ TT::Subtraction(_, _) => Err(Error::CannotCalculateEndOfYearOn(els)), + els @ TT::Seconds(_) + | els @ TT::Minutes(_) + | els @ TT::Hours(_) + | els @ TT::Days(_) + | els @ TT::Months(_) + | els @ TT::Years(_) + | els @ TT::Addition(_, _) + | els @ TT::Subtraction(_, _) => Err(Error::CannotCalculateEndOfYearOn(els)), TT::Moment(m) => NaiveDate::from_ymd_opt(m.year(), 12, 31) .and_then(|nd| nd.and_hms_opt(0, 0, 0)) .map(TT::moment) .ok_or(Error::OutOfBounds(m.year(), 12, 31, 0, 0, 0)) .map_err(Error::from), - TT::EndOfYear(e) => do_calculate(*e), - TT::EndOfMonth(e) => do_calculate(*e), - TT::EndOfDay(e) => do_calculate(*e), - TT::EndOfHour(e) => do_calculate(*e), + TT::EndOfYear(e) => do_calculate(*e), + TT::EndOfMonth(e) => do_calculate(*e), + TT::EndOfDay(e) => do_calculate(*e), + TT::EndOfHour(e) => do_calculate(*e), TT::EndOfMinute(e) => do_calculate(*e), } } @@ -488,18 +489,18 @@ fn end_of_year(tt: TimeType) -> Result<TimeType> { /// /// Calling a end-of-month on a end-of-month yields end-of-month applied only once. fn end_of_month(tt: TimeType) -> Result<TimeType> { - use timetype::TimeType as TT; + use crate::timetype::TimeType as TT; match do_calculate(tt)? { - els @ TT::Seconds(_) | - els @ TT::Minutes(_) | - els @ TT::Hours(_) | - els @ TT::Days(_) | - els @ TT::Months(_) | - els @ TT::Years(_) | - els @ TT::Addition(_, _) | - els @ TT::Subtraction(_, _) => Err(Error::CannotCalculateEndOfMonthOn(els)), - TT::Moment(m) => { + els @ TT::Seconds(_) + | els @ TT::Minutes(_) + | els @ TT::Hours(_) + | els @ TT::Days(_) + | els @ TT::Months(_) + | els @ TT::Years(_) + | els @ TT::Addition(_, _) + | els @ TT::Subtraction(_, _) => Err(Error::CannotCalculateEndOfMonthOn(els)), + TT::Moment(m) => { let last_day = get_num_of_days_in_month(m.year() as i64, m.month() as i64) as u32; NaiveDate::from_ymd_opt(m.year(), m.month(), last_day) .and_then(|nd| nd.and_hms_opt(0, 0, 0)) @@ -507,10 +508,10 @@ fn end_of_month(tt: TimeType) -> Result<TimeType> { .ok_or(Error::OutOfBounds(m.year(), m.month(), last_day, 0, 0, 0)) .map_err(Error::from) }, - TT::EndOfYear(e) => do_calculate(*e), - TT::EndOfMonth(e) => do_calculate(*e), - TT::EndOfDay(e) => do_calculate(*e), - TT::EndOfHour(e) => do_calculate(*e), + TT::EndOfYear(e) => do_calculate(*e), + TT::EndOfMonth(e) => do_calculate(*e), + TT::EndOfDay(e) => do_calculate(*e), + TT::EndOfHour(e) => do_calculate(*e), TT::EndOfMinute(e) => do_calculate(*e), } } @@ -520,26 +521,26 @@ fn end_of_month(tt: TimeType) -> Result<TimeType> { /// /// Calling a end-of-day on a end-of-day yields end-of-month applied only once. fn end_of_day(tt: TimeType) -> Result<TimeType> { - use timetype::TimeType as TT; + use crate::timetype::TimeType as TT; match do_calculate(tt)? { - els @ TT::Seconds(_) | - els @ TT::Minutes(_) | - els @ TT::Hours(_) | - els @ TT::Days(_) | - els @ TT::Months(_) | - els @ TT::Years(_) | - els @ TT::Addition(_, _) | - els @ TT::Subtraction(_, _) => Err(Error::CannotCalculateEndOfMonthOn(els)), + els @ TT::Seconds(_) + | els @ TT::Minutes(_) + | els @ TT::Hours(_) + | els @ TT::Days(_) + | els @ TT::Months(_) + | els @ TT::Years(_) + | els @ TT::Addition(_, _) + | els @ TT::Subtraction(_, _) => Err(Error::CannotCalculateEndOfMonthOn(els)), TT::Moment(m) => NaiveDate::from_ymd_opt(m.year(), m.month(), m.day()) .and_then(|nd| nd.and_hms_opt(23, 59, 59)) .map(TT::moment) .ok_or(Error::OutOfBounds(m.year(), m.month(), m.day(), 23, 59, 59)) .map_err(Error::from), - TT::EndOfYear(e) => do_calculate(*e), - TT::EndOfMonth(e) => do_calculate(*e), - TT::EndOfDay(e) => do_calculate(*e), - TT::EndOfHour(e) => do_calculate(*e), + TT::EndOfYear(e) => do_calculate(*e), + TT::EndOfMonth(e) => do_calculate(*e), + TT::EndOfDay(e) => do_calculate(*e), + TT::EndOfHour(e) => do_calculate(*e), TT::EndOfMinute(e) => do_calculate(*e), } } @@ -549,26 +550,26 @@ fn end_of_day(tt: TimeType) -> Result<TimeType> { /// /// Calling a end-of-hour on a end-of-hour yields end-of-month applied only once. fn end_of_hour(tt: TimeType) -> Result<TimeType> { - use timetype::TimeType as TT; + use crate::timetype::TimeType as TT; match do_calculate(tt)? { - els @ TT::Seconds(_) | - els @ TT::Minutes(_) | - els @ TT::Hours(_) | - els @ TT::Days(_) | - els @ TT::Months(_) | - els @ TT::Years(_) | - els @ TT::Addition(_, _) | - els @ TT::Subtraction(_, _) => Err(Error::CannotCalculateEndOfMonthOn(els)), - TT::Moment(m) => NaiveDate::from_ymd_opt(m.year(), m.month(), m.day()) + els @ TT::Seconds(_) + | els @ TT::Minutes(_) + | els @ TT::Hours(_) + | els @ TT::Days(_) + | els @ TT::Months(_) + | els @ TT::Years(_) + | els @ TT::Addition(_, _) + | els @ TT::Subtraction(_, _) => Err(Error::CannotCalculateEndOfMonthOn(els)), + TT::Moment(m) => NaiveDate::from_ymd_opt(m.year(), m.month(), m.day()) .and_then(|nd| nd.and_hms_opt(m.hour(), 59, 59)) .map(TT::moment) .ok_or(Error::OutOfBounds(m.year(), m.month(), m.day(), m.hour(), 59, 59)) .map_err(Error::from), - TT::EndOfYear(e) => do_calculate(*e), - TT::EndOfMonth(e) => do_calculate(*e), - TT::EndOfDay(e) => do_calculate(*e), - TT::EndOfHour(e) => do_calculate(*e), + TT::EndOfYear(e) => do_calculate(*e), + TT::EndOfMonth(e) => do_calculate(*e), + TT::EndOfDay(e) => do_calculate(*e), + TT::EndOfHour(e) => do_calculate(*e), TT::EndOfMinute(e) => do_calculate(*e), } } @@ -578,33 +579,40 @@ fn end_of_hour(tt: TimeType) -> Result<TimeType> { /// /// Calling a end-of-minute on a end-of-minute yields end-of-month applied only once. fn end_of_minute(tt: TimeType) -> Result<TimeType> { - use timetype::TimeType as TT; + use crate::timetype::TimeType as TT; match do_calculate(tt)? { - els @ TT::Seconds(_) | - els @ TT::Minutes(_) | - els @ TT::Hours(_) | - els @ TT::Days(_) | - els @ TT::Months(_) | - els @ TT::Years(_) | - els @ TT::Addition(_, _) | - els @ TT::Subtraction(_, _) => Err(Error::CannotCalculateEndOfMonthOn(els)), - TT::Moment(m) => NaiveDate::from_ymd_opt(m.year(), m.month(), m.day()) + els @ TT::Seconds(_) + | els @ TT::Minutes(_) + | els @ TT::Hours(_) + | els @ TT::Days(_) + | els @ TT::Months(_) + | els @ TT::Years(_) + | els @ TT::Addition(_, _) + | els @ TT::Subtraction(_, _) => Err(Error::CannotCalculateEndOfMonthOn(els)), + TT::Moment(m) => NaiveDate::from_ymd_opt(m.year(), m.month(), m.day()) .and_then(|nd| nd.and_hms_opt(m.hour(), m.minute(), 59)) .map(TT::moment) - .ok_or(Error::OutOfBounds(m.year(), m.month(), m.day(), m.hour(), m.minute(), 59)) + .ok_or(Error::OutOfBounds( + m.year(), + m.month(), + m.day(), + m.hour(), + m.minute(), + 59, + )) .map_err(Error::from), - TT::EndOfYear(e) => do_calculate(*e), - TT::EndOfMonth(e) => do_calculate(*e), - TT::EndOfDay(e) => do_calculate(*e), - TT::EndOfHour(e) => do_calculate(*e), + TT::EndOfYear(e) => do_calculate(*e), + TT::EndOfMonth(e) => do_calculate(*e), + TT::EndOfDay(e) => do_calculate(*e), + TT::EndOfHour(e) => do_calculate(*e), TT::EndOfMinute(e) => do_calculate(*e), } } #[allow(clippy::boxed_local)] fn add(a: Box<TimeType>, b: Box<TimeType>) -> Result<TimeType> { - use timetype::TimeType as TT; + use crate::timetype::TimeType as TT; match (*a, *b) { (TT::Moment(mom), thing) => add_to_moment(mom, thing), @@ -612,20 +620,14 @@ fn add(a: Box<TimeType>, b: Box<TimeType>) -> Result<TimeType> { (TT::Seconds(a), other) => add_to_seconds(a, other), (TT::Minutes(a), other) => add_to_minutes(a, other), - (TT::Hours(a), other) => add_to_hours(a, other), - (TT::Days(a), other) => add_to_days(a, other), - (TT::Months(a), other) => add_to_months(a, other), - (TT::Years(a), other) => add_to_years(a, other), - - (TT::Addition(a, b), other) => add(a, b) - .map(Box::new) - .and_then(|bx| add(bx, Box::new(other))), - (other, TT::Addition(a, b)) => add(a, b) - .map(Box::new) - .and_then(|bx| add(Box::new(other), bx)), - (TT::Subtraction(a, b), other) => sub(a, b) - .map(Box::new) - .and_then(|bx| add(Box::new(other), bx)), + (TT::Hours(a), other) => add_to_hours(a, other), + (TT::Days(a), other) => add_to_days(a, other), + (TT::Months(a), other) => add_to_months(a, other), + (TT::Years(a), other) => add_to_years(a, other), + + (TT::Addition(a, b), other) => add(a, b).map(Box::new).and_then(|bx| add(bx, Box::new(other))), + (other, TT::Addition(a, b)) => add(a, b).map(Box::new).and_then(|bx| add(Box::new(other), bx)), + (TT::Subtraction(a, b), other) => sub(a, b).map(Box::new).and_then(|bx| add(Box::new(other), bx)), (other, TT::Subtraction(a, b)) => do_calculate(*a) .map(Box::new) .and_then(|bx| add(Box::new(other), bx)) @@ -650,264 +652,270 @@ fn add(a: Box<TimeType>, b: Box<TimeType>) -> Result<TimeType> { } fn add_to_seconds(amount: i64, tt: TimeType) -> Result<TimeType> { - use timetype::TimeType as TT; + use crate::timetype::TimeType as TT; match tt { - TT::Seconds(a) => Ok(TT::Seconds(a + amount)), - TT::Minutes(a) => Ok(TT::Seconds(a * 60 + amount)), - TT::Hours(a) => Ok(TT::Seconds(a * 60 * 60 + amount)), - TT::Days(a) => Ok(TT::Seconds(a * 60 * 60 * 24 + amount)), - TT::Months(a) => Ok(TT::Seconds(a * 60 * 60 * 24 * 30 + amount)), - TT::Years(a) => Ok(TT::Seconds(a * 60 * 60 * 24 * 30 * 12 + amount)), - TT::Moment(m) => Err(Error::CannotAdd(TT::Seconds(amount), TT::Moment(m))), - TT::EndOfYear(e) => Err(Error::CannotAdd(TT::Seconds(amount), TT::EndOfYear(e))), - TT::EndOfMonth(e) => Err(Error::CannotAdd(TT::Seconds(amount), TT::EndOfMonth(e))), - TT::EndOfDay(e) => Err(Error::CannotAdd(TT::Seconds(amount), TT::EndOfDay(e))), - TT::EndOfHour(e) => Err(Error::CannotAdd(TT::Seconds(amount), TT::EndOfHour(e))), - TT::EndOfMinute(e) => Err(Error::CannotAdd(TT::Seconds(amount), TT::EndOfMinute(e))), - TT::Addition(b, c) => add_to_seconds(amount, add(b, c)?), + TT::Seconds(a) => Ok(TT::Seconds(a + amount)), + TT::Minutes(a) => Ok(TT::Seconds(a * 60 + amount)), + TT::Hours(a) => Ok(TT::Seconds(a * 60 * 60 + amount)), + TT::Days(a) => Ok(TT::Seconds(a * 60 * 60 * 24 + amount)), + TT::Months(a) => Ok(TT::Seconds(a * 60 * 60 * 24 * 30 + amount)), + TT::Years(a) => Ok(TT::Seconds(a * 60 * 60 * 24 * 30 * 12 + amount)), + TT::Moment(m) => Err(Error::CannotAdd(TT::Seconds(amount), TT::Moment(m))), + TT::EndOfYear(e) => Err(Error::CannotAdd(TT::Seconds(amount), TT::EndOfYear(e))), + TT::EndOfMonth(e) => Err(Error::CannotAdd(TT::Seconds(amount), TT::EndOfMonth(e))), + TT::EndOfDay(e) => Err(Error::CannotAdd(TT::Seconds(amount), TT::EndOfDay(e))), + TT::EndOfHour(e) => Err(Error::CannotAdd(TT::Seconds(amount), TT::EndOfHour(e))), + TT::EndOfMinute(e) => Err(Error::CannotAdd(TT::Seconds(amount), TT::EndOfMinute(e))), + TT::Addition(b, c) => add_to_seconds(amount, add(b, c)?), TT::Subtraction(b, c) => add_to_seconds(amount, sub(b, c)?), } } fn add_to_minutes(amount: i64, tt: TimeType) -> Result<TimeType> { - use timetype::TimeType as TT; + use crate::timetype::TimeType as TT; match tt { - TT::Seconds(a) => Ok(TT::Seconds(a + amount * 60)), - TT::Minutes(a) => Ok(TT::Minutes(a + amount)), - TT::Hours(a) => Ok(TT::Minutes(a * 60 + amount)), - TT::Days(a) => Ok(TT::Minutes(a * 60 * 24 + amount)), - TT::Months(a) => Ok(TT::Minutes(a * 60 * 24 * 30 + amount)), - TT::Years(a) => Ok(TT::Minutes(a * 60 * 24 * 30 * 12 + amount)), - TT::Moment(m) => Err(Error::CannotAdd(TT::Minutes(amount), TT::Moment(m))), - TT::EndOfYear(e) => Err(Error::CannotAdd(TT::Minutes(amount), TT::EndOfYear(e))), - TT::EndOfMonth(e) => Err(Error::CannotAdd(TT::Minutes(amount), TT::EndOfMonth(e))), - TT::EndOfDay(e) => Err(Error::CannotAdd(TT::Minutes(amount), TT::EndOfDay(e))), - TT::EndOfHour(e) => Err(Error::CannotAdd(TT::Minutes(amount), TT::EndOfHour(e))), - TT::EndOfMinute(e) => Err(Error::CannotAdd(TT::Minutes(amount), TT::EndOfMinute(e))), - TT::Addition(b, c) => add_to_minutes(amount, add(b, c)?), + TT::Seconds(a) => Ok(TT::Seconds(a + amount * 60)), + TT::Minutes(a) => Ok(TT::Minutes(a + amount)), + TT::Hours(a) => Ok(TT::Minutes(a * 60 + amount)), + TT::Days(a) => Ok(TT::Minutes(a * 60 * 24 + amount)), + TT::Months(a) => Ok(TT::Minutes(a * 60 * 24 * 30 + amount)), + TT::Years(a) => Ok(TT::Minutes(a * 60 * 24 * 30 * 12 + amount)), + TT::Moment(m) => Err(Error::CannotAdd(TT::Minutes(amount), TT::Moment(m))), + TT::EndOfYear(e) => Err(Error::CannotAdd(TT::Minutes(amount), TT::EndOfYear(e))), + TT::EndOfMonth(e) => Err(Error::CannotAdd(TT::Minutes(amount), TT::EndOfMonth(e))), + TT::EndOfDay(e) => Err(Error::CannotAdd(TT::Minutes(amount), TT::EndOfDay(e))), + TT::EndOfHour(e) => Err(Error::CannotAdd(TT::Minutes(amount), TT::EndOfHour(e))), + TT::EndOfMinute(e) => Err(Error::CannotAdd(TT::Minutes(amount), TT::EndOfMinute(e))), + TT::Addition(b, c) => add_to_minutes(amount, add(b, c)?), TT::Subtraction(b, c) => add_to_minutes(amount, sub(b, c)?), } } fn add_to_hours(amount: i64, tt: TimeType) -> Result<TimeType> { - use timetype::TimeType as TT; + use crate::timetype::TimeType as TT; match tt { - TT::Seconds(a) => Ok(TT::Seconds(a + amount * 60 * 60)), - TT::Minutes(a) => Ok(TT::Minutes(a + amount * 60)), - TT::Hours(a) => Ok(TT::Hours( a + amount)), - TT::Days(a) => Ok(TT::Hours( a * 24 + amount)), - TT::Months(a) => Ok(TT::Hours( a * 24 * 30 + amount)), - TT::Years(a) => Ok(TT::Hours( a * 24 * 30 * 12 + amount)), - TT::Moment(m) => Err(Error::CannotAdd(TT::Hours(amount), TT::Moment(m))), - TT::EndOfYear(e) => Err(Error::CannotAdd(TT::Hours(amount), TT::EndOfYear(e))), - TT::EndOfMonth(e) => Err(Error::CannotAdd(TT::Hours(amount), TT::EndOfMonth(e))), - TT::EndOfDay(e) => Err(Error::CannotAdd(TT::Hours(amount), TT::EndOfDay(e))), - TT::EndOfHour(e) => Err(Error::CannotAdd(TT::Hours(amount), TT::EndOfHour(e))), - TT::EndOfMinute(e) => Err(Error::CannotAdd(TT::Hours(amount), TT::EndOfMinute(e))), - TT::Addition(b, c) => add_to_hours(amount, add(b, c)?), + TT::Seconds(a) => Ok(TT::Seconds(a + amount * 60 * 60)), + TT::Minutes(a) => Ok(TT::Minutes(a + amount * 60)), + TT::Hours(a) => Ok(TT::Hours(a + amount)), + TT::Days(a) => Ok(TT::Hours(a * 24 + amount)), + TT::Months(a) => Ok(TT::Hours(a * 24 * 30 + amount)), + TT::Years(a) => Ok(TT::Hours(a * 24 * 30 * 12 + amount)), + TT::Moment(m) => Err(Error::CannotAdd(TT::Hours(amount), TT::Moment(m))), + TT::EndOfYear(e) => Err(Error::CannotAdd(TT::Hours(amount), TT::EndOfYear(e))), + TT::EndOfMonth(e) => Err(Error::CannotAdd(TT::Hours(amount), TT::EndOfMonth(e))), + TT::EndOfDay(e) => Err(Error::CannotAdd(TT::Hours(amount), TT::EndOfDay(e))), + TT::EndOfHour(e) => Err(Error::CannotAdd(TT::Hours(amount), TT::EndOfHour(e))), + TT::EndOfMinute(e) => Err(Error::CannotAdd(TT::Hours(amount), TT::EndOfMinute(e))), + TT::Addition(b, c) => add_to_hours(amount, add(b, c)?), TT::Subtraction(b, c) => add_to_hours(amount, sub(b, c)?), } } fn add_to_days(amount: i64, tt: TimeType) -> Result<TimeType> { - use timetype::TimeType as TT; + use crate::timetype::TimeType as TT; match tt { - TT::Seconds(a) => Ok(TT::Seconds(a + amount * 24 * 60 * 60)), - TT::Minutes(a) => Ok(TT::Minutes(a + amount * 24 * 60)), - TT::Hours(a) => Ok(TT::Hours( a + amount * 24)), - TT::Days(a) => Ok(TT::Days( a + amount)), - TT::Months(a) => Ok(TT::Days( a * 30 + amount)), - TT::Years(a) => Ok(TT::Days( a * 30 * 12 + amount)), - TT::Moment(m) => Err(Error::CannotAdd(TT::Days(amount), TT::Moment(m))), - TT::EndOfYear(e) => Err(Error::CannotAdd(TT::Days(amount), TT::EndOfYear(e))), - TT::EndOfMonth(e) => Err(Error::CannotAdd(TT::Days(amount), TT::EndOfMonth(e))), - TT::EndOfDay(e) => Err(Error::CannotAdd(TT::Days(amount), TT::EndOfDay(e))), - TT::EndOfHour(e) => Err(Error::CannotAdd(TT::Days(amount), TT::EndOfHour(e))), - TT::EndOfMinute(e) => Err(Error::CannotAdd(TT::Days(amount), TT::EndOfMinute(e))), - TT::Addition(b, c) => add_to_days(amount, add(b, c)?), + TT::Seconds(a) => Ok(TT::Seconds(a + amount * 24 * 60 * 60)), + TT::Minutes(a) => Ok(TT::Minutes(a + amount * 24 * 60)), + TT::Hours(a) => Ok(TT::Hours(a + amount * 24)), + TT::Days(a) => Ok(TT::Days(a + amount)), + TT::Months(a) => Ok(TT::Days(a * 30 + amount)), + TT::Years(a) => Ok(TT::Days(a * 30 * 12 + amount)), + TT::Moment(m) => Err(Error::CannotAdd(TT::Days(amount), TT::Moment(m))), + TT::EndOfYear(e) => Err(Error::CannotAdd(TT::Days(amount), TT::EndOfYear(e))), + TT::EndOfMonth(e) => Err(Error::CannotAdd(TT::Days(amount), TT::EndOfMonth(e))), + TT::EndOfDay(e) => Err(Error::CannotAdd(TT::Days(amount), TT::EndOfDay(e))), + TT::EndOfHour(e) => Err(Error::CannotAdd(TT::Days(amount), TT::EndOfHour(e))), + TT::EndOfMinute(e) => Err(Error::CannotAdd(TT::Days(amount), TT::EndOfMinute(e))), + TT::Addition(b, c) => add_to_days(amount, add(b, c)?), TT::Subtraction(b, c) => add_to_days(amount, sub(b, c)?), } } fn add_to_months(amount: i64, tt: TimeType) -> Result<TimeType> { - use timetype::TimeType as TT; + use crate::timetype::TimeType as TT; match tt { - TT::Seconds(a) => Ok(TT::Seconds(a + amount * 30 * 24 * 60 * 60)), - TT::Minutes(a) => Ok(TT::Minutes(a + amount * 30 * 24 * 60)), - TT::Hours(a) => Ok(TT::Hours( a + amount * 30 * 24)), - TT::Days(a) => Ok(TT::Days( a + amount * 30)), - TT::Months(a) => Ok(TT::Months( a + amount)), - TT::Years(a) => Ok(TT::Months( a * 12 + amount)), - TT::Moment(m) => Err(Error::CannotAdd(TT::Months(amount), TT::Moment(m))), - TT::EndOfYear(e) => Err(Error::CannotAdd(TT::Months(amount), TT::EndOfYear(e))), - TT::EndOfMonth(e) => Err(Error::CannotAdd(TT::Months(amount), TT::EndOfMonth(e))), - TT::EndOfDay(e) => Err(Error::CannotAdd(TT::Months(amount), TT::EndOfDay(e))), - TT::EndOfHour(e) => Err(Error::CannotAdd(TT::Months(amount), TT::EndOfHour(e))), - TT::EndOfMinute(e) => Err(Error::CannotAdd(TT::Months(amount), TT::EndOfMinute(e))), - TT::Addition(b, c) => add_to_months(amount, add(b, c)?), + TT::Seconds(a) => Ok(TT::Seconds(a + amount * 30 * 24 * 60 * 60)), + TT::Minutes(a) => Ok(TT::Minutes(a + amount * 30 * 24 * 60)), + TT::Hours(a) => Ok(TT::Hours(a + amount * 30 * 24)), + TT::Days(a) => Ok(TT::Days(a + amount * 30)), + TT::Months(a) => Ok(TT::Months(a + amount)), + TT::Years(a) => Ok(TT::Months(a * 12 + amount)), + TT::Moment(m) => Err(Error::CannotAdd(TT::Months(amount), TT::Moment(m))), + TT::EndOfYear(e) => Err(Error::CannotAdd(TT::Months(amount), TT::EndOfYear(e))), + TT::EndOfMonth(e) => Err(Error::CannotAdd(TT::Months(amount), TT::EndOfMonth(e))), + TT::EndOfDay(e) => Err(Error::CannotAdd(TT::Months(amount), TT::EndOfDay(e))), + TT::EndOfHour(e) => Err(Error::CannotAdd(TT::Months(amount), TT::EndOfHour(e))), + TT::EndOfMinute(e) => Err(Error::CannotAdd(TT::Months(amount), TT::EndOfMinute(e))), + TT::Addition(b, c) => add_to_months(amount, add(b, c)?), TT::Subtraction(b, c) => add_to_months(amount, sub(b, c)?), } } fn add_to_years(amount: i64, tt: TimeType) -> Result<TimeType> { - use timetype::TimeType as TT; + use crate::timetype::TimeType as TT; match tt { - TT::Seconds(a) => Ok(TT::Seconds(a + amount * 12 * 30 * 24 * 60 * 60)), - TT::Minutes(a) => Ok(TT::Minutes(a + amount * 12 * 30 * 24 * 60)), - TT::Hours(a) => Ok(TT::Hours( a + amount * 12 * 30 * 24)), - TT::Days(a) => Ok(TT::Days( a + amount * 12 * 30)), - TT::Months(a) => Ok(TT::Months( a + amount * 12)), - TT::Years(a) => Ok(TT::Years( a + amount)), - TT::Moment(m) => Err(Error::CannotAdd(TT::Years(amount), TT::Moment(m))), - TT::EndOfYear(e) => Err(Error::CannotAdd(TT::Years(amount), TT::EndOfYear(e))), - TT::EndOfMonth(e) => Err(Error::CannotAdd(TT::Years(amount), TT::EndOfMonth(e))), - TT::EndOfDay(e) => Err(Error::CannotAdd(TT::Years(amount), TT::EndOfDay(e))), - TT::EndOfHour(e) => Err(Error::CannotAdd(TT::Years(amount), TT::EndOfHour(e))), - TT::EndOfMinute(e) => Err(Error::CannotAdd(TT::Years(amount), TT::EndOfMinute(e))), - TT::Addition(b, c) => add_to_years(amount, add(b, c)?), + TT::Seconds(a) => Ok(TT::Seconds(a + amount * 12 * 30 * 24 * 60 * 60)), + TT::Minutes(a) => Ok(TT::Minutes(a + amount * 12 * 30 * 24 * 60)), + TT::Hours(a) => Ok(TT::Hours(a + amount * 12 * 30 * 24)), + TT::Days(a) => Ok(TT::Days(a + amount * 12 * 30)), + TT::Months(a) => Ok(TT::Months(a + amount * 12)), + TT::Years(a) => Ok(TT::Years(a + amount)), + TT::Moment(m) => Err(Error::CannotAdd(TT::Years(amount), TT::Moment(m))), + TT::EndOfYear(e) => Err(Error::CannotAdd(TT::Years(amount), TT::EndOfYear(e))), + TT::EndOfMonth(e) => Err(Error::CannotAdd(TT::Years(amount), TT::EndOfMonth(e))), + TT::EndOfDay(e) => Err(Error::CannotAdd(TT::Years(amount), TT::EndOfDay(e))), + TT::EndOfHour(e) => Err(Error::CannotAdd(TT::Years(amount), TT::EndOfHour(e))), + TT::EndOfMinute(e) => Err(Error::CannotAdd(TT::Years(amount), TT::EndOfMinute(e))), + TT::Addition(b, c) => add_to_years(amount, add(b, c)?), TT::Subtraction(b, c) => add_to_years(amount, sub(b, c)?), } } fn add_to_moment(mom: NaiveDateTime, tt: TimeType) -> Result<TimeType> { - use timetype::TimeType as TT; + use crate::timetype::TimeType as TT; match tt { - TT::Seconds(a) => { - let y = mom.year() as i64; + TT::Seconds(a) => { + let y = mom.year() as i64; let mo = mom.month() as i64; - let d = mom.day() as i64; - let h = mom.hour() as i64; + let d = mom.day() as i64; + let h = mom.hour() as i64; let mi = mom.minute() as i64; - let s = mom.second() as i64 + a; + let s = mom.second() as i64 + a; let (y, mo, d, h, mi, s) = adjust_times_add(y, mo, d, h, mi, s); let tt = NaiveDate::from_ymd_opt(y as i32, mo as u32, d as u32) .and_then(|nd| nd.and_hms_opt(h as u32, mi as u32, s as u32)) - .ok_or(Error::OutOfBounds(y as i32, mo as u32, h as u32, h as u32, mi as u32, s as u32)) + .ok_or(Error::OutOfBounds( + y as i32, mo as u32, h as u32, h as u32, mi as u32, s as u32, + )) .map_err(Error::from)?; Ok(TimeType::moment(tt)) }, - TT::Minutes(a) => { - let y = mom.year() as i64; + TT::Minutes(a) => { + let y = mom.year() as i64; let mo = mom.month() as i64; - let d = mom.day() as i64; - let h = mom.hour() as i64; + let d = mom.day() as i64; + let h = mom.hour() as i64; let mi = mom.minute() as i64 + a; - let s = mom.second() as i64; + let s = mom.second() as i64; let (y, mo, d, h, mi, s) = adjust_times_add(y, mo, d, h, mi, s); let tt = NaiveDate::from_ymd_opt(y as i32, mo as u32, d as u32) .and_then(|nd| nd.and_hms_opt(h as u32, mi as u32, s as u32)) - .ok_or(Error::OutOfBounds(y as i32, mo as u32, h as u32, h as u32, mi as u32, s as u32)) + .ok_or(Error::OutOfBounds( + y as i32, mo as u32, h as u32, h as u32, mi as u32, s as u32, + )) .map_err(Error::from)?; Ok(TimeType::moment(tt)) }, - TT::Hours(a) => { - let y = mom.year() as i64; + TT::Hours(a) => { + let y = mom.year() as i64; let mo = mom.month() as i64; - let d = mom.day() as i64; - let h = mom.hour() as i64 + a; + let d = mom.day() as i64; + let h = mom.hour() as i64 + a; let mi = mom.minute() as i64; - let s = mom.second() as i64; + let s = mom.second() as i64; let (y, mo, d, h, mi, s) = adjust_times_add(y, mo, d, h, mi, s); let tt = NaiveDate::from_ymd_opt(y as i32, mo as u32, d as u32) .and_then(|nd| nd.and_hms_opt(h as u32, mi as u32, s as u32)) - .ok_or(Error::OutOfBounds(y as i32, mo as u32, h as u32, h as u32, mi as u32, s as u32)) + .ok_or(Error::OutOfBounds( + y as i32, mo as u32, h as u32, h as u32, mi as u32, s as u32, + )) .map_err(Error::from)?; Ok(TimeType::moment(tt)) }, - TT::Days(a) => { - let y = mom.year() as i64; + TT::Days(a) => { + let y = mom.year() as i64; let mo = mom.month() as i64; - let d = mom.day() as i64 + a; - let h = mom.hour() as i64; + let d = mom.day() as i64 + a; + let h = mom.hour() as i64; let mi = mom.minute() as i64; - let s = mom.second() as i64; + let s = mom.second() as i64; let (y, mo, d, h, mi, s) = adjust_times_add(y, mo, d, h, mi, s); let tt = NaiveDate::from_ymd_opt(y as i32, mo as u32, d as u32) .and_then(|nd| nd.and_hms_opt(h as u32, mi as u32, s as u32)) - .ok_or(Error::OutOfBounds(y as i32, mo as u32, h as u32, h as u32, mi as u32, s as u32)) + .ok_or(Error::OutOfBounds( + y as i32, mo as u32, h as u32, h as u32, mi as u32, s as u32, + )) .map_err(Error::from)?; Ok(TimeType::moment(tt)) }, - TT::Months(a) => { - let y = mom.year() as i64; + TT::Months(a) => { + let y = mom.year() as i64; let mo = mom.month() as i64 + a; - let d = mom.day() as i64; - let h = mom.hour() as i64; + let d = mom.day() as i64; + let h = mom.hour() as i64; let mi = mom.minute() as i64; - let s = mom.second() as i64; + let s = mom.second() as i64; let (y, mo, d, h, mi, s) = adjust_times_add(y, mo, d, h, mi, s); let tt = NaiveDate::from_ymd_opt(y as i32, mo as u32, d as u32) .and_then(|nd| nd.and_hms_opt(h as u32, mi as u32, s as u32)) - .ok_or(Error::OutOfBounds(y as i32, mo as u32, h as u32, h as u32, mi as u32, s as u32)) + .ok_or(Error::OutOfBounds( + y as i32, mo as u32, h as u32, h as u32, mi as u32, s as u32, + )) .map_err(Error::from)?; Ok(TimeType::moment(t |