diff --git a/README.md b/README.md index 5a5a74b42a..48af06cfdb 100644 --- a/README.md +++ b/README.md @@ -238,8 +238,8 @@ Chrono now also provides date formatting in almost any language without the help of an additional C library. This functionality is under the feature `unstable-locales`: -```text -chrono { version = "0.4", features = ["unstable-locales"] +```toml +chrono = { version = "0.4", features = ["unstable-locales"] } ``` The `unstable-locales` feature requires and implies at least the `alloc` feature. @@ -251,8 +251,8 @@ let dt = Utc.ymd(2014, 11, 28).and_hms(12, 0, 9); assert_eq!(dt.format("%Y-%m-%d %H:%M:%S").to_string(), "2014-11-28 12:00:09"); assert_eq!(dt.format("%a %b %e %T %Y").to_string(), "Fri Nov 28 12:00:09 2014"); assert_eq!(dt.format_localized("%A %e %B %Y, %T", Locale::fr_BE).to_string(), "vendredi 28 novembre 2014, 12:00:09"); -assert_eq!(dt.format("%a %b %e %T %Y").to_string(), dt.format("%c").to_string()); +assert_eq!(dt.format("%a %b %e %T %Y").to_string(), dt.format("%c").to_string()); assert_eq!(dt.to_string(), "2014-11-28 12:00:09 UTC"); assert_eq!(dt.to_rfc2822(), "Fri, 28 Nov 2014 12:00:09 +0000"); assert_eq!(dt.to_rfc3339(), "2014-11-28T12:00:09+00:00"); diff --git a/src/datetime.rs b/src/datetime.rs index 312961069f..1b8ccbb426 100644 --- a/src/datetime.rs +++ b/src/datetime.rs @@ -310,6 +310,25 @@ impl DateTime { } } +impl Default for DateTime { + fn default() -> Self { + Utc.from_utc_datetime(&NaiveDateTime::default()) + } +} + +#[cfg(feature = "clock")] +impl Default for DateTime { + fn default() -> Self { + Local.from_utc_datetime(&NaiveDateTime::default()) + } +} + +impl Default for DateTime { + fn default() -> Self { + FixedOffset::west(0).from_utc_datetime(&NaiveDateTime::default()) + } +} + /// Convert a `DateTime` instance into a `DateTime` instance. impl From> for DateTime { /// Convert this `DateTime` instance into a `DateTime` instance. diff --git a/src/lib.rs b/src/lib.rs index 0d10c4920d..a645639eeb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -230,8 +230,8 @@ //! help of an additional C library. This functionality is under the feature //! `unstable-locales`: //! -//! ```text -//! chrono { version = "0.4", features = ["unstable-locales"] +//! ```toml +//! chrono = { version = "0.4", features = ["unstable-locales"] } //! ``` //! //! The `unstable-locales` feature requires and implies at least the `alloc` feature. @@ -239,12 +239,14 @@ //! ```rust //! use chrono::prelude::*; //! +//! # #[cfg(feature = "unstable-locales")] +//! # fn test() { //! let dt = Utc.ymd(2014, 11, 28).and_hms(12, 0, 9); //! assert_eq!(dt.format("%Y-%m-%d %H:%M:%S").to_string(), "2014-11-28 12:00:09"); //! assert_eq!(dt.format("%a %b %e %T %Y").to_string(), "Fri Nov 28 12:00:09 2014"); //! assert_eq!(dt.format_localized("%A %e %B %Y, %T", Locale::fr_BE).to_string(), "vendredi 28 novembre 2014, 12:00:09"); -//! assert_eq!(dt.format("%a %b %e %T %Y").to_string(), dt.format("%c").to_string()); //! +//! assert_eq!(dt.format("%a %b %e %T %Y").to_string(), dt.format("%c").to_string()); //! assert_eq!(dt.to_string(), "2014-11-28 12:00:09 UTC"); //! assert_eq!(dt.to_rfc2822(), "Fri, 28 Nov 2014 12:00:09 +0000"); //! assert_eq!(dt.to_rfc3339(), "2014-11-28T12:00:09+00:00"); @@ -253,6 +255,12 @@ //! // Note that milli/nanoseconds are only printed if they are non-zero //! let dt_nano = Utc.ymd(2014, 11, 28).and_hms_nano(12, 0, 9, 1); //! assert_eq!(format!("{:?}", dt_nano), "2014-11-28T12:00:09.000000001Z"); +//! # } +//! # #[cfg(not(feature = "unstable-locales"))] +//! # fn test() {} +//! # if cfg!(feature = "unstable-locales") { +//! # test(); +//! # } //! ``` //! //! Parsing can be done with three methods: diff --git a/src/naive/date.rs b/src/naive/date.rs index bfb94b497a..989254ae5f 100644 --- a/src/naive/date.rs +++ b/src/naive/date.rs @@ -1714,6 +1714,22 @@ impl str::FromStr for NaiveDate { } } +/// The default value for a NaiveDate is 1st of January 1970. +/// +/// # Example +/// +/// ```rust +/// use chrono::NaiveDate; +/// +/// let default_date = NaiveDate::default(); +/// assert_eq!(default_date, NaiveDate::from_ymd(1970, 1, 1)); +/// ``` +impl Default for NaiveDate { + fn default() -> Self { + NaiveDate::from_ymd(1970, 1, 1) + } +} + #[cfg(all(test, any(feature = "rustc-serialize", feature = "serde")))] fn test_encodable_json(to_string: F) where diff --git a/src/naive/datetime.rs b/src/naive/datetime.rs index 84b6cecd15..db23f54037 100644 --- a/src/naive/datetime.rs +++ b/src/naive/datetime.rs @@ -1511,6 +1511,23 @@ impl str::FromStr for NaiveDateTime { } } +/// The default value for a NaiveDateTime is one with epoch 0 +/// that is, 1st of January 1970 at 00:00:00. +/// +/// # Example +/// +/// ```rust +/// use chrono::NaiveDateTime; +/// +/// let default_date = NaiveDateTime::default(); +/// assert_eq!(default_date, NaiveDateTime::from_timestamp(0, 0)); +/// ``` +impl Default for NaiveDateTime { + fn default() -> Self { + NaiveDateTime::from_timestamp(0, 0) + } +} + #[cfg(all(test, any(feature = "rustc-serialize", feature = "serde")))] fn test_encodable_json(to_string: F) where diff --git a/src/naive/time.rs b/src/naive/time.rs index 4d2b5c2f51..ee44903ed2 100644 --- a/src/naive/time.rs +++ b/src/naive/time.rs @@ -1341,6 +1341,22 @@ impl str::FromStr for NaiveTime { } } +/// The default value for a NaiveTime is midnight, 00:00:00 exactly. +/// +/// # Example +/// +/// ```rust +/// use chrono::NaiveTime; +/// +/// let default_time = NaiveTime::default(); +/// assert_eq!(default_time, NaiveTime::from_hms(0, 0, 0)); +/// ``` +impl Default for NaiveTime { + fn default() -> Self { + NaiveTime::from_hms(0, 0, 0) + } +} + #[cfg(all(test, any(feature = "rustc-serialize", feature = "serde")))] fn test_encodable_json(to_string: F) where