summaryrefslogtreecommitdiffstats
path: root/src/timetype.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/timetype.rs')
-rw-r--r--src/timetype.rs1816
1 files changed, 912 insertions, 904 deletions
diff --git a/src/timetype.rs b/src/timetype.rs
index 818ad4b..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,43 +91,33 @@ 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 {
- match *self {
- TimeType::Seconds(_) |
- TimeType::Minutes(_) |
- TimeType::Hours(_) |
- TimeType::Days(_) |
- TimeType::Months(_) |
- TimeType::Years(_) => true,
- _ => false,
- }
+ matches!(
+ self,
+ TimeType::Seconds(_)
+ | TimeType::Minutes(_)
+ | TimeType::Hours(_)
+ | TimeType::Days(_)
+ | TimeType::Months(_)
+ | TimeType::Years(_)
+ )
}
pub fn is_moment(&self) -> bool {
- match *self {
- TimeType::Moment(_) => true,
- _ => false,
- }
+ matches!(self, TimeType::Moment(_))
}
pub fn is_addition(&self) -> bool {
- match *self {
- TimeType::Addition(_, _) => true,
- _ => false,
- }
+ matches!(self, TimeType::Addition(_, _))
}
pub fn is_subtraction(&self) -> bool {
- match *self {
- TimeType::Subtraction(_, _) => true,
- _ => false,
- }
+ matches!(self, TimeType::Subtraction(_, _))
}
pub fn seconds(i: i64) -> TimeType {
@@ -242,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,
}
}
@@ -268,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,
}
}
@@ -294,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,
}
}
@@ -320,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,
}
}
@@ -346,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,
}
}
@@ -372,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(ref m) => Some(&m),
- _ => None,
+ match self {
+ TimeType::Moment(m) => Some(m),
+ _ => None,
}
}
@@ -394,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())),
}
}
@@ -405,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())),
}
}
@@ -414,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
@@ -451,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),
}
}
@@ -470,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)
- .map(|nd| nd.and_hms(0, 0, 0))
+ .and_then(|nd| nd.and_hms_opt(0, 0, 0))
.map(TT::moment)
- .ok_or(Error::OutOfBounds(m.year() as i32, 12, 31, 0, 0, 0))
+ .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),
}
}
@@ -500,29 +489,29 @@ 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)
- .map(|nd| nd.and_hms(0, 0, 0))
+ .and_then(|nd| nd.and_hms_opt(0, 0, 0))
.map(TT::moment)
- .ok_or(Error::OutOfBounds(m.year() as i32, m.month() as u32, last_day, 0, 0, 0))
+ .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),
}
}
@@ -532,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())
- .map(|nd| nd.and_hms(23, 59, 59))
+ .and_then(|nd| nd.and_hms_opt(23, 59, 59))
.map(TT::moment)
- .ok_or(Error::OutOfBounds(m.year() as i32, m.month() as u32, m.day() as u32, 23, 59, 59))
+ .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),
}
}
@@ -561,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() as i32, m.month() as u32, m.day() as u32, m.hour() as u32, 59, 59))
+ .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),
}
}
@@ -590,32 +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() as i32, m.month() as u32, m.day() as u32, m.hour() as u32, m.minute() as u32, 59 as u32))
+ .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),
@@ -623,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))
@@ -661,263 +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;