Skip to content

Commit

Permalink
Merge pull request #506 from DiD92/add-default-impl
Browse files Browse the repository at this point in the history
Add Default implementation for naive date types
  • Loading branch information
quodlibetor authored Jan 22, 2021
2 parents 19ec092 + a1cbbaa commit 3467172
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 6 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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");
Expand Down
19 changes: 19 additions & 0 deletions src/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,25 @@ impl<Tz: TimeZone> DateTime<Tz> {
}
}

impl Default for DateTime<Utc> {
fn default() -> Self {
Utc.from_utc_datetime(&NaiveDateTime::default())
}
}

#[cfg(feature = "clock")]
impl Default for DateTime<Local> {
fn default() -> Self {
Local.from_utc_datetime(&NaiveDateTime::default())
}
}

impl Default for DateTime<FixedOffset> {
fn default() -> Self {
FixedOffset::west(0).from_utc_datetime(&NaiveDateTime::default())
}
}

/// Convert a `DateTime<Utc>` instance into a `DateTime<FixedOffset>` instance.
impl From<DateTime<Utc>> for DateTime<FixedOffset> {
/// Convert this `DateTime<Utc>` instance into a `DateTime<FixedOffset>` instance.
Expand Down
14 changes: 11 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,21 +230,23 @@
//! 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.
//!
//! ```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");
Expand All @@ -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:
Expand Down
16 changes: 16 additions & 0 deletions src/naive/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<F, E>(to_string: F)
where
Expand Down
17 changes: 17 additions & 0 deletions src/naive/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<F, E>(to_string: F)
where
Expand Down
16 changes: 16 additions & 0 deletions src/naive/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<F, E>(to_string: F)
where
Expand Down

0 comments on commit 3467172

Please sign in to comment.