/rust/registry/src/index.crates.io-1949cf8c6b5b557f/hifitime-4.2.3/src/month.rs
Line | Count | Source |
1 | | /* |
2 | | * Hifitime |
3 | | * Copyright (C) 2017-onward Christopher Rabotin <christopher.rabotin@gmail.com> et al. (cf. https://github.com/nyx-space/hifitime/graphs/contributors) |
4 | | * This Source Code Form is subject to the terms of the Mozilla Public |
5 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
6 | | * file, You can obtain one at https://mozilla.org/MPL/2.0/. |
7 | | * |
8 | | * Documentation: https://nyxspace.com/ |
9 | | */ |
10 | | |
11 | | use crate::ParsingError; |
12 | | use core::fmt; |
13 | | use core::str::FromStr; |
14 | | |
15 | | #[cfg(feature = "python")] |
16 | | use pyo3::prelude::*; |
17 | | |
18 | | #[cfg(feature = "serde")] |
19 | | use serde_derive::{Deserialize, Serialize}; |
20 | | |
21 | | /// Defines Month names, can be initialized either from its variant or its integer (1 for January). |
22 | | /// |
23 | | /// :type month: int |
24 | | #[cfg_attr(kani, derive(kani::Arbitrary))] |
25 | | #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] |
26 | | #[repr(u8)] |
27 | | #[cfg_attr(feature = "python", pyclass(eq, eq_int))] |
28 | | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
29 | | #[derive(Default)] |
30 | | pub enum MonthName { |
31 | | #[default] |
32 | | January, |
33 | | February, |
34 | | March, |
35 | | April, |
36 | | May, |
37 | | June, |
38 | | July, |
39 | | August, |
40 | | September, |
41 | | October, |
42 | | November, |
43 | | December, |
44 | | } |
45 | | |
46 | | #[cfg(feature = "python")] |
47 | | #[pymethods] |
48 | | impl MonthName { |
49 | | /// Convert the MonthName to integer |
50 | | /// |
51 | | /// :rtype: int |
52 | | fn __int__(&self) -> u8 { |
53 | | *self as u8 + 1 |
54 | | } |
55 | | |
56 | | #[new] |
57 | | fn py_new(month: u8) -> Self { |
58 | | month.into() |
59 | | } |
60 | | } |
61 | | |
62 | | impl FromStr for MonthName { |
63 | | type Err = ParsingError; |
64 | 0 | fn from_str(s: &str) -> Result<Self, Self::Err> { |
65 | 0 | match s.trim() { |
66 | 0 | "jan" | "Jan" | "JAN" | "January" | "JANUARY" | "january" => Ok(Self::January), |
67 | 0 | "feb" | "Feb" | "FEB" | "February" | "FEBRUARY" | "february" => Ok(Self::February), |
68 | 0 | "mar" | "Mar" | "MAR" | "March" | "MARCH" | "march" => Ok(Self::March), |
69 | 0 | "apr" | "Apr" | "APR" | "April" | "APRIL" | "april" => Ok(Self::April), |
70 | 0 | "may" | "May" | "MAY" => Ok(Self::May), |
71 | 0 | "jun" | "Jun" | "JUN" | "June" | "JUNE" | "june" => Ok(Self::June), |
72 | 0 | "jul" | "Jul" | "JUL" | "July" | "JULY" | "july" => Ok(Self::July), |
73 | 0 | "aug" | "Aug" | "AUG" | "August" | "AUGUST" | "august" => Ok(Self::August), |
74 | 0 | "sep" | "Sep" | "SEP" | "September" | "SEPTEMBER" | "september" => Ok(Self::September), |
75 | 0 | "oct" | "Oct" | "OCT" | "October" | "OCTOBER" | "october" => Ok(Self::October), |
76 | 0 | "nov" | "Nov" | "NOV" | "November" | "NOVEMBER" | "november" => Ok(Self::November), |
77 | 0 | "dec" | "Dec" | "DEC" | "December" | "DECEMBER" | "december" => Ok(Self::December), |
78 | 0 | _ => Err(ParsingError::UnknownMonthName), |
79 | | } |
80 | 0 | } |
81 | | } |
82 | | |
83 | | impl From<u8> for MonthName { |
84 | 0 | fn from(u: u8) -> Self { |
85 | 0 | match u { |
86 | 0 | 1 => Self::January, |
87 | 0 | 2 => Self::February, |
88 | 0 | 3 => Self::March, |
89 | 0 | 4 => Self::April, |
90 | 0 | 5 => Self::May, |
91 | 0 | 6 => Self::June, |
92 | 0 | 7 => Self::July, |
93 | 0 | 8 => Self::August, |
94 | 0 | 9 => Self::September, |
95 | 0 | 10 => Self::October, |
96 | 0 | 11 => Self::November, |
97 | 0 | 12 => Self::December, |
98 | 0 | _ => Self::default(), // Defaults back to default for other values. |
99 | | } |
100 | 0 | } |
101 | | } |
102 | | |
103 | | impl fmt::Display for MonthName { |
104 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
105 | 0 | write!(f, "{self:?}") |
106 | 0 | } |
107 | | } |
108 | | |
109 | | /// LowerHex allows printing the week day in its shortened form in English |
110 | | impl fmt::LowerHex for MonthName { |
111 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
112 | 0 | match self { |
113 | 0 | Self::January => write!(f, "Jan"), |
114 | 0 | Self::February => write!(f, "Feb"), |
115 | 0 | Self::March => write!(f, "Mar"), |
116 | 0 | Self::April => write!(f, "Apr"), |
117 | 0 | Self::May => write!(f, "May"), |
118 | 0 | Self::June => write!(f, "Jun"), |
119 | 0 | Self::July => write!(f, "Jul"), |
120 | 0 | Self::August => write!(f, "Aug"), |
121 | 0 | Self::September => write!(f, "Sep"), |
122 | 0 | Self::October => write!(f, "Oct"), |
123 | 0 | Self::November => write!(f, "Nov"), |
124 | 0 | Self::December => write!(f, "Dec"), |
125 | | } |
126 | 0 | } |
127 | | } |