summaryrefslogtreecommitdiff
path: root/src/date.rs
diff options
context:
space:
mode:
authorBotahamec <botahamec@outlook.com>2022-03-18 14:57:13 -0400
committerBotahamec <botahamec@outlook.com>2022-03-18 14:57:13 -0400
commitdb9f20681f90f206d393d2b06a6a8401515ff562 (patch)
tree48c16c94a4bf0da27c7cde56a247703b7794600c /src/date.rs
parente0e0a18a4bc873583e973d771669e88a92b20d92 (diff)
Addition methods for NaiveDateTime
Diffstat (limited to 'src/date.rs')
-rw-r--r--src/date.rs53
1 files changed, 33 insertions, 20 deletions
diff --git a/src/date.rs b/src/date.rs
index 3691a7b..89ca721 100644
--- a/src/date.rs
+++ b/src/date.rs
@@ -86,25 +86,32 @@ impl Date {
self.year.is_leap_year()
}
- // TODO overflow handling
- pub const fn add_years(self, years: i16) -> Result<Self, LeapDayNotInLeapYearError> {
- let year = self.year + years;
+ pub const fn add_years_overflowing(
+ self,
+ years: i16,
+ ) -> Result<(Self, bool), LeapDayNotInLeapYearError> {
+ let (year, overflow) = self.year.overflowing_add(years);
- if self.day == 29 && self.month == Month::February && !year.is_leap_year() {
+ if self.day == 29 && (self.month as u8) == (Month::February as u8) && !year.is_leap_year() {
Err(LeapDayNotInLeapYearError(self.year))
} else {
- Ok(Self {
- year,
- month: self.month,
- day: self.day,
- })
+ Ok((
+ Self {
+ year,
+ month: self.month,
+ day: self.day,
+ },
+ overflow,
+ ))
}
}
- // TODO overflow handling
- pub const fn add_months(self, months: i8) -> Result<Self, DayGreaterThanMaximumForMonthError> {
+ pub const fn add_months_overflowing(
+ self,
+ months: i8,
+ ) -> Result<(Self, bool), DayGreaterThanMaximumForMonthError> {
let (month, years_to_add) = self.month.add_overflowing(months);
- let year = self.year + years_to_add;
+ let (year, overflow) = self.year.overflowing_add(years_to_add as i16);
let max_days_for_month = month.days(year.is_leap_year());
if self.day > max_days_for_month {
@@ -114,11 +121,14 @@ impl Date {
month_max_day: max_days_for_month,
})
} else {
- Ok(Self {
- year,
- month,
- day: self.day,
- })
+ Ok((
+ Self {
+ year,
+ month,
+ day: self.day,
+ },
+ overflow,
+ ))
}
}
@@ -151,9 +161,12 @@ impl Date {
}
#[must_use]
- pub const fn add_days(self, days: i64) -> Self {
- let total_days_since_ce = self.days_after_common_era() + days;
- Self::from_days_after_common_era(total_days_since_ce)
+ pub const fn add_days_overflowing(self, days: i64) -> (Self, bool) {
+ let (total_days_since_ce, overflow) = self.days_after_common_era().overflowing_add(days);
+ (
+ Self::from_days_after_common_era(total_days_since_ce),
+ overflow,
+ )
}
}