Coverage Report

Created: 2026-09-14 06:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/time-0.3.55/src/weekday.rs
Line
Count
Source
1
//! Days of the week.
2
3
use core::fmt;
4
use core::str::FromStr;
5
6
use powerfmt::smart_display::{FormatterOptions, Metadata, SmartDisplay};
7
8
use self::Weekday::*;
9
use crate::error;
10
use crate::iter::WeekdayIter;
11
12
/// Days of the week.
13
///
14
/// As order is dependent on context (Sunday could be either two days after or five days before
15
/// Friday), this type does not implement `PartialOrd` or `Ord`.
16
#[repr(u8)]
17
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
18
pub enum Weekday {
19
    #[expect(missing_docs)]
20
    Monday,
21
    #[expect(missing_docs)]
22
    Tuesday,
23
    #[expect(missing_docs)]
24
    Wednesday,
25
    #[expect(missing_docs)]
26
    Thursday,
27
    #[expect(missing_docs)]
28
    Friday,
29
    #[expect(missing_docs)]
30
    Saturday,
31
    #[expect(missing_docs)]
32
    Sunday,
33
}
34
35
impl Weekday {
36
    /// Get the previous weekday.
37
    ///
38
    /// ```rust
39
    /// # use time::Weekday;
40
    /// assert_eq!(Weekday::Tuesday.previous(), Weekday::Monday);
41
    /// ```
42
    #[inline]
43
0
    pub const fn previous(self) -> Self {
44
0
        match self {
45
0
            Monday => Sunday,
46
0
            Tuesday => Monday,
47
0
            Wednesday => Tuesday,
48
0
            Thursday => Wednesday,
49
0
            Friday => Thursday,
50
0
            Saturday => Friday,
51
0
            Sunday => Saturday,
52
        }
53
0
    }
54
55
    /// Get the next weekday.
56
    ///
57
    /// ```rust
58
    /// # use time::Weekday;
59
    /// assert_eq!(Weekday::Monday.next(), Weekday::Tuesday);
60
    /// ```
61
    #[inline]
62
0
    pub const fn next(self) -> Self {
63
0
        match self {
64
0
            Monday => Tuesday,
65
0
            Tuesday => Wednesday,
66
0
            Wednesday => Thursday,
67
0
            Thursday => Friday,
68
0
            Friday => Saturday,
69
0
            Saturday => Sunday,
70
0
            Sunday => Monday,
71
        }
72
0
    }
73
74
    /// Get n-th next day.
75
    ///
76
    /// ```rust
77
    /// # use time::Weekday;
78
    /// assert_eq!(Weekday::Monday.nth_next(1), Weekday::Tuesday);
79
    /// assert_eq!(Weekday::Sunday.nth_next(10), Weekday::Wednesday);
80
    /// ```
81
    #[inline]
82
0
    pub const fn nth_next(self, n: u8) -> Self {
83
0
        match (self.number_days_from_monday() + n % 7) % 7 {
84
0
            0 => Monday,
85
0
            1 => Tuesday,
86
0
            2 => Wednesday,
87
0
            3 => Thursday,
88
0
            4 => Friday,
89
0
            5 => Saturday,
90
0
            val => {
91
0
                debug_assert!(val == 6);
92
0
                Sunday
93
            }
94
        }
95
0
    }
96
97
    /// Get n-th previous day.
98
    ///
99
    /// ```rust
100
    /// # use time::Weekday;
101
    /// assert_eq!(Weekday::Monday.nth_prev(1), Weekday::Sunday);
102
    /// assert_eq!(Weekday::Sunday.nth_prev(10), Weekday::Thursday);
103
    /// ```
104
    #[inline]
105
0
    pub const fn nth_prev(self, n: u8) -> Self {
106
0
        match self.number_days_from_monday().cast_signed() - (n % 7).cast_signed() {
107
0
            1 | -6 => Tuesday,
108
0
            2 | -5 => Wednesday,
109
0
            3 | -4 => Thursday,
110
0
            4 | -3 => Friday,
111
0
            5 | -2 => Saturday,
112
0
            6 | -1 => Sunday,
113
0
            val => {
114
0
                debug_assert!(val == 0);
115
0
                Monday
116
            }
117
        }
118
0
    }
119
120
    /// Get the one-indexed number of days from Monday.
121
    ///
122
    /// ```rust
123
    /// # use time::Weekday;
124
    /// assert_eq!(Weekday::Monday.number_from_monday(), 1);
125
    /// ```
126
    #[doc(alias = "iso_weekday_number")]
127
    #[inline]
128
0
    pub const fn number_from_monday(self) -> u8 {
129
0
        self.number_days_from_monday() + 1
130
0
    }
131
132
    /// Get the one-indexed number of days from Sunday.
133
    ///
134
    /// ```rust
135
    /// # use time::Weekday;
136
    /// assert_eq!(Weekday::Monday.number_from_sunday(), 2);
137
    /// ```
138
    #[inline]
139
0
    pub const fn number_from_sunday(self) -> u8 {
140
0
        self.number_days_from_sunday() + 1
141
0
    }
142
143
    /// Get the zero-indexed number of days from Monday.
144
    ///
145
    /// ```rust
146
    /// # use time::Weekday;
147
    /// assert_eq!(Weekday::Monday.number_days_from_monday(), 0);
148
    /// ```
149
    #[inline]
150
0
    pub const fn number_days_from_monday(self) -> u8 {
151
0
        self as u8
152
0
    }
153
154
    /// Get the zero-indexed number of days from Sunday.
155
    ///
156
    /// ```rust
157
    /// # use time::Weekday;
158
    /// assert_eq!(Weekday::Monday.number_days_from_sunday(), 1);
159
    /// ```
160
    #[inline]
161
0
    pub const fn number_days_from_sunday(self) -> u8 {
162
0
        match self {
163
0
            Monday => 1,
164
0
            Tuesday => 2,
165
0
            Wednesday => 3,
166
0
            Thursday => 4,
167
0
            Friday => 5,
168
0
            Saturday => 6,
169
0
            Sunday => 0,
170
        }
171
0
    }
172
173
    /// Create an infinite iterator starting at this weekday.
174
    ///
175
    /// ```rust
176
    /// # use time::Weekday;
177
    /// let mut iter = Weekday::iter_from(Weekday::Monday);
178
    /// assert_eq!(iter.next(), Some(Weekday::Monday));
179
    /// assert_eq!(iter.next(), Some(Weekday::Tuesday));
180
    /// assert_eq!(iter.next(), Some(Weekday::Wednesday));
181
    /// assert_eq!(iter.next(), Some(Weekday::Thursday));
182
    /// assert_eq!(iter.next(), Some(Weekday::Friday));
183
    /// assert_eq!(iter.next(), Some(Weekday::Saturday));
184
    /// assert_eq!(iter.next(), Some(Weekday::Sunday));
185
    /// assert_eq!(iter.next(), Some(Weekday::Monday));
186
    /// // … continuing forever
187
    /// ```
188
    #[inline]
189
0
    pub const fn iter_from(start: Self) -> WeekdayIter {
190
0
        WeekdayIter::new(start)
191
0
    }
192
}
193
194
impl SmartDisplay for Weekday {
195
    type Metadata = ();
196
197
    #[inline]
198
0
    fn metadata(&self, _: FormatterOptions) -> Metadata<'_, Self> {
199
0
        match self {
200
0
            Monday => Metadata::new(6, self, ()),
201
0
            Tuesday => Metadata::new(7, self, ()),
202
0
            Wednesday => Metadata::new(9, self, ()),
203
0
            Thursday => Metadata::new(8, self, ()),
204
0
            Friday => Metadata::new(6, self, ()),
205
0
            Saturday => Metadata::new(8, self, ()),
206
0
            Sunday => Metadata::new(6, self, ()),
207
        }
208
0
    }
209
210
    #[inline]
211
0
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
212
0
        f.pad(match self {
213
0
            Monday => "Monday",
214
0
            Tuesday => "Tuesday",
215
0
            Wednesday => "Wednesday",
216
0
            Thursday => "Thursday",
217
0
            Friday => "Friday",
218
0
            Saturday => "Saturday",
219
0
            Sunday => "Sunday",
220
        })
221
0
    }
222
}
223
224
impl fmt::Display for Weekday {
225
    #[inline]
226
0
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
227
0
        SmartDisplay::fmt(self, f)
228
0
    }
229
}
230
231
impl FromStr for Weekday {
232
    type Err = error::InvalidVariant;
233
234
    #[inline]
235
0
    fn from_str(s: &str) -> Result<Self, Self::Err> {
236
0
        match s {
237
0
            "Monday" => Ok(Monday),
238
0
            "Tuesday" => Ok(Tuesday),
239
0
            "Wednesday" => Ok(Wednesday),
240
0
            "Thursday" => Ok(Thursday),
241
0
            "Friday" => Ok(Friday),
242
0
            "Saturday" => Ok(Saturday),
243
0
            "Sunday" => Ok(Sunday),
244
0
            _ => Err(error::InvalidVariant),
245
        }
246
0
    }
247
}