cftime_rs/
calendars.rs

1//! Module defining the calendars and their methods
2
3/// Represents the different types of calendars based on the
4/// CF Conventions.
5#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Copy, Clone, Default)]
6pub enum Calendar {
7    // alias of Standard
8    #[default]
9    Standard,
10    ProlepticGregorian,
11    NoLeap,
12    AllLeap,
13    Julian,
14    Day360,
15}
16
17/// Convert the calendar to a good formatted string
18impl std::fmt::Display for Calendar {
19    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
20        let name = match *self {
21            Calendar::Standard => "Standard",
22            Calendar::ProlepticGregorian => "Proleptic Gregorian",
23            Calendar::NoLeap => "No Leap",
24            Calendar::AllLeap => "All Leap",
25            Calendar::Julian => "Julian",
26            Calendar::Day360 => "360 Day",
27        };
28        write!(f, "{name}")
29    }
30}
31
32/// Convert a valid cf unit calendar string to a Calendar
33/// If no valid string is provided, Standard is returned
34impl std::str::FromStr for Calendar {
35    type Err = crate::errors::Error;
36    fn from_str(s: &str) -> Result<Self, Self::Err> {
37        match s.trim().to_lowercase().as_str() {
38            "standard" | "gregorian" => Ok(Calendar::Standard),
39            "proleptic_gregorian" => Ok(Calendar::ProlepticGregorian),
40            "no_leap" | "day365" => Ok(Calendar::NoLeap),
41            "all_leap" | "day366" => Ok(Calendar::AllLeap),
42            "julian" => Ok(Calendar::Julian),
43            "360_day" => Ok(Calendar::Day360),
44            _ => Ok(Calendar::Standard),
45        }
46    }
47}