Skip to content

Commit

Permalink
progress
Browse files Browse the repository at this point in the history
  • Loading branch information
BurntSushi committed Jul 3, 2024
1 parent e0a5f87 commit a2f9480
Show file tree
Hide file tree
Showing 2 changed files with 388 additions and 57 deletions.
114 changes: 57 additions & 57 deletions src/civil/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,58 @@ impl Date {
is_leap_year(self.year_ranged())
}

/// Returns the date immediately following this one.
///
/// # Errors
///
/// This returns an error when this date is the maximum value.
///
/// # Example
///
/// ```
/// use jiff::civil::Date;
///
/// let d = Date::constant(2024, 2, 28);
/// assert_eq!(d.tomorrow()?, Date::constant(2024, 2, 29));
///
/// // The max doesn't have a tomorrow.
/// assert!(Date::MAX.tomorrow().is_err());
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[inline]
pub fn tomorrow(self) -> Result<Date, Error> {
let day = self.to_unix_epoch_days();
let next = day.try_checked_add("days", C(1))?;
Ok(Date::from_unix_epoch_days(next))
}

/// Returns the date immediately preceding this one.
///
/// # Errors
///
/// This returns an error when this date is the minimum value.
///
/// # Example
///
/// ```
/// use jiff::civil::Date;
///
/// let d = Date::constant(2024, 3, 1);
/// assert_eq!(d.yesterday()?, Date::constant(2024, 2, 29));
///
/// // The min doesn't have a yesterday.
/// assert!(Date::MIN.yesterday().is_err());
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[inline]
pub fn yesterday(self) -> Result<Date, Error> {
let day = self.to_unix_epoch_days();
let next = day.try_checked_sub("days", C(1))?;
Ok(Date::from_unix_epoch_days(next))
}

/// Returns the "nth" weekday from the beginning or end of the month in
/// which this date resides.
///
Expand Down Expand Up @@ -842,8 +894,8 @@ impl Date {
///
/// # Errors
///
/// This returns an error when `nth` is `0`, or if it would otherwise result
/// in a date that overflows the minimum/maximum values of `Date`.
/// This returns an error when `nth` is `0`, or if it would otherwise
/// result in a date that overflows the minimum/maximum values of `Date`.
///
/// # Example
///
Expand Down Expand Up @@ -915,9 +967,9 @@ impl Date {
///
/// # Example: the start of Israeli summer time
///
/// Israeli law says (at present, 2024-03-11) that DST or "summer time"
/// starts on the Friday before the last Sunday in March. We can find that
/// date using both `nth_weekday` and [`Date::nth_weekday_of_month`]:
/// Israeli law says (at present, as of 2024-03-11) that DST or "summer
/// time" starts on the Friday before the last Sunday in March. We can find
/// that date using both `nth_weekday` and [`Date::nth_weekday_of_month`]:
///
/// ```
/// use jiff::civil::{Date, Weekday};
Expand Down Expand Up @@ -997,58 +1049,6 @@ impl Date {
}
}

/// Returns the date immediately following this one.
///
/// # Errors
///
/// This returns an error when this date is the maximum value.
///
/// # Example
///
/// ```
/// use jiff::civil::Date;
///
/// let d = Date::constant(2024, 2, 28);
/// assert_eq!(d.tomorrow()?, Date::constant(2024, 2, 29));
///
/// // The max doesn't have a tomorrow.
/// assert!(Date::MAX.tomorrow().is_err());
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[inline]
pub fn tomorrow(self) -> Result<Date, Error> {
let day = self.to_unix_epoch_days();
let next = day.try_checked_add("days", C(1))?;
Ok(Date::from_unix_epoch_days(next))
}

/// Returns the date immediately preceding this one.
///
/// # Errors
///
/// This returns an error when this date is the minimum value.
///
/// # Example
///
/// ```
/// use jiff::civil::Date;
///
/// let d = Date::constant(2024, 3, 1);
/// assert_eq!(d.yesterday()?, Date::constant(2024, 2, 29));
///
/// // The min doesn't have a yesterday.
/// assert!(Date::MIN.yesterday().is_err());
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[inline]
pub fn yesterday(self) -> Result<Date, Error> {
let day = self.to_unix_epoch_days();
let next = day.try_checked_sub("days", C(1))?;
Ok(Date::from_unix_epoch_days(next))
}

/// Construct an [ISO 8601 week date] from this Gregorian date.
///
/// The [`ISOWeekDate`] type describes itself in more detail, but in
Expand Down
Loading

0 comments on commit a2f9480

Please sign in to comment.