Skip to content

Commit

Permalink
Make compatible with no_std
Browse files Browse the repository at this point in the history
  • Loading branch information
quodlibetor authored and botahamec committed May 26, 2022
1 parent 7603b2e commit 788f0db
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,13 @@ use core::str::FromStr;
#[cfg(any(feature = "std", test))]
use std::error::Error;

use div::{div_floor, mod_floor};
#[cfg(any(feature = "alloc", feature = "std", test))]
use naive::{NaiveDate, NaiveTime};
#[cfg(any(feature = "alloc", feature = "std", test))]
use offset::{FixedOffset, Offset};
#[cfg(any(feature = "alloc", feature = "std", test))]
use {Datelike, Month, Timelike, Weekday};
use {ParseMonthError, ParseWeekdayError};
use {Datelike, Timelike};
use {Month, ParseMonthError, ParseWeekdayError, Weekday};

pub use self::parse::parse;
pub use self::parsed::Parsed;
Expand Down Expand Up @@ -421,6 +420,7 @@ fn format_inner<'a>(
["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];

use core::fmt::Write;
use div::{div_floor, mod_floor};

match *item {
Item::Literal(s) | Item::Space(s) => result.push_str(s),
Expand Down
44 changes: 35 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,30 @@ impl Month {
Month::December => 12,
}
}

/// Get the name of the month
///
/// ```
/// use chrono::Month;
///
/// assert_eq!(Month::January.name(), "January")
/// ```
pub fn name(&self) -> &'static str {
match *self {
Month::January => "January",
Month::February => "February",
Month::March => "March",
Month::April => "April",
Month::May => "May",
Month::June => "June",
Month::July => "July",
Month::August => "August",
Month::September => "September",
Month::October => "October",
Month::November => "November",
Month::December => "December",
}
}
}

impl num_traits::FromPrimitive for Month {
Expand Down Expand Up @@ -1070,18 +1094,20 @@ impl fmt::Debug for ParseMonthError {
write!(f, "ParseMonthError {{ .. }}")
}
}

#[cfg(feature = "serde")]
mod month_serde {
use super::Month;
use serdelib::{de, ser};
use std::fmt;

use core::fmt;

impl ser::Serialize for Month {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: ser::Serializer,
{
serializer.serialize_str(&format!("{:?}", self))
serializer.collect_str(self.name())
}
}

Expand Down Expand Up @@ -1459,12 +1485,12 @@ fn test_num_days_from_ce_against_alternative_impl() {
let mid_year = jan1_year + Duration::days(133);
assert_eq!(mid_year.num_days_from_ce(), num_days_from_ce(&mid_year), "on {:?}", mid_year);
}
}

#[test]
fn test_month_enum_succ_pred() {
assert_eq!(Month::January.succ(), Month::February);
assert_eq!(Month::December.succ(), Month::January);
assert_eq!(Month::January.pred(), Month::December);
assert_eq!(Month::February.pred(), Month::January);
}
#[test]
fn test_month_enum_succ_pred() {
assert_eq!(Month::January.succ(), Month::February);
assert_eq!(Month::December.succ(), Month::January);
assert_eq!(Month::January.pred(), Month::December);
assert_eq!(Month::February.pred(), Month::January);
}

0 comments on commit 788f0db

Please sign in to comment.