oxedyne/fe2o3/fe2o3_datime/src/constant/day_of_week.rs
4.1 KiB, 26 runs
created by r1870400018:6337, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! Days of the week, numbered Monday 1 through Sunday 7. |
| 2 | //! |
| 3 | //! [Written with AI entirely](https://need2know.ai/entirely-ai/code)\ |
| 4 | //! Anthropic Claude |
| 5 | |
| 6 | use oxedyne_fe2o3_core::prelude::*; |
| 7 | |
| 8 | use std::fmt::{self, Display}; |
| 9 | |
| 10 | #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] |
| 11 | pub enum DayOfWeek { |
| 12 | Monday, |
| 13 | Tuesday, |
| 14 | Wednesday, |
| 15 | Thursday, |
| 16 | Friday, |
| 17 | Saturday, |
| 18 | Sunday, |
| 19 | } |
| 20 | |
| 21 | impl DayOfWeek { |
| 22 | /// Monday is 1 and Sunday 7. |
| 23 | pub fn of(&self) -> u8 { |
| 24 | match self { |
| 25 | Self::Monday => 1, |
| 26 | Self::Tuesday => 2, |
| 27 | Self::Wednesday => 3, |
| 28 | Self::Thursday => 4, |
| 29 | Self::Friday => 5, |
| 30 | Self::Saturday => 6, |
| 31 | Self::Sunday => 7, |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | pub fn from_number(n: u8) -> Outcome<Self> { |
| 36 | match n { |
| 37 | 1 => Ok(Self::Monday), |
| 38 | 2 => Ok(Self::Tuesday), |
| 39 | 3 => Ok(Self::Wednesday), |
| 40 | 4 => Ok(Self::Thursday), |
| 41 | 5 => Ok(Self::Friday), |
| 42 | 6 => Ok(Self::Saturday), |
| 43 | 7 => Ok(Self::Sunday), |
| 44 | _ => Err(err!( |
| 45 | "Invalid day of week number: {}, must be 1-7", n; |
| 46 | Invalid, Input, Range)), |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | pub fn next(&self) -> Self { |
| 51 | match self { |
| 52 | Self::Monday => Self::Tuesday, |
| 53 | Self::Tuesday => Self::Wednesday, |
| 54 | Self::Wednesday => Self::Thursday, |
| 55 | Self::Thursday => Self::Friday, |
| 56 | Self::Friday => Self::Saturday, |
| 57 | Self::Saturday => Self::Sunday, |
| 58 | Self::Sunday => Self::Monday, |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | pub fn previous(&self) -> Self { |
| 63 | match self { |
| 64 | Self::Monday => Self::Sunday, |
| 65 | Self::Tuesday => Self::Monday, |
| 66 | Self::Wednesday => Self::Tuesday, |
| 67 | Self::Thursday => Self::Wednesday, |
| 68 | Self::Friday => Self::Thursday, |
| 69 | Self::Saturday => Self::Friday, |
| 70 | Self::Sunday => Self::Saturday, |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | pub fn abbrev(&self) -> &'static str { |
| 75 | match self { |
| 76 | Self::Monday => "Mon", |
| 77 | Self::Tuesday => "Tue", |
| 78 | Self::Wednesday => "Wed", |
| 79 | Self::Thursday => "Thu", |
| 80 | Self::Friday => "Fri", |
| 81 | Self::Saturday => "Sat", |
| 82 | Self::Sunday => "Sun", |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | pub fn name(&self) -> &'static str { |
| 87 | match self { |
| 88 | Self::Monday => "Monday", |
| 89 | Self::Tuesday => "Tuesday", |
| 90 | Self::Wednesday => "Wednesday", |
| 91 | Self::Thursday => "Thursday", |
| 92 | Self::Friday => "Friday", |
| 93 | Self::Saturday => "Saturday", |
| 94 | Self::Sunday => "Sunday", |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | pub fn is_weekend(&self) -> bool { |
| 99 | matches!(self, Self::Saturday | Self::Sunday) |
| 100 | } |
| 101 | |
| 102 | pub fn is_weekday(&self) -> bool { |
| 103 | !self.is_weekend() |
| 104 | } |
| 105 | |
| 106 | pub fn short_name(&self) -> &'static str { |
| 107 | self.abbrev() |
| 108 | } |
| 109 | |
| 110 | pub fn long_name(&self) -> &'static str { |
| 111 | self.name() |
| 112 | } |
| 113 | |
| 114 | /// Matches full names and abbreviations, case insensitively. |
| 115 | pub fn from_name(name: &str) -> Option<Self> { |
| 116 | let name = name.to_lowercase(); |
| 117 | match name.as_str() { |
| 118 | "monday" | "mon" => Some(Self::Monday), |
| 119 | "tuesday" | "tue" | "tues" => Some(Self::Tuesday), |
| 120 | "wednesday" | "wed" => Some(Self::Wednesday), |
| 121 | "thursday" | "thu" | "thur" | "thurs" => Some(Self::Thursday), |
| 122 | "friday" | "fri" => Some(Self::Friday), |
| 123 | "saturday" | "sat" => Some(Self::Saturday), |
| 124 | "sunday" | "sun" => Some(Self::Sunday), |
| 125 | _ => None, |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /// Zero when the two days are the same, never seven. |
| 130 | pub fn days_until(&self, target: &Self) -> u8 { |
| 131 | let current = self.of(); |
| 132 | let target = target.of(); |
| 133 | |
| 134 | if current == target { |
| 135 | 0 |
| 136 | } else if target > current { |
| 137 | target - current |
| 138 | } else { |
| 139 | 7 - (current - target) |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | impl Display for DayOfWeek { |
| 145 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 146 | write!(f, "{}", self.name()) |
| 147 | } |
| 148 | } |