From e02c5b5930c2c9ba3e5423fe12e2ef0155017297 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Thu, 30 May 2024 20:31:36 +0200 Subject: Merging upstream version 1.74.1+dfsg1. Signed-off-by: Daniel Baumann --- vendor/time/src/weekday.rs | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) (limited to 'vendor/time/src/weekday.rs') diff --git a/vendor/time/src/weekday.rs b/vendor/time/src/weekday.rs index d530a2e4d..07642498d 100644 --- a/vendor/time/src/weekday.rs +++ b/vendor/time/src/weekday.rs @@ -13,19 +13,19 @@ use crate::error; /// Friday), this type does not implement `PartialOrd` or `Ord`. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub enum Weekday { - #[allow(clippy::missing_docs_in_private_items)] + #[allow(missing_docs)] Monday, - #[allow(clippy::missing_docs_in_private_items)] + #[allow(missing_docs)] Tuesday, - #[allow(clippy::missing_docs_in_private_items)] + #[allow(missing_docs)] Wednesday, - #[allow(clippy::missing_docs_in_private_items)] + #[allow(missing_docs)] Thursday, - #[allow(clippy::missing_docs_in_private_items)] + #[allow(missing_docs)] Friday, - #[allow(clippy::missing_docs_in_private_items)] + #[allow(missing_docs)] Saturday, - #[allow(clippy::missing_docs_in_private_items)] + #[allow(missing_docs)] Sunday, } @@ -88,6 +88,28 @@ impl Weekday { } } + /// Get n-th previous day. + /// + /// ```rust + /// # use time::Weekday; + /// assert_eq!(Weekday::Monday.nth_prev(1), Weekday::Sunday); + /// assert_eq!(Weekday::Sunday.nth_prev(10), Weekday::Thursday); + /// ``` + pub const fn nth_prev(self, n: u8) -> Self { + match self.number_days_from_monday() as i8 - (n % 7) as i8 { + 1 | -6 => Tuesday, + 2 | -5 => Wednesday, + 3 | -4 => Thursday, + 4 | -3 => Friday, + 5 | -2 => Saturday, + 6 | -1 => Sunday, + val => { + debug_assert!(val == 0); + Monday + } + } + } + /// Get the one-indexed number of days from Monday. /// /// ```rust -- cgit v1.2.3