diff --git a/src/format/mod.rs b/src/format/mod.rs index a1f197ec46..459c201ae2 100644 --- a/src/format/mod.rs +++ b/src/format/mod.rs @@ -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; @@ -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), diff --git a/src/lib.rs b/src/lib.rs index 4a383d92cb..7cebccfaf5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 { @@ -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(&self, serializer: S) -> Result where S: ser::Serializer, { - serializer.serialize_str(&format!("{:?}", self)) + serializer.collect_str(self.name()) } } @@ -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); }