devela/phys/time/calendar/
weekday.rs
1use crate::{Display, FmtResult, Formatter, FromStr};
7#[allow(clippy::enum_glob_use)]
8use Weekday::*;
9
10#[repr(u8)]
12#[derive(Clone, Copy, Debug, PartialEq, Eq)]
13pub enum Weekday {
14 Monday = 0,
20
21 Tuesday,
26
27 Wednesday,
33
34 Thursday,
39
40 Friday,
46
47 Saturday,
52
53 Sunday,
58}
59
60impl Weekday {
61 pub const COUNT: usize = 7;
63
64 pub const fn previous(self) -> Weekday {
66 self.previous_nth(1)
67 }
68
69 pub const fn previous_nth(self, nth: usize) -> Weekday {
71 Self::from_monday_index_unchecked(self.index_from_monday().wrapping_sub(nth) % Self::COUNT)
72 }
73
74 pub const fn next(self) -> Weekday {
76 self.next_nth(1)
77 }
78
79 pub const fn next_nth(self, nth: usize) -> Weekday {
81 Self::from_monday_index_unchecked(self.index_from_monday().wrapping_add(nth) % Self::COUNT)
82 }
83}
84
85impl Weekday {
87 pub const fn number_from_monday(self) -> u8 {
91 self.index_from_monday() as u8 + 1
92 }
93
94 pub const fn index_from_monday(self) -> usize {
96 self as _
97 }
98
99 pub const fn from_monday_number(n: u8) -> Result<Weekday, &'static str> {
104 match n {
105 1 => Ok(Monday),
106 2 => Ok(Tuesday),
107 3 => Ok(Wednesday),
108 4 => Ok(Thursday),
109 5 => Ok(Friday),
110 6 => Ok(Saturday),
111 7 => Ok(Sunday),
112 _ => Err("The weekday number must be between 1 and 7."),
113 }
114 }
115
116 pub const fn from_monday_index(index: usize) -> Result<Weekday, &'static str> {
121 match index {
122 0 => Ok(Monday),
123 1 => Ok(Tuesday),
124 2 => Ok(Wednesday),
125 3 => Ok(Thursday),
126 4 => Ok(Friday),
127 5 => Ok(Saturday),
128 6 => Ok(Sunday),
129 _ => Err("The weekday number must be between 0 and 6."),
130 }
131 }
132
133 pub const fn from_monday_index_unchecked(index: usize) -> Self {
138 match index {
139 0 => Monday,
140 1 => Tuesday,
141 2 => Wednesday,
142 3 => Thursday,
143 4 => Friday,
144 5 => Saturday,
145 6 => Sunday,
146 _ => panic!("The weekday number must be between 0 and 6."),
147 }
148 }
149}
150
151impl Weekday {
153 pub const fn number_from_sunday(self) -> u8 {
155 self.index_from_sunday() as u8 + 1
156 }
157
158 pub const fn index_from_sunday(self) -> usize {
160 match self {
161 Monday => 1,
162 Tuesday => 2,
163 Wednesday => 3,
164 Thursday => 4,
165 Friday => 5,
166 Saturday => 6,
167 Sunday => 0,
168 }
169 }
170
171 pub const fn from_sunday_number(n: u8) -> Result<Weekday, &'static str> {
176 match n {
177 1 => Ok(Sunday),
178 2 => Ok(Monday),
179 3 => Ok(Tuesday),
180 4 => Ok(Wednesday),
181 5 => Ok(Thursday),
182 6 => Ok(Friday),
183 7 => Ok(Saturday),
184 _ => Err("The weekday number must be between 1 and 7."),
185 }
186 }
187
188 pub const fn from_sunday_index(index: usize) -> Result<Weekday, &'static str> {
193 match index {
194 0 => Ok(Sunday),
195 1 => Ok(Monday),
196 2 => Ok(Tuesday),
197 3 => Ok(Wednesday),
198 4 => Ok(Thursday),
199 5 => Ok(Friday),
200 6 => Ok(Saturday),
201 _ => Err("The weekday number must be between 0 and 6."),
202 }
203 }
204
205 pub const fn from_sunday_index_unchecked(index: usize) -> Self {
210 match index {
211 0 => Sunday,
212 1 => Monday,
213 2 => Tuesday,
214 3 => Wednesday,
215 4 => Thursday,
216 5 => Friday,
217 6 => Saturday,
218 _ => panic!("The weekday number must be between 0 and 6."),
219 }
220 }
221}
222
223#[allow(missing_docs, non_upper_case_globals)]
225impl Weekday {
226 pub fn abbr3(self) -> &'static str {
228 match self {
229 Monday => "Mon",
230 Tuesday => "Tue",
231 Wednesday => "Wed",
232 Thursday => "Thu",
233 Friday => "Fri",
234 Saturday => "Sat",
235 Sunday => "Sun",
236 }
237 }
238
239 pub const Mon: Weekday = Weekday::Monday;
240 pub const Tue: Weekday = Weekday::Tuesday;
241 pub const Wed: Weekday = Weekday::Wednesday;
242 pub const Thu: Weekday = Weekday::Thursday;
243 pub const Fri: Weekday = Weekday::Friday;
244 pub const Sat: Weekday = Weekday::Saturday;
245 pub const Sun: Weekday = Weekday::Sunday;
246
247 pub fn abbr2(self) -> &'static str {
249 match self {
250 Monday => "MO",
251 Tuesday => "TU",
252 Wednesday => "WE",
253 Thursday => "TH",
254 Friday => "FR",
255 Saturday => "SA",
256 Sunday => "SU",
257 }
258 }
259
260 pub const MO: Weekday = Weekday::Monday;
261 pub const TU: Weekday = Weekday::Tuesday;
262 pub const WE: Weekday = Weekday::Wednesday;
263 pub const TH: Weekday = Weekday::Thursday;
264 pub const FR: Weekday = Weekday::Friday;
265 pub const SA: Weekday = Weekday::Saturday;
266 pub const SU: Weekday = Weekday::Sunday;
267
268 pub fn abbr1(self) -> &'static str {
270 match self {
271 Monday => "M",
272 Tuesday => "T",
273 Wednesday => "W",
274 Thursday => "H",
275 Friday => "F",
276 Saturday => "A",
277 Sunday => "U",
278 }
279 }
280
281 pub const M: Weekday = Weekday::Monday;
282 pub const T: Weekday = Weekday::Tuesday;
283 pub const W: Weekday = Weekday::Wednesday;
284 pub const H: Weekday = Weekday::Thursday;
285 pub const F: Weekday = Weekday::Friday;
286 pub const A: Weekday = Weekday::Saturday;
287 pub const U: Weekday = Weekday::Sunday;
288
289 pub const fn emoji(self) -> char {
302 match self {
303 Monday => '🌕',
305 Tuesday => '🏹',
307 Wednesday => '🧙',
309 Thursday => '⚡',
311 Friday => '💕',
313 Saturday => '💰',
315 Sunday => '🌞',
317 }
318 }
319
320 pub const fn planet(self) -> char {
330 match self {
331 Monday => '☽',
333 Tuesday => '♂',
335 Wednesday => '☿',
337 Thursday => '♃',
339 Friday => '♀',
341 Saturday => '♄',
343 Sunday => '☀',
345 }
346 }
347
348 pub const fn planet_name(self) -> &'static str {
358 match self {
359 Monday => "Moon",
360 Tuesday => "Mars",
361 Wednesday => "Mercury",
362 Thursday => "Jupiter",
363 Friday => "Venus",
364 Saturday => "Saturn",
365 Sunday => "Sun",
366 }
367 }
368}
369
370impl Display for Weekday {
371 fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult<()> {
372 f.write_str(match self {
373 Monday => "Monday",
374 Tuesday => "Tuesday",
375 Wednesday => "Wednesday",
376 Thursday => "Thursday",
377 Friday => "Friday",
378 Saturday => "Saturday",
379 Sunday => "Sunday",
380 })
381 }
382}
383
384impl FromStr for Weekday {
387 type Err = &'static str;
388
389 fn from_str(s: &str) -> Result<Weekday, Self::Err> {
390 if s.eq_ignore_ascii_case("Monday") {
392 Ok(Monday)
393 } else if s.eq_ignore_ascii_case("Tuesday") {
394 Ok(Tuesday)
395 } else if s.eq_ignore_ascii_case("Wednesday") {
396 Ok(Wednesday)
397 } else if s.eq_ignore_ascii_case("Thursday") {
398 Ok(Thursday)
399 } else if s.eq_ignore_ascii_case("Friday") {
400 Ok(Friday)
401 } else if s.eq_ignore_ascii_case("Saturday") {
402 Ok(Saturday)
403 } else if s.eq_ignore_ascii_case("Sunday") {
404 Ok(Sunday)
405 } else if s.eq_ignore_ascii_case("Mon") {
407 Ok(Monday)
408 } else if s.eq_ignore_ascii_case("Tue") {
409 Ok(Tuesday)
410 } else if s.eq_ignore_ascii_case("Wed") {
411 Ok(Wednesday)
412 } else if s.eq_ignore_ascii_case("Thu") {
413 Ok(Thursday)
414 } else if s.eq_ignore_ascii_case("Fri") {
415 Ok(Friday)
416 } else if s.eq_ignore_ascii_case("Sat") {
417 Ok(Saturday)
418 } else if s.eq_ignore_ascii_case("Sun") {
419 Ok(Sunday)
420 } else if s.eq_ignore_ascii_case("MO") {
422 Ok(Monday)
423 } else if s.eq_ignore_ascii_case("TU") {
424 Ok(Tuesday)
425 } else if s.eq_ignore_ascii_case("WE") {
426 Ok(Wednesday)
427 } else if s.eq_ignore_ascii_case("TH") {
428 Ok(Thursday)
429 } else if s.eq_ignore_ascii_case("FR") {
430 Ok(Friday)
431 } else if s.eq_ignore_ascii_case("SA") {
432 Ok(Saturday)
433 } else if s.eq_ignore_ascii_case("SU") {
434 Ok(Sunday)
435 } else if s.eq_ignore_ascii_case("M") {
437 Ok(Monday)
438 } else if s.eq_ignore_ascii_case("T") {
439 Ok(Tuesday)
440 } else if s.eq_ignore_ascii_case("W") {
441 Ok(Wednesday)
442 } else if s.eq_ignore_ascii_case("H") {
443 Ok(Thursday)
444 } else if s.eq_ignore_ascii_case("F") {
445 Ok(Friday)
446 } else if s.eq_ignore_ascii_case("S") {
447 Ok(Saturday)
448 } else if s.eq_ignore_ascii_case("U") {
449 Ok(Sunday)
450
451 } else {
453 Err("Invalid weekday name.")
454 }
455 }
456}