mirror of
https://github.com/chronotope/chrono.git
synced 2025-09-28 05:21:39 +00:00
Rustfmt doctests
This commit is contained in:
parent
60f8b742c9
commit
45f8cecabc
@ -81,7 +81,7 @@ impl<Tz: TimeZone> DateTime<Tz> {
|
||||
///
|
||||
#[cfg_attr(not(feature = "clock"), doc = "```ignore")]
|
||||
#[cfg_attr(feature = "clock", doc = "```rust")]
|
||||
/// use chrono::{Local, DateTime};
|
||||
/// use chrono::{DateTime, Local};
|
||||
///
|
||||
/// let dt = Local::now();
|
||||
/// // Get components
|
||||
@ -166,7 +166,8 @@ impl<Tz: TimeZone> DateTime<Tz> {
|
||||
/// use chrono::prelude::*;
|
||||
///
|
||||
/// let date: DateTime<Utc> = Utc.with_ymd_and_hms(2020, 1, 1, 0, 0, 0).unwrap();
|
||||
/// let other: DateTime<FixedOffset> = FixedOffset::east_opt(23).unwrap().with_ymd_and_hms(2020, 1, 1, 0, 0, 0).unwrap();
|
||||
/// let other: DateTime<FixedOffset> =
|
||||
/// FixedOffset::east_opt(23).unwrap().with_ymd_and_hms(2020, 1, 1, 0, 0, 0).unwrap();
|
||||
/// assert_eq!(date.date_naive(), other.date_naive());
|
||||
/// ```
|
||||
#[inline]
|
||||
@ -208,12 +209,22 @@ impl<Tz: TimeZone> DateTime<Tz> {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{Utc, NaiveDate};
|
||||
/// use chrono::{NaiveDate, Utc};
|
||||
///
|
||||
/// let dt = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap().and_hms_milli_opt(0, 0, 1, 444).unwrap().and_local_timezone(Utc).unwrap();
|
||||
/// let dt = NaiveDate::from_ymd_opt(1970, 1, 1)
|
||||
/// .unwrap()
|
||||
/// .and_hms_milli_opt(0, 0, 1, 444)
|
||||
/// .unwrap()
|
||||
/// .and_local_timezone(Utc)
|
||||
/// .unwrap();
|
||||
/// assert_eq!(dt.timestamp_millis(), 1_444);
|
||||
///
|
||||
/// let dt = NaiveDate::from_ymd_opt(2001, 9, 9).unwrap().and_hms_milli_opt(1, 46, 40, 555).unwrap().and_local_timezone(Utc).unwrap();
|
||||
/// let dt = NaiveDate::from_ymd_opt(2001, 9, 9)
|
||||
/// .unwrap()
|
||||
/// .and_hms_milli_opt(1, 46, 40, 555)
|
||||
/// .unwrap()
|
||||
/// .and_local_timezone(Utc)
|
||||
/// .unwrap();
|
||||
/// assert_eq!(dt.timestamp_millis(), 1_000_000_000_555);
|
||||
/// ```
|
||||
#[inline]
|
||||
@ -227,12 +238,22 @@ impl<Tz: TimeZone> DateTime<Tz> {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{Utc, NaiveDate};
|
||||
/// use chrono::{NaiveDate, Utc};
|
||||
///
|
||||
/// let dt = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap().and_hms_micro_opt(0, 0, 1, 444).unwrap().and_local_timezone(Utc).unwrap();
|
||||
/// let dt = NaiveDate::from_ymd_opt(1970, 1, 1)
|
||||
/// .unwrap()
|
||||
/// .and_hms_micro_opt(0, 0, 1, 444)
|
||||
/// .unwrap()
|
||||
/// .and_local_timezone(Utc)
|
||||
/// .unwrap();
|
||||
/// assert_eq!(dt.timestamp_micros(), 1_000_444);
|
||||
///
|
||||
/// let dt = NaiveDate::from_ymd_opt(2001, 9, 9).unwrap().and_hms_micro_opt(1, 46, 40, 555).unwrap().and_local_timezone(Utc).unwrap();
|
||||
/// let dt = NaiveDate::from_ymd_opt(2001, 9, 9)
|
||||
/// .unwrap()
|
||||
/// .and_hms_micro_opt(1, 46, 40, 555)
|
||||
/// .unwrap()
|
||||
/// .and_local_timezone(Utc)
|
||||
/// .unwrap();
|
||||
/// assert_eq!(dt.timestamp_micros(), 1_000_000_000_000_555);
|
||||
/// ```
|
||||
#[inline]
|
||||
@ -271,24 +292,54 @@ impl<Tz: TimeZone> DateTime<Tz> {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{Utc, NaiveDate};
|
||||
/// use chrono::{NaiveDate, Utc};
|
||||
///
|
||||
/// let dt = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap().and_hms_nano_opt(0, 0, 1, 444).unwrap().and_local_timezone(Utc).unwrap();
|
||||
/// let dt = NaiveDate::from_ymd_opt(1970, 1, 1)
|
||||
/// .unwrap()
|
||||
/// .and_hms_nano_opt(0, 0, 1, 444)
|
||||
/// .unwrap()
|
||||
/// .and_local_timezone(Utc)
|
||||
/// .unwrap();
|
||||
/// assert_eq!(dt.timestamp_nanos_opt(), Some(1_000_000_444));
|
||||
///
|
||||
/// let dt = NaiveDate::from_ymd_opt(2001, 9, 9).unwrap().and_hms_nano_opt(1, 46, 40, 555).unwrap().and_local_timezone(Utc).unwrap();
|
||||
/// let dt = NaiveDate::from_ymd_opt(2001, 9, 9)
|
||||
/// .unwrap()
|
||||
/// .and_hms_nano_opt(1, 46, 40, 555)
|
||||
/// .unwrap()
|
||||
/// .and_local_timezone(Utc)
|
||||
/// .unwrap();
|
||||
/// assert_eq!(dt.timestamp_nanos_opt(), Some(1_000_000_000_000_000_555));
|
||||
///
|
||||
/// let dt = NaiveDate::from_ymd_opt(1677, 9, 21).unwrap().and_hms_nano_opt(0, 12, 43, 145_224_192).unwrap().and_local_timezone(Utc).unwrap();
|
||||
/// let dt = NaiveDate::from_ymd_opt(1677, 9, 21)
|
||||
/// .unwrap()
|
||||
/// .and_hms_nano_opt(0, 12, 43, 145_224_192)
|
||||
/// .unwrap()
|
||||
/// .and_local_timezone(Utc)
|
||||
/// .unwrap();
|
||||
/// assert_eq!(dt.timestamp_nanos_opt(), Some(-9_223_372_036_854_775_808));
|
||||
///
|
||||
/// let dt = NaiveDate::from_ymd_opt(2262, 4, 11).unwrap().and_hms_nano_opt(23, 47, 16, 854_775_807).unwrap().and_local_timezone(Utc).unwrap();
|
||||
/// let dt = NaiveDate::from_ymd_opt(2262, 4, 11)
|
||||
/// .unwrap()
|
||||
/// .and_hms_nano_opt(23, 47, 16, 854_775_807)
|
||||
/// .unwrap()
|
||||
/// .and_local_timezone(Utc)
|
||||
/// .unwrap();
|
||||
/// assert_eq!(dt.timestamp_nanos_opt(), Some(9_223_372_036_854_775_807));
|
||||
///
|
||||
/// let dt = NaiveDate::from_ymd_opt(1677, 9, 21).unwrap().and_hms_nano_opt(0, 12, 43, 145_224_191).unwrap().and_local_timezone(Utc).unwrap();
|
||||
/// let dt = NaiveDate::from_ymd_opt(1677, 9, 21)
|
||||
/// .unwrap()
|
||||
/// .and_hms_nano_opt(0, 12, 43, 145_224_191)
|
||||
/// .unwrap()
|
||||
/// .and_local_timezone(Utc)
|
||||
/// .unwrap();
|
||||
/// assert_eq!(dt.timestamp_nanos_opt(), None);
|
||||
///
|
||||
/// let dt = NaiveDate::from_ymd_opt(2262, 4, 11).unwrap().and_hms_nano_opt(23, 47, 16, 854_775_808).unwrap().and_local_timezone(Utc).unwrap();
|
||||
/// let dt = NaiveDate::from_ymd_opt(2262, 4, 11)
|
||||
/// .unwrap()
|
||||
/// .and_hms_nano_opt(23, 47, 16, 854_775_808)
|
||||
/// .unwrap()
|
||||
/// .and_local_timezone(Utc)
|
||||
/// .unwrap();
|
||||
/// assert_eq!(dt.timestamp_nanos_opt(), None);
|
||||
/// ```
|
||||
#[inline]
|
||||
@ -566,18 +617,26 @@ impl<Tz: TimeZone> DateTime<Tz> {
|
||||
///
|
||||
/// ```rust
|
||||
/// # use chrono::{FixedOffset, SecondsFormat, TimeZone, Utc, NaiveDate};
|
||||
/// let dt = NaiveDate::from_ymd_opt(2018, 1, 26).unwrap().and_hms_micro_opt(18, 30, 9, 453_829).unwrap().and_local_timezone(Utc).unwrap();
|
||||
/// assert_eq!(dt.to_rfc3339_opts(SecondsFormat::Millis, false),
|
||||
/// "2018-01-26T18:30:09.453+00:00");
|
||||
/// assert_eq!(dt.to_rfc3339_opts(SecondsFormat::Millis, true),
|
||||
/// "2018-01-26T18:30:09.453Z");
|
||||
/// assert_eq!(dt.to_rfc3339_opts(SecondsFormat::Secs, true),
|
||||
/// "2018-01-26T18:30:09Z");
|
||||
/// let dt = NaiveDate::from_ymd_opt(2018, 1, 26)
|
||||
/// .unwrap()
|
||||
/// .and_hms_micro_opt(18, 30, 9, 453_829)
|
||||
/// .unwrap()
|
||||
/// .and_local_timezone(Utc)
|
||||
/// .unwrap();
|
||||
/// assert_eq!(dt.to_rfc3339_opts(SecondsFormat::Millis, false), "2018-01-26T18:30:09.453+00:00");
|
||||
/// assert_eq!(dt.to_rfc3339_opts(SecondsFormat::Millis, true), "2018-01-26T18:30:09.453Z");
|
||||
/// assert_eq!(dt.to_rfc3339_opts(SecondsFormat::Secs, true), "2018-01-26T18:30:09Z");
|
||||
///
|
||||
/// let pst = FixedOffset::east_opt(8 * 60 * 60).unwrap();
|
||||
/// let dt = pst.from_local_datetime(&NaiveDate::from_ymd_opt(2018, 1, 26).unwrap().and_hms_micro_opt(10, 30, 9, 453_829).unwrap()).unwrap();
|
||||
/// assert_eq!(dt.to_rfc3339_opts(SecondsFormat::Secs, true),
|
||||
/// "2018-01-26T10:30:09+08:00");
|
||||
/// let dt = pst
|
||||
/// .from_local_datetime(
|
||||
/// &NaiveDate::from_ymd_opt(2018, 1, 26)
|
||||
/// .unwrap()
|
||||
/// .and_hms_micro_opt(10, 30, 9, 453_829)
|
||||
/// .unwrap(),
|
||||
/// )
|
||||
/// .unwrap();
|
||||
/// assert_eq!(dt.to_rfc3339_opts(SecondsFormat::Secs, true), "2018-01-26T10:30:09+08:00");
|
||||
/// ```
|
||||
#[cfg(feature = "alloc")]
|
||||
#[must_use]
|
||||
@ -619,7 +678,8 @@ impl DateTime<Utc> {
|
||||
/// ```
|
||||
/// use chrono::{DateTime, Utc};
|
||||
///
|
||||
/// let dt: DateTime<Utc> = DateTime::<Utc>::from_timestamp(1431648000, 0).expect("invalid timestamp");
|
||||
/// let dt: DateTime<Utc> =
|
||||
/// DateTime::<Utc>::from_timestamp(1431648000, 0).expect("invalid timestamp");
|
||||
///
|
||||
/// assert_eq!(dt.to_string(), "2015-05-15 00:00:00 UTC");
|
||||
/// assert_eq!(DateTime::from_timestamp(dt.timestamp(), dt.timestamp_subsec_nanos()).unwrap(), dt);
|
||||
@ -650,7 +710,8 @@ impl DateTime<Utc> {
|
||||
/// ```
|
||||
/// use chrono::{DateTime, Utc};
|
||||
///
|
||||
/// let dt: DateTime<Utc> = DateTime::<Utc>::from_timestamp_millis(947638923004).expect("invalid timestamp");
|
||||
/// let dt: DateTime<Utc> =
|
||||
/// DateTime::<Utc>::from_timestamp_millis(947638923004).expect("invalid timestamp");
|
||||
///
|
||||
/// assert_eq!(dt.to_string(), "2000-01-12 01:02:03.004 UTC");
|
||||
/// assert_eq!(DateTime::from_timestamp_millis(dt.timestamp_millis()).unwrap(), dt);
|
||||
@ -837,11 +898,21 @@ impl DateTime<FixedOffset> {
|
||||
/// # Example
|
||||
///
|
||||
/// ```rust
|
||||
/// use chrono::{DateTime, FixedOffset, TimeZone, NaiveDate};
|
||||
/// use chrono::{DateTime, FixedOffset, NaiveDate, TimeZone};
|
||||
///
|
||||
/// let dt = DateTime::parse_from_str(
|
||||
/// "1983 Apr 13 12:09:14.274 +0000", "%Y %b %d %H:%M:%S%.3f %z");
|
||||
/// assert_eq!(dt, Ok(FixedOffset::east_opt(0).unwrap().from_local_datetime(&NaiveDate::from_ymd_opt(1983, 4, 13).unwrap().and_hms_milli_opt(12, 9, 14, 274).unwrap()).unwrap()));
|
||||
/// let dt = DateTime::parse_from_str("1983 Apr 13 12:09:14.274 +0000", "%Y %b %d %H:%M:%S%.3f %z");
|
||||
/// assert_eq!(
|
||||
/// dt,
|
||||
/// Ok(FixedOffset::east_opt(0)
|
||||
/// .unwrap()
|
||||
/// .from_local_datetime(
|
||||
/// &NaiveDate::from_ymd_opt(1983, 4, 13)
|
||||
/// .unwrap()
|
||||
/// .and_hms_milli_opt(12, 9, 14, 274)
|
||||
/// .unwrap()
|
||||
/// )
|
||||
/// .unwrap())
|
||||
/// );
|
||||
/// ```
|
||||
pub fn parse_from_str(s: &str, fmt: &str) -> ParseResult<DateTime<FixedOffset>> {
|
||||
let mut parsed = Parsed::new();
|
||||
@ -867,10 +938,13 @@ impl DateTime<FixedOffset> {
|
||||
/// ```rust
|
||||
/// # use chrono::{DateTime, FixedOffset, TimeZone};
|
||||
/// let (datetime, remainder) = DateTime::parse_and_remainder(
|
||||
/// "2015-02-18 23:16:09 +0200 trailing text", "%Y-%m-%d %H:%M:%S %z").unwrap();
|
||||
/// "2015-02-18 23:16:09 +0200 trailing text",
|
||||
/// "%Y-%m-%d %H:%M:%S %z",
|
||||
/// )
|
||||
/// .unwrap();
|
||||
/// assert_eq!(
|
||||
/// datetime,
|
||||
/// FixedOffset::east_opt(2*3600).unwrap().with_ymd_and_hms(2015, 2, 18, 23, 16, 9).unwrap()
|
||||
/// FixedOffset::east_opt(2 * 3600).unwrap().with_ymd_and_hms(2015, 2, 18, 23, 16, 9).unwrap()
|
||||
/// );
|
||||
/// assert_eq!(remainder, " trailing text");
|
||||
/// ```
|
||||
@ -1213,8 +1287,14 @@ impl<Tz: TimeZone, Tz2: TimeZone> PartialOrd<DateTime<Tz2>> for DateTime<Tz> {
|
||||
/// ```
|
||||
/// use chrono::prelude::*;
|
||||
///
|
||||
/// let earlier = Utc.with_ymd_and_hms(2015, 5, 15, 2, 0, 0).unwrap().with_timezone(&FixedOffset::west_opt(1 * 3600).unwrap());
|
||||
/// let later = Utc.with_ymd_and_hms(2015, 5, 15, 3, 0, 0).unwrap().with_timezone(&FixedOffset::west_opt(5 * 3600).unwrap());
|
||||
/// let earlier = Utc
|
||||
/// .with_ymd_and_hms(2015, 5, 15, 2, 0, 0)
|
||||
/// .unwrap()
|
||||
/// .with_timezone(&FixedOffset::west_opt(1 * 3600).unwrap());
|
||||
/// let later = Utc
|
||||
/// .with_ymd_and_hms(2015, 5, 15, 3, 0, 0)
|
||||
/// .unwrap()
|
||||
/// .with_timezone(&FixedOffset::west_opt(5 * 3600).unwrap());
|
||||
///
|
||||
/// assert_eq!(earlier.to_string(), "2015-05-15 01:00:00 -01:00");
|
||||
/// assert_eq!(later.to_string(), "2015-05-14 22:00:00 -05:00");
|
||||
|
@ -127,13 +127,16 @@ impl<'de> de::Deserialize<'de> for DateTime<Local> {
|
||||
/// #[derive(Deserialize, Serialize)]
|
||||
/// struct S {
|
||||
/// #[serde(with = "ts_nanoseconds")]
|
||||
/// time: DateTime<Utc>
|
||||
/// time: DateTime<Utc>,
|
||||
/// }
|
||||
///
|
||||
/// let time = NaiveDate::from_ymd_opt(2018, 5, 17).unwrap().and_hms_nano_opt(02, 04, 59, 918355733).unwrap().and_local_timezone(Utc).unwrap();
|
||||
/// let my_s = S {
|
||||
/// time: time.clone(),
|
||||
/// };
|
||||
/// let time = NaiveDate::from_ymd_opt(2018, 5, 17)
|
||||
/// .unwrap()
|
||||
/// .and_hms_nano_opt(02, 04, 59, 918355733)
|
||||
/// .unwrap()
|
||||
/// .and_local_timezone(Utc)
|
||||
/// .unwrap();
|
||||
/// let my_s = S { time: time.clone() };
|
||||
///
|
||||
/// let as_string = serde_json::to_string(&my_s)?;
|
||||
/// assert_eq!(as_string, r#"{"time":1526522699918355733}"#);
|
||||
@ -171,11 +174,16 @@ pub mod ts_nanoseconds {
|
||||
/// #[derive(Serialize)]
|
||||
/// struct S {
|
||||
/// #[serde(serialize_with = "to_nano_ts")]
|
||||
/// time: DateTime<Utc>
|
||||
/// time: DateTime<Utc>,
|
||||
/// }
|
||||
///
|
||||
/// let my_s = S {
|
||||
/// time: NaiveDate::from_ymd_opt(2018, 5, 17).unwrap().and_hms_nano_opt(02, 04, 59, 918355733).unwrap().and_local_timezone(Utc).unwrap(),
|
||||
/// time: NaiveDate::from_ymd_opt(2018, 5, 17)
|
||||
/// .unwrap()
|
||||
/// .and_hms_nano_opt(02, 04, 59, 918355733)
|
||||
/// .unwrap()
|
||||
/// .and_local_timezone(Utc)
|
||||
/// .unwrap(),
|
||||
/// };
|
||||
/// let as_string = serde_json::to_string(&my_s)?;
|
||||
/// assert_eq!(as_string, r#"{"time":1526522699918355733}"#);
|
||||
@ -203,7 +211,7 @@ pub mod ts_nanoseconds {
|
||||
/// #[derive(Debug, PartialEq, Deserialize)]
|
||||
/// struct S {
|
||||
/// #[serde(deserialize_with = "from_nano_ts")]
|
||||
/// time: DateTime<Utc>
|
||||
/// time: DateTime<Utc>,
|
||||
/// }
|
||||
///
|
||||
/// let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918355733 }"#)?;
|
||||
@ -267,13 +275,18 @@ pub mod ts_nanoseconds {
|
||||
/// #[derive(Deserialize, Serialize)]
|
||||
/// struct S {
|
||||
/// #[serde(with = "ts_nanoseconds_option")]
|
||||
/// time: Option<DateTime<Utc>>
|
||||
/// time: Option<DateTime<Utc>>,
|
||||
/// }
|
||||
///
|
||||
/// let time = Some(NaiveDate::from_ymd_opt(2018, 5, 17).unwrap().and_hms_nano_opt(02, 04, 59, 918355733).unwrap().and_local_timezone(Utc).unwrap());
|
||||
/// let my_s = S {
|
||||
/// time: time.clone(),
|
||||
/// };
|
||||
/// let time = Some(
|
||||
/// NaiveDate::from_ymd_opt(2018, 5, 17)
|
||||
/// .unwrap()
|
||||
/// .and_hms_nano_opt(02, 04, 59, 918355733)
|
||||
/// .unwrap()
|
||||
/// .and_local_timezone(Utc)
|
||||
/// .unwrap(),
|
||||
/// );
|
||||
/// let my_s = S { time: time.clone() };
|
||||
///
|
||||
/// let as_string = serde_json::to_string(&my_s)?;
|
||||
/// assert_eq!(as_string, r#"{"time":1526522699918355733}"#);
|
||||
@ -310,11 +323,18 @@ pub mod ts_nanoseconds_option {
|
||||
/// #[derive(Serialize)]
|
||||
/// struct S {
|
||||
/// #[serde(serialize_with = "to_nano_tsopt")]
|
||||
/// time: Option<DateTime<Utc>>
|
||||
/// time: Option<DateTime<Utc>>,
|
||||
/// }
|
||||
///
|
||||
/// let my_s = S {
|
||||
/// time: Some(NaiveDate::from_ymd_opt(2018, 5, 17).unwrap().and_hms_nano_opt(02, 04, 59, 918355733).unwrap().and_local_timezone(Utc).unwrap()),
|
||||
/// time: Some(
|
||||
/// NaiveDate::from_ymd_opt(2018, 5, 17)
|
||||
/// .unwrap()
|
||||
/// .and_hms_nano_opt(02, 04, 59, 918355733)
|
||||
/// .unwrap()
|
||||
/// .and_local_timezone(Utc)
|
||||
/// .unwrap(),
|
||||
/// ),
|
||||
/// };
|
||||
/// let as_string = serde_json::to_string(&my_s)?;
|
||||
/// assert_eq!(as_string, r#"{"time":1526522699918355733}"#);
|
||||
@ -345,7 +365,7 @@ pub mod ts_nanoseconds_option {
|
||||
/// #[derive(Debug, PartialEq, Deserialize)]
|
||||
/// struct S {
|
||||
/// #[serde(deserialize_with = "from_nano_tsopt")]
|
||||
/// time: Option<DateTime<Utc>>
|
||||
/// time: Option<DateTime<Utc>>,
|
||||
/// }
|
||||
///
|
||||
/// let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918355733 }"#)?;
|
||||
@ -407,13 +427,16 @@ pub mod ts_nanoseconds_option {
|
||||
/// #[derive(Deserialize, Serialize)]
|
||||
/// struct S {
|
||||
/// #[serde(with = "ts_microseconds")]
|
||||
/// time: DateTime<Utc>
|
||||
/// time: DateTime<Utc>,
|
||||
/// }
|
||||
///
|
||||
/// let time = NaiveDate::from_ymd_opt(2018, 5, 17).unwrap().and_hms_micro_opt(02, 04, 59, 918355).unwrap().and_local_timezone(Utc).unwrap();
|
||||
/// let my_s = S {
|
||||
/// time: time.clone(),
|
||||
/// };
|
||||
/// let time = NaiveDate::from_ymd_opt(2018, 5, 17)
|
||||
/// .unwrap()
|
||||
/// .and_hms_micro_opt(02, 04, 59, 918355)
|
||||
/// .unwrap()
|
||||
/// .and_local_timezone(Utc)
|
||||
/// .unwrap();
|
||||
/// let my_s = S { time: time.clone() };
|
||||
///
|
||||
/// let as_string = serde_json::to_string(&my_s)?;
|
||||
/// assert_eq!(as_string, r#"{"time":1526522699918355}"#);
|
||||
@ -442,11 +465,16 @@ pub mod ts_microseconds {
|
||||
/// #[derive(Serialize)]
|
||||
/// struct S {
|
||||
/// #[serde(serialize_with = "to_micro_ts")]
|
||||
/// time: DateTime<Utc>
|
||||
/// time: DateTime<Utc>,
|
||||
/// }
|
||||
///
|
||||
/// let my_s = S {
|
||||
/// time: NaiveDate::from_ymd_opt(2018, 5, 17).unwrap().and_hms_micro_opt(02, 04, 59, 918355).unwrap().and_local_timezone(Utc).unwrap(),
|
||||
/// time: NaiveDate::from_ymd_opt(2018, 5, 17)
|
||||
/// .unwrap()
|
||||
/// .and_hms_micro_opt(02, 04, 59, 918355)
|
||||
/// .unwrap()
|
||||
/// .and_local_timezone(Utc)
|
||||
/// .unwrap(),
|
||||
/// };
|
||||
/// let as_string = serde_json::to_string(&my_s)?;
|
||||
/// assert_eq!(as_string, r#"{"time":1526522699918355}"#);
|
||||
@ -472,7 +500,7 @@ pub mod ts_microseconds {
|
||||
/// #[derive(Debug, PartialEq, Deserialize)]
|
||||
/// struct S {
|
||||
/// #[serde(deserialize_with = "from_micro_ts")]
|
||||
/// time: DateTime<Utc>
|
||||
/// time: DateTime<Utc>,
|
||||
/// }
|
||||
///
|
||||
/// let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918355 }"#)?;
|
||||
@ -536,13 +564,18 @@ pub mod ts_microseconds {
|
||||
/// #[derive(Deserialize, Serialize)]
|
||||
/// struct S {
|
||||
/// #[serde(with = "ts_microseconds_option")]
|
||||
/// time: Option<DateTime<Utc>>
|
||||
/// time: Option<DateTime<Utc>>,
|
||||
/// }
|
||||
///
|
||||
/// let time = Some(NaiveDate::from_ymd_opt(2018, 5, 17).unwrap().and_hms_micro_opt(02, 04, 59, 918355).unwrap().and_local_timezone(Utc).unwrap());
|
||||
/// let my_s = S {
|
||||
/// time: time.clone(),
|
||||
/// };
|
||||
/// let time = Some(
|
||||
/// NaiveDate::from_ymd_opt(2018, 5, 17)
|
||||
/// .unwrap()
|
||||
/// .and_hms_micro_opt(02, 04, 59, 918355)
|
||||
/// .unwrap()
|
||||
/// .and_local_timezone(Utc)
|
||||
/// .unwrap(),
|
||||
/// );
|
||||
/// let my_s = S { time: time.clone() };
|
||||
///
|
||||
/// let as_string = serde_json::to_string(&my_s)?;
|
||||
/// assert_eq!(as_string, r#"{"time":1526522699918355}"#);
|
||||
@ -570,11 +603,18 @@ pub mod ts_microseconds_option {
|
||||
/// #[derive(Serialize)]
|
||||
/// struct S {
|
||||
/// #[serde(serialize_with = "to_micro_tsopt")]
|
||||
/// time: Option<DateTime<Utc>>
|
||||
/// time: Option<DateTime<Utc>>,
|
||||
/// }
|
||||
///
|
||||
/// let my_s = S {
|
||||
/// time: Some(NaiveDate::from_ymd_opt(2018, 5, 17).unwrap().and_hms_micro_opt(02, 04, 59, 918355).unwrap().and_local_timezone(Utc).unwrap()),
|
||||
/// time: Some(
|
||||
/// NaiveDate::from_ymd_opt(2018, 5, 17)
|
||||
/// .unwrap()
|
||||
/// .and_hms_micro_opt(02, 04, 59, 918355)
|
||||
/// .unwrap()
|
||||
/// .and_local_timezone(Utc)
|
||||
/// .unwrap(),
|
||||
/// ),
|
||||
/// };
|
||||
/// let as_string = serde_json::to_string(&my_s)?;
|
||||
/// assert_eq!(as_string, r#"{"time":1526522699918355}"#);
|
||||
@ -603,7 +643,7 @@ pub mod ts_microseconds_option {
|
||||
/// #[derive(Debug, PartialEq, Deserialize)]
|
||||
/// struct S {
|
||||
/// #[serde(deserialize_with = "from_micro_tsopt")]
|
||||
/// time: Option<DateTime<Utc>>
|
||||
/// time: Option<DateTime<Utc>>,
|
||||
/// }
|
||||
///
|
||||
/// let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918355 }"#)?;
|
||||
@ -665,13 +705,16 @@ pub mod ts_microseconds_option {
|
||||
/// #[derive(Deserialize, Serialize)]
|
||||
/// struct S {
|
||||
/// #[serde(with = "ts_milliseconds")]
|
||||
/// time: DateTime<Utc>
|
||||
/// time: DateTime<Utc>,
|
||||
/// }
|
||||
///
|
||||
/// let time = NaiveDate::from_ymd_opt(2018, 5, 17).unwrap().and_hms_milli_opt(02, 04, 59, 918).unwrap().and_local_timezone(Utc).unwrap();
|
||||
/// let my_s = S {
|
||||
/// time: time.clone(),
|
||||
/// };
|
||||
/// let time = NaiveDate::from_ymd_opt(2018, 5, 17)
|
||||
/// .unwrap()
|
||||
/// .and_hms_milli_opt(02, 04, 59, 918)
|
||||
/// .unwrap()
|
||||
/// .and_local_timezone(Utc)
|
||||
/// .unwrap();
|
||||
/// let my_s = S { time: time.clone() };
|
||||
///
|
||||
/// let as_string = serde_json::to_string(&my_s)?;
|
||||
/// assert_eq!(as_string, r#"{"time":1526522699918}"#);
|
||||
@ -700,11 +743,16 @@ pub mod ts_milliseconds {
|
||||
/// #[derive(Serialize)]
|
||||
/// struct S {
|
||||
/// #[serde(serialize_with = "to_milli_ts")]
|
||||
/// time: DateTime<Utc>
|
||||
/// time: DateTime<Utc>,
|
||||
/// }
|
||||
///
|
||||
/// let my_s = S {
|
||||
/// time: NaiveDate::from_ymd_opt(2018, 5, 17).unwrap().and_hms_milli_opt(02, 04, 59, 918).unwrap().and_local_timezone(Utc).unwrap(),
|
||||
/// time: NaiveDate::from_ymd_opt(2018, 5, 17)
|
||||
/// .unwrap()
|
||||
/// .and_hms_milli_opt(02, 04, 59, 918)
|
||||
/// .unwrap()
|
||||
/// .and_local_timezone(Utc)
|
||||
/// .unwrap(),
|
||||
/// };
|
||||
/// let as_string = serde_json::to_string(&my_s)?;
|
||||
/// assert_eq!(as_string, r#"{"time":1526522699918}"#);
|
||||
@ -730,7 +778,7 @@ pub mod ts_milliseconds {
|
||||
/// #[derive(Debug, PartialEq, Deserialize)]
|
||||
/// struct S {
|
||||
/// #[serde(deserialize_with = "from_milli_ts")]
|
||||
/// time: DateTime<Utc>
|
||||
/// time: DateTime<Utc>,
|
||||
/// }
|
||||
///
|
||||
/// let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918 }"#)?;
|
||||
@ -788,13 +836,18 @@ pub mod ts_milliseconds {
|
||||
/// #[derive(Deserialize, Serialize)]
|
||||
/// struct S {
|
||||
/// #[serde(with = "ts_milliseconds_option")]
|
||||
/// time: Option<DateTime<Utc>>
|
||||
/// time: Option<DateTime<Utc>>,
|
||||
/// }
|
||||
///
|
||||
/// let time = Some(NaiveDate::from_ymd_opt(2018, 5, 17).unwrap().and_hms_milli_opt(02, 04, 59, 918).unwrap().and_local_timezone(Utc).unwrap());
|
||||
/// let my_s = S {
|
||||
/// time: time.clone(),
|
||||
/// };
|
||||
/// let time = Some(
|
||||
/// NaiveDate::from_ymd_opt(2018, 5, 17)
|
||||
/// .unwrap()
|
||||
/// .and_hms_milli_opt(02, 04, 59, 918)
|
||||
/// .unwrap()
|
||||
/// .and_local_timezone(Utc)
|
||||
/// .unwrap(),
|
||||
/// );
|
||||
/// let my_s = S { time: time.clone() };
|
||||
///
|
||||
/// let as_string = serde_json::to_string(&my_s)?;
|
||||
/// assert_eq!(as_string, r#"{"time":1526522699918}"#);
|
||||
@ -822,11 +875,18 @@ pub mod ts_milliseconds_option {
|
||||
/// #[derive(Serialize)]
|
||||
/// struct S {
|
||||
/// #[serde(serialize_with = "to_milli_tsopt")]
|
||||
/// time: Option<DateTime<Utc>>
|
||||
/// time: Option<DateTime<Utc>>,
|
||||
/// }
|
||||
///
|
||||
/// let my_s = S {
|
||||
/// time: Some(NaiveDate::from_ymd_opt(2018, 5, 17).unwrap().and_hms_milli_opt(02, 04, 59, 918).unwrap().and_local_timezone(Utc).unwrap()),
|
||||
/// time: Some(
|
||||
/// NaiveDate::from_ymd_opt(2018, 5, 17)
|
||||
/// .unwrap()
|
||||
/// .and_hms_milli_opt(02, 04, 59, 918)
|
||||
/// .unwrap()
|
||||
/// .and_local_timezone(Utc)
|
||||
/// .unwrap(),
|
||||
/// ),
|
||||
/// };
|
||||
/// let as_string = serde_json::to_string(&my_s)?;
|
||||
/// assert_eq!(as_string, r#"{"time":1526522699918}"#);
|
||||
@ -862,7 +922,7 @@ pub mod ts_milliseconds_option {
|
||||
/// #[derive(Deserialize, PartialEq, Debug)]
|
||||
/// struct S {
|
||||
/// #[serde(default, deserialize_with = "from_milli_tsopt")]
|
||||
/// time: Option<DateTime<Utc>>
|
||||
/// time: Option<DateTime<Utc>>,
|
||||
/// }
|
||||
///
|
||||
/// let my_s: E<S> = serde_json::from_str(r#"{ "time": 1526522699918 }"#)?;
|
||||
@ -929,13 +989,11 @@ pub mod ts_milliseconds_option {
|
||||
/// #[derive(Deserialize, Serialize)]
|
||||
/// struct S {
|
||||
/// #[serde(with = "ts_seconds")]
|
||||
/// time: DateTime<Utc>
|
||||
/// time: DateTime<Utc>,
|
||||
/// }
|
||||
///
|
||||
/// let time = Utc.with_ymd_and_hms(2015, 5, 15, 10, 0, 0).unwrap();
|
||||
/// let my_s = S {
|
||||
/// time: time.clone(),
|
||||
/// };
|
||||
/// let my_s = S { time: time.clone() };
|
||||
///
|
||||
/// let as_string = serde_json::to_string(&my_s)?;
|
||||
/// assert_eq!(as_string, r#"{"time":1431684000}"#);
|
||||
@ -963,12 +1021,10 @@ pub mod ts_seconds {
|
||||
/// #[derive(Serialize)]
|
||||
/// struct S {
|
||||
/// #[serde(serialize_with = "to_ts")]
|
||||
/// time: DateTime<Utc>
|
||||
/// time: DateTime<Utc>,
|
||||
/// }
|
||||
///
|
||||
/// let my_s = S {
|
||||
/// time: Utc.with_ymd_and_hms(2015, 5, 15, 10, 0, 0).unwrap(),
|
||||
/// };
|
||||
/// let my_s = S { time: Utc.with_ymd_and_hms(2015, 5, 15, 10, 0, 0).unwrap() };
|
||||
/// let as_string = serde_json::to_string(&my_s)?;
|
||||
/// assert_eq!(as_string, r#"{"time":1431684000}"#);
|
||||
/// # Ok::<(), serde_json::Error>(())
|
||||
@ -993,7 +1049,7 @@ pub mod ts_seconds {
|
||||
/// #[derive(Debug, PartialEq, Deserialize)]
|
||||
/// struct S {
|
||||
/// #[serde(deserialize_with = "from_ts")]
|
||||
/// time: DateTime<Utc>
|
||||
/// time: DateTime<Utc>,
|
||||
/// }
|
||||
///
|
||||
/// let my_s: S = serde_json::from_str(r#"{ "time": 1431684000 }"#)?;
|
||||
@ -1052,13 +1108,11 @@ pub mod ts_seconds {
|
||||
/// #[derive(Deserialize, Serialize)]
|
||||
/// struct S {
|
||||
/// #[serde(with = "ts_seconds_option")]
|
||||
/// time: Option<DateTime<Utc>>
|
||||
/// time: Option<DateTime<Utc>>,
|
||||
/// }
|
||||
///
|
||||
/// let time = Some(Utc.with_ymd_and_hms(2015, 5, 15, 10, 0, 0).unwrap());
|
||||
/// let my_s = S {
|
||||
/// time: time.clone(),
|
||||
/// };
|
||||
/// let my_s = S { time: time.clone() };
|
||||
///
|
||||
/// let as_string = serde_json::to_string(&my_s)?;
|
||||
/// assert_eq!(as_string, r#"{"time":1431684000}"#);
|
||||
@ -1086,12 +1140,10 @@ pub mod ts_seconds_option {
|
||||
/// #[derive(Serialize)]
|
||||
/// struct S {
|
||||
/// #[serde(serialize_with = "to_tsopt")]
|
||||
/// time: Option<DateTime<Utc>>
|
||||
/// time: Option<DateTime<Utc>>,
|
||||
/// }
|
||||
///
|
||||
/// let my_s = S {
|
||||
/// time: Some(Utc.with_ymd_and_hms(2015, 5, 15, 10, 0, 0).unwrap()),
|
||||
/// };
|
||||
/// let my_s = S { time: Some(Utc.with_ymd_and_hms(2015, 5, 15, 10, 0, 0).unwrap()) };
|
||||
/// let as_string = serde_json::to_string(&my_s)?;
|
||||
/// assert_eq!(as_string, r#"{"time":1431684000}"#);
|
||||
/// # Ok::<(), serde_json::Error>(())
|
||||
@ -1119,7 +1171,7 @@ pub mod ts_seconds_option {
|
||||
/// #[derive(Debug, PartialEq, Deserialize)]
|
||||
/// struct S {
|
||||
/// #[serde(deserialize_with = "from_tsopt")]
|
||||
/// time: Option<DateTime<Utc>>
|
||||
/// time: Option<DateTime<Utc>>,
|
||||
/// }
|
||||
///
|
||||
/// let my_s: S = serde_json::from_str(r#"{ "time": 1431684000 }"#)?;
|
||||
|
120
src/lib.rs
120
src/lib.rs
@ -107,7 +107,7 @@
|
||||
#![cfg_attr(feature = "now", doc = "```rust")]
|
||||
//! use chrono::prelude::*;
|
||||
//!
|
||||
//! let utc: DateTime<Utc> = Utc::now(); // e.g. `2014-11-28T12:45:59.324310806Z`
|
||||
//! let utc: DateTime<Utc> = Utc::now(); // e.g. `2014-11-28T12:45:59.324310806Z`
|
||||
//! # let _ = utc;
|
||||
//! ```
|
||||
//!
|
||||
@ -125,34 +125,74 @@
|
||||
//!
|
||||
#![cfg_attr(not(feature = "now"), doc = "```ignore")]
|
||||
#![cfg_attr(feature = "now", doc = "```rust")]
|
||||
//! use chrono::prelude::*;
|
||||
//! use chrono::offset::LocalResult;
|
||||
//! use chrono::prelude::*;
|
||||
//!
|
||||
//! # fn doctest() -> Option<()> {
|
||||
//!
|
||||
//! let dt = Utc.with_ymd_and_hms(2014, 7, 8, 9, 10, 11).unwrap(); // `2014-07-08T09:10:11Z`
|
||||
//! assert_eq!(dt, NaiveDate::from_ymd_opt(2014, 7, 8)?.and_hms_opt(9, 10, 11)?.and_local_timezone(Utc).unwrap());
|
||||
//! assert_eq!(
|
||||
//! dt,
|
||||
//! NaiveDate::from_ymd_opt(2014, 7, 8)?
|
||||
//! .and_hms_opt(9, 10, 11)?
|
||||
//! .and_local_timezone(Utc)
|
||||
//! .unwrap()
|
||||
//! );
|
||||
//!
|
||||
//! // July 8 is 188th day of the year 2014 (`o` for "ordinal")
|
||||
//! assert_eq!(dt, NaiveDate::from_yo_opt(2014, 189)?.and_hms_opt(9, 10, 11)?.and_utc());
|
||||
//! // July 8 is Tuesday in ISO week 28 of the year 2014.
|
||||
//! assert_eq!(dt, NaiveDate::from_isoywd_opt(2014, 28, Weekday::Tue)?.and_hms_opt(9, 10, 11)?.and_utc());
|
||||
//! assert_eq!(
|
||||
//! dt,
|
||||
//! NaiveDate::from_isoywd_opt(2014, 28, Weekday::Tue)?.and_hms_opt(9, 10, 11)?.and_utc()
|
||||
//! );
|
||||
//!
|
||||
//! let dt = NaiveDate::from_ymd_opt(2014, 7, 8)?.and_hms_milli_opt(9, 10, 11, 12)?.and_local_timezone(Utc).unwrap(); // `2014-07-08T09:10:11.012Z`
|
||||
//! assert_eq!(dt, NaiveDate::from_ymd_opt(2014, 7, 8)?.and_hms_micro_opt(9, 10, 11, 12_000)?.and_local_timezone(Utc).unwrap());
|
||||
//! assert_eq!(dt, NaiveDate::from_ymd_opt(2014, 7, 8)?.and_hms_nano_opt(9, 10, 11, 12_000_000)?.and_local_timezone(Utc).unwrap());
|
||||
//! let dt = NaiveDate::from_ymd_opt(2014, 7, 8)?
|
||||
//! .and_hms_milli_opt(9, 10, 11, 12)?
|
||||
//! .and_local_timezone(Utc)
|
||||
//! .unwrap(); // `2014-07-08T09:10:11.012Z`
|
||||
//! assert_eq!(
|
||||
//! dt,
|
||||
//! NaiveDate::from_ymd_opt(2014, 7, 8)?
|
||||
//! .and_hms_micro_opt(9, 10, 11, 12_000)?
|
||||
//! .and_local_timezone(Utc)
|
||||
//! .unwrap()
|
||||
//! );
|
||||
//! assert_eq!(
|
||||
//! dt,
|
||||
//! NaiveDate::from_ymd_opt(2014, 7, 8)?
|
||||
//! .and_hms_nano_opt(9, 10, 11, 12_000_000)?
|
||||
//! .and_local_timezone(Utc)
|
||||
//! .unwrap()
|
||||
//! );
|
||||
//!
|
||||
//! // dynamic verification
|
||||
//! assert_eq!(Utc.with_ymd_and_hms(2014, 7, 8, 21, 15, 33),
|
||||
//! LocalResult::Single(NaiveDate::from_ymd_opt(2014, 7, 8)?.and_hms_opt(21, 15, 33)?.and_utc()));
|
||||
//! assert_eq!(
|
||||
//! Utc.with_ymd_and_hms(2014, 7, 8, 21, 15, 33),
|
||||
//! LocalResult::Single(
|
||||
//! NaiveDate::from_ymd_opt(2014, 7, 8)?.and_hms_opt(21, 15, 33)?.and_utc()
|
||||
//! )
|
||||
//! );
|
||||
//! assert_eq!(Utc.with_ymd_and_hms(2014, 7, 8, 80, 15, 33), LocalResult::None);
|
||||
//! assert_eq!(Utc.with_ymd_and_hms(2014, 7, 38, 21, 15, 33), LocalResult::None);
|
||||
//!
|
||||
//! # #[cfg(feature = "clock")] {
|
||||
//! // other time zone objects can be used to construct a local datetime.
|
||||
//! // obviously, `local_dt` is normally different from `dt`, but `fixed_dt` should be identical.
|
||||
//! let local_dt = Local.from_local_datetime(&NaiveDate::from_ymd_opt(2014, 7, 8).unwrap().and_hms_milli_opt(9, 10, 11, 12).unwrap()).unwrap();
|
||||
//! let fixed_dt = FixedOffset::east_opt(9 * 3600).unwrap().from_local_datetime(&NaiveDate::from_ymd_opt(2014, 7, 8).unwrap().and_hms_milli_opt(18, 10, 11, 12).unwrap()).unwrap();
|
||||
//! let local_dt = Local
|
||||
//! .from_local_datetime(
|
||||
//! &NaiveDate::from_ymd_opt(2014, 7, 8).unwrap().and_hms_milli_opt(9, 10, 11, 12).unwrap(),
|
||||
//! )
|
||||
//! .unwrap();
|
||||
//! let fixed_dt = FixedOffset::east_opt(9 * 3600)
|
||||
//! .unwrap()
|
||||
//! .from_local_datetime(
|
||||
//! &NaiveDate::from_ymd_opt(2014, 7, 8)
|
||||
//! .unwrap()
|
||||
//! .and_hms_milli_opt(18, 10, 11, 12)
|
||||
//! .unwrap(),
|
||||
//! )
|
||||
//! .unwrap();
|
||||
//! assert_eq!(dt, fixed_dt);
|
||||
//! # let _ = local_dt;
|
||||
//! # }
|
||||
@ -172,7 +212,15 @@
|
||||
//! use chrono::TimeDelta;
|
||||
//!
|
||||
//! // assume this returned `2014-11-28T21:45:59.324310806+09:00`:
|
||||
//! let dt = FixedOffset::east_opt(9*3600).unwrap().from_local_datetime(&NaiveDate::from_ymd_opt(2014, 11, 28).unwrap().and_hms_nano_opt(21, 45, 59, 324310806).unwrap()).unwrap();
|
||||
//! let dt = FixedOffset::east_opt(9 * 3600)
|
||||
//! .unwrap()
|
||||
//! .from_local_datetime(
|
||||
//! &NaiveDate::from_ymd_opt(2014, 11, 28)
|
||||
//! .unwrap()
|
||||
//! .and_hms_nano_opt(21, 45, 59, 324310806)
|
||||
//! .unwrap(),
|
||||
//! )
|
||||
//! .unwrap();
|
||||
//!
|
||||
//! // property accessors
|
||||
//! assert_eq!((dt.year(), dt.month(), dt.day()), (2014, 11, 28));
|
||||
@ -186,7 +234,15 @@
|
||||
//! // time zone accessor and manipulation
|
||||
//! assert_eq!(dt.offset().fix().local_minus_utc(), 9 * 3600);
|
||||
//! assert_eq!(dt.timezone(), FixedOffset::east_opt(9 * 3600).unwrap());
|
||||
//! assert_eq!(dt.with_timezone(&Utc), NaiveDate::from_ymd_opt(2014, 11, 28).unwrap().and_hms_nano_opt(12, 45, 59, 324310806).unwrap().and_local_timezone(Utc).unwrap());
|
||||
//! assert_eq!(
|
||||
//! dt.with_timezone(&Utc),
|
||||
//! NaiveDate::from_ymd_opt(2014, 11, 28)
|
||||
//! .unwrap()
|
||||
//! .and_hms_nano_opt(12, 45, 59, 324310806)
|
||||
//! .unwrap()
|
||||
//! .and_local_timezone(Utc)
|
||||
//! .unwrap()
|
||||
//! );
|
||||
//!
|
||||
//! // a sample of property manipulations (validates dynamically)
|
||||
//! assert_eq!(dt.with_day(29).unwrap().weekday(), Weekday::Sat); // 2014-11-29 is Saturday
|
||||
@ -198,10 +254,14 @@
|
||||
//! let dt2 = Utc.with_ymd_and_hms(2014, 11, 14, 10, 9, 8).unwrap();
|
||||
//! assert_eq!(dt1.signed_duration_since(dt2), TimeDelta::seconds(-2 * 3600 + 2));
|
||||
//! assert_eq!(dt2.signed_duration_since(dt1), TimeDelta::seconds(2 * 3600 - 2));
|
||||
//! assert_eq!(Utc.with_ymd_and_hms(1970, 1, 1, 0, 0, 0).unwrap() + TimeDelta::seconds(1_000_000_000),
|
||||
//! Utc.with_ymd_and_hms(2001, 9, 9, 1, 46, 40).unwrap());
|
||||
//! assert_eq!(Utc.with_ymd_and_hms(1970, 1, 1, 0, 0, 0).unwrap() - TimeDelta::seconds(1_000_000_000),
|
||||
//! Utc.with_ymd_and_hms(1938, 4, 24, 22, 13, 20).unwrap());
|
||||
//! assert_eq!(
|
||||
//! Utc.with_ymd_and_hms(1970, 1, 1, 0, 0, 0).unwrap() + TimeDelta::seconds(1_000_000_000),
|
||||
//! Utc.with_ymd_and_hms(2001, 9, 9, 1, 46, 40).unwrap()
|
||||
//! );
|
||||
//! assert_eq!(
|
||||
//! Utc.with_ymd_and_hms(1970, 1, 1, 0, 0, 0).unwrap() - TimeDelta::seconds(1_000_000_000),
|
||||
//! Utc.with_ymd_and_hms(1938, 4, 24, 22, 13, 20).unwrap()
|
||||
//! );
|
||||
//! ```
|
||||
//!
|
||||
//! ### Formatting and Parsing
|
||||
@ -236,7 +296,10 @@
|
||||
//! let dt = Utc.with_ymd_and_hms(2014, 11, 28, 12, 0, 9).unwrap();
|
||||
//! 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_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.to_string(), "2014-11-28 12:00:09 UTC");
|
||||
@ -245,7 +308,12 @@
|
||||
//! assert_eq!(format!("{:?}", dt), "2014-11-28T12:00:09Z");
|
||||
//!
|
||||
//! // Note that milli/nanoseconds are only printed if they are non-zero
|
||||
//! let dt_nano = NaiveDate::from_ymd_opt(2014, 11, 28).unwrap().and_hms_nano_opt(12, 0, 9, 1).unwrap().and_local_timezone(Utc).unwrap();
|
||||
//! let dt_nano = NaiveDate::from_ymd_opt(2014, 11, 28)
|
||||
//! .unwrap()
|
||||
//! .and_hms_nano_opt(12, 0, 9, 1)
|
||||
//! .unwrap()
|
||||
//! .and_local_timezone(Utc)
|
||||
//! .unwrap();
|
||||
//! assert_eq!(format!("{:?}", dt_nano), "2014-11-28T12:00:09.000000001Z");
|
||||
//! # }
|
||||
//! # #[cfg(not(all(feature = "unstable-locales", feature = "alloc")))]
|
||||
@ -280,7 +348,7 @@
|
||||
//! use chrono::prelude::*;
|
||||
//!
|
||||
//! let dt = Utc.with_ymd_and_hms(2014, 11, 28, 12, 0, 9).unwrap();
|
||||
//! let fixed_dt = dt.with_timezone(&FixedOffset::east_opt(9*3600).unwrap());
|
||||
//! let fixed_dt = dt.with_timezone(&FixedOffset::east_opt(9 * 3600).unwrap());
|
||||
//!
|
||||
//! // method 1
|
||||
//! assert_eq!("2014-11-28T12:00:09Z".parse::<DateTime<Utc>>(), Ok(dt.clone()));
|
||||
@ -288,10 +356,14 @@
|
||||
//! assert_eq!("2014-11-28T21:00:09+09:00".parse::<DateTime<FixedOffset>>(), Ok(fixed_dt.clone()));
|
||||
//!
|
||||
//! // method 2
|
||||
//! assert_eq!(DateTime::parse_from_str("2014-11-28 21:00:09 +09:00", "%Y-%m-%d %H:%M:%S %z"),
|
||||
//! Ok(fixed_dt.clone()));
|
||||
//! assert_eq!(DateTime::parse_from_rfc2822("Fri, 28 Nov 2014 21:00:09 +0900"),
|
||||
//! Ok(fixed_dt.clone()));
|
||||
//! assert_eq!(
|
||||
//! DateTime::parse_from_str("2014-11-28 21:00:09 +09:00", "%Y-%m-%d %H:%M:%S %z"),
|
||||
//! Ok(fixed_dt.clone())
|
||||
//! );
|
||||
//! assert_eq!(
|
||||
//! DateTime::parse_from_rfc2822("Fri, 28 Nov 2014 21:00:09 +0900"),
|
||||
//! Ok(fixed_dt.clone())
|
||||
//! );
|
||||
//! assert_eq!(DateTime::parse_from_rfc3339("2014-11-28T21:00:09+09:00"), Ok(fixed_dt.clone()));
|
||||
//!
|
||||
//! // oops, the year is missing!
|
||||
|
@ -371,11 +371,11 @@ impl NaiveDate {
|
||||
/// let from_ndays_opt = NaiveDate::from_num_days_from_ce_opt;
|
||||
/// let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap();
|
||||
///
|
||||
/// assert_eq!(from_ndays_opt(730_000), Some(from_ymd(1999, 9, 3)));
|
||||
/// assert_eq!(from_ndays_opt(1), Some(from_ymd(1, 1, 1)));
|
||||
/// assert_eq!(from_ndays_opt(0), Some(from_ymd(0, 12, 31)));
|
||||
/// assert_eq!(from_ndays_opt(-1), Some(from_ymd(0, 12, 30)));
|
||||
/// assert_eq!(from_ndays_opt(100_000_000), None);
|
||||
/// assert_eq!(from_ndays_opt(730_000), Some(from_ymd(1999, 9, 3)));
|
||||
/// assert_eq!(from_ndays_opt(1), Some(from_ymd(1, 1, 1)));
|
||||
/// assert_eq!(from_ndays_opt(0), Some(from_ymd(0, 12, 31)));
|
||||
/// assert_eq!(from_ndays_opt(-1), Some(from_ymd(0, 12, 30)));
|
||||
/// assert_eq!(from_ndays_opt(100_000_000), None);
|
||||
/// assert_eq!(from_ndays_opt(-100_000_000), None);
|
||||
/// ```
|
||||
#[must_use]
|
||||
@ -426,8 +426,10 @@ impl NaiveDate {
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{NaiveDate, Weekday};
|
||||
/// assert_eq!(NaiveDate::from_weekday_of_month_opt(2017, 3, Weekday::Fri, 2),
|
||||
/// NaiveDate::from_ymd_opt(2017, 3, 10))
|
||||
/// assert_eq!(
|
||||
/// NaiveDate::from_weekday_of_month_opt(2017, 3, Weekday::Fri, 2),
|
||||
/// NaiveDate::from_ymd_opt(2017, 3, 10)
|
||||
/// )
|
||||
/// ```
|
||||
#[must_use]
|
||||
pub const fn from_weekday_of_month_opt(
|
||||
@ -456,10 +458,14 @@ impl NaiveDate {
|
||||
///
|
||||
/// let parse_from_str = NaiveDate::parse_from_str;
|
||||
///
|
||||
/// assert_eq!(parse_from_str("2015-09-05", "%Y-%m-%d"),
|
||||
/// Ok(NaiveDate::from_ymd_opt(2015, 9, 5).unwrap()));
|
||||
/// assert_eq!(parse_from_str("5sep2015", "%d%b%Y"),
|
||||
/// Ok(NaiveDate::from_ymd_opt(2015, 9, 5).unwrap()));
|
||||
/// assert_eq!(
|
||||
/// parse_from_str("2015-09-05", "%Y-%m-%d"),
|
||||
/// Ok(NaiveDate::from_ymd_opt(2015, 9, 5).unwrap())
|
||||
/// );
|
||||
/// assert_eq!(
|
||||
/// parse_from_str("5sep2015", "%d%b%Y"),
|
||||
/// Ok(NaiveDate::from_ymd_opt(2015, 9, 5).unwrap())
|
||||
/// );
|
||||
/// ```
|
||||
///
|
||||
/// Time and offset is ignored for the purpose of parsing.
|
||||
@ -467,8 +473,10 @@ impl NaiveDate {
|
||||
/// ```
|
||||
/// # use chrono::NaiveDate;
|
||||
/// # let parse_from_str = NaiveDate::parse_from_str;
|
||||
/// assert_eq!(parse_from_str("2014-5-17T12:34:56+09:30", "%Y-%m-%dT%H:%M:%S%z"),
|
||||
/// Ok(NaiveDate::from_ymd_opt(2014, 5, 17).unwrap()));
|
||||
/// assert_eq!(
|
||||
/// parse_from_str("2014-5-17T12:34:56+09:30", "%Y-%m-%dT%H:%M:%S%z"),
|
||||
/// Ok(NaiveDate::from_ymd_opt(2014, 5, 17).unwrap())
|
||||
/// );
|
||||
/// ```
|
||||
///
|
||||
/// Out-of-bound dates or insufficient fields are errors.
|
||||
@ -504,8 +512,8 @@ impl NaiveDate {
|
||||
///
|
||||
/// ```rust
|
||||
/// # use chrono::{NaiveDate};
|
||||
/// let (date, remainder) = NaiveDate::parse_and_remainder(
|
||||
/// "2015-02-18 trailing text", "%Y-%m-%d").unwrap();
|
||||
/// let (date, remainder) =
|
||||
/// NaiveDate::parse_and_remainder("2015-02-18 trailing text", "%Y-%m-%d").unwrap();
|
||||
/// assert_eq!(date, NaiveDate::from_ymd_opt(2015, 2, 18).unwrap());
|
||||
/// assert_eq!(remainder, " trailing text");
|
||||
/// ```
|
||||
@ -566,7 +574,8 @@ impl NaiveDate {
|
||||
/// );
|
||||
///
|
||||
/// assert_eq!(
|
||||
/// NaiveDate::from_ymd_opt(2014, 1, 1).unwrap()
|
||||
/// NaiveDate::from_ymd_opt(2014, 1, 1)
|
||||
/// .unwrap()
|
||||
/// .checked_sub_months(Months::new(core::i32::MAX as u32 + 1)),
|
||||
/// None
|
||||
/// );
|
||||
@ -716,7 +725,7 @@ impl NaiveDate {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{NaiveDate, NaiveTime, NaiveDateTime};
|
||||
/// use chrono::{NaiveDate, NaiveDateTime, NaiveTime};
|
||||
///
|
||||
/// let d = NaiveDate::from_ymd_opt(2015, 6, 3).unwrap();
|
||||
/// let t = NaiveTime::from_hms_milli_opt(12, 34, 56, 789).unwrap();
|
||||
@ -803,12 +812,12 @@ impl NaiveDate {
|
||||
/// use chrono::NaiveDate;
|
||||
///
|
||||
/// let d = NaiveDate::from_ymd_opt(2015, 6, 3).unwrap();
|
||||
/// assert!(d.and_hms_milli_opt(12, 34, 56, 789).is_some());
|
||||
/// assert!(d.and_hms_milli_opt(12, 34, 56, 789).is_some());
|
||||
/// assert!(d.and_hms_milli_opt(12, 34, 59, 1_789).is_some()); // leap second
|
||||
/// assert!(d.and_hms_milli_opt(12, 34, 59, 2_789).is_none());
|
||||
/// assert!(d.and_hms_milli_opt(12, 34, 60, 789).is_none());
|
||||
/// assert!(d.and_hms_milli_opt(12, 60, 56, 789).is_none());
|
||||
/// assert!(d.and_hms_milli_opt(24, 34, 56, 789).is_none());
|
||||
/// assert!(d.and_hms_milli_opt(12, 34, 60, 789).is_none());
|
||||
/// assert!(d.and_hms_milli_opt(12, 60, 56, 789).is_none());
|
||||
/// assert!(d.and_hms_milli_opt(24, 34, 56, 789).is_none());
|
||||
/// ```
|
||||
#[inline]
|
||||
#[must_use]
|
||||
@ -835,7 +844,7 @@ impl NaiveDate {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{NaiveDate, NaiveDateTime, Datelike, Timelike, Weekday};
|
||||
/// use chrono::{Datelike, NaiveDate, NaiveDateTime, Timelike, Weekday};
|
||||
///
|
||||
/// let d = NaiveDate::from_ymd_opt(2015, 6, 3).unwrap();
|
||||
///
|
||||
@ -867,12 +876,12 @@ impl NaiveDate {
|
||||
/// use chrono::NaiveDate;
|
||||
///
|
||||
/// let d = NaiveDate::from_ymd_opt(2015, 6, 3).unwrap();
|
||||
/// assert!(d.and_hms_micro_opt(12, 34, 56, 789_012).is_some());
|
||||
/// assert!(d.and_hms_micro_opt(12, 34, 56, 789_012).is_some());
|
||||
/// assert!(d.and_hms_micro_opt(12, 34, 59, 1_789_012).is_some()); // leap second
|
||||
/// assert!(d.and_hms_micro_opt(12, 34, 59, 2_789_012).is_none());
|
||||
/// assert!(d.and_hms_micro_opt(12, 34, 60, 789_012).is_none());
|
||||
/// assert!(d.and_hms_micro_opt(12, 60, 56, 789_012).is_none());
|
||||
/// assert!(d.and_hms_micro_opt(24, 34, 56, 789_012).is_none());
|
||||
/// assert!(d.and_hms_micro_opt(12, 34, 60, 789_012).is_none());
|
||||
/// assert!(d.and_hms_micro_opt(12, 60, 56, 789_012).is_none());
|
||||
/// assert!(d.and_hms_micro_opt(24, 34, 56, 789_012).is_none());
|
||||
/// ```
|
||||
#[inline]
|
||||
#[must_use]
|
||||
@ -917,12 +926,12 @@ impl NaiveDate {
|
||||
/// use chrono::NaiveDate;
|
||||
///
|
||||
/// let d = NaiveDate::from_ymd_opt(2015, 6, 3).unwrap();
|
||||
/// assert!(d.and_hms_nano_opt(12, 34, 56, 789_012_345).is_some());
|
||||
/// assert!(d.and_hms_nano_opt(12, 34, 56, 789_012_345).is_some());
|
||||
/// assert!(d.and_hms_nano_opt(12, 34, 59, 1_789_012_345).is_some()); // leap second
|
||||
/// assert!(d.and_hms_nano_opt(12, 34, 59, 2_789_012_345).is_none());
|
||||
/// assert!(d.and_hms_nano_opt(12, 34, 60, 789_012_345).is_none());
|
||||
/// assert!(d.and_hms_nano_opt(12, 60, 56, 789_012_345).is_none());
|
||||
/// assert!(d.and_hms_nano_opt(24, 34, 56, 789_012_345).is_none());
|
||||
/// assert!(d.and_hms_nano_opt(12, 34, 60, 789_012_345).is_none());
|
||||
/// assert!(d.and_hms_nano_opt(12, 60, 56, 789_012_345).is_none());
|
||||
/// assert!(d.and_hms_nano_opt(24, 34, 56, 789_012_345).is_none());
|
||||
/// ```
|
||||
#[inline]
|
||||
#[must_use]
|
||||
@ -980,8 +989,10 @@ impl NaiveDate {
|
||||
/// ```
|
||||
/// use chrono::NaiveDate;
|
||||
///
|
||||
/// assert_eq!(NaiveDate::from_ymd_opt(2015, 6, 3).unwrap().succ_opt(),
|
||||
/// Some(NaiveDate::from_ymd_opt(2015, 6, 4).unwrap()));
|
||||
/// assert_eq!(
|
||||
/// NaiveDate::from_ymd_opt(2015, 6, 3).unwrap().succ_opt(),
|
||||
/// Some(NaiveDate::from_ymd_opt(2015, 6, 4).unwrap())
|
||||
/// );
|
||||
/// assert_eq!(NaiveDate::MAX.succ_opt(), None);
|
||||
/// ```
|
||||
#[inline]
|
||||
@ -1017,8 +1028,10 @@ impl NaiveDate {
|
||||
/// ```
|
||||
/// use chrono::NaiveDate;
|
||||
///
|
||||
/// assert_eq!(NaiveDate::from_ymd_opt(2015, 6, 3).unwrap().pred_opt(),
|
||||
/// Some(NaiveDate::from_ymd_opt(2015, 6, 2).unwrap()));
|
||||
/// assert_eq!(
|
||||
/// NaiveDate::from_ymd_opt(2015, 6, 3).unwrap().pred_opt(),
|
||||
/// Some(NaiveDate::from_ymd_opt(2015, 6, 2).unwrap())
|
||||
/// );
|
||||
/// assert_eq!(NaiveDate::MIN.pred_opt(), None);
|
||||
/// ```
|
||||
#[inline]
|
||||
@ -1040,13 +1053,17 @@ impl NaiveDate {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{TimeDelta, NaiveDate};
|
||||
/// use chrono::{NaiveDate, TimeDelta};
|
||||
///
|
||||
/// let d = NaiveDate::from_ymd_opt(2015, 9, 5).unwrap();
|
||||
/// assert_eq!(d.checked_add_signed(TimeDelta::days(40)),
|
||||
/// Some(NaiveDate::from_ymd_opt(2015, 10, 15).unwrap()));
|
||||
/// assert_eq!(d.checked_add_signed(TimeDelta::days(-40)),
|
||||
/// Some(NaiveDate::from_ymd_opt(2015, 7, 27).unwrap()));
|
||||
/// assert_eq!(
|
||||
/// d.checked_add_signed(TimeDelta::days(40)),
|
||||
/// Some(NaiveDate::from_ymd_opt(2015, 10, 15).unwrap())
|
||||
/// );
|
||||
/// assert_eq!(
|
||||
/// d.checked_add_signed(TimeDelta::days(-40)),
|
||||
/// Some(NaiveDate::from_ymd_opt(2015, 7, 27).unwrap())
|
||||
/// );
|
||||
/// assert_eq!(d.checked_add_signed(TimeDelta::days(1_000_000_000)), None);
|
||||
/// assert_eq!(d.checked_add_signed(TimeDelta::days(-1_000_000_000)), None);
|
||||
/// assert_eq!(NaiveDate::MAX.checked_add_signed(TimeDelta::days(1)), None);
|
||||
@ -1069,13 +1086,17 @@ impl NaiveDate {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{TimeDelta, NaiveDate};
|
||||
/// use chrono::{NaiveDate, TimeDelta};
|
||||
///
|
||||
/// let d = NaiveDate::from_ymd_opt(2015, 9, 5).unwrap();
|
||||
/// assert_eq!(d.checked_sub_signed(TimeDelta::days(40)),
|
||||
/// Some(NaiveDate::from_ymd_opt(2015, 7, 27).unwrap()));
|
||||
/// assert_eq!(d.checked_sub_signed(TimeDelta::days(-40)),
|
||||
/// Some(NaiveDate::from_ymd_opt(2015, 10, 15).unwrap()));
|
||||
/// assert_eq!(
|
||||
/// d.checked_sub_signed(TimeDelta::days(40)),
|
||||
/// Some(NaiveDate::from_ymd_opt(2015, 7, 27).unwrap())
|
||||
/// );
|
||||
/// assert_eq!(
|
||||
/// d.checked_sub_signed(TimeDelta::days(-40)),
|
||||
/// Some(NaiveDate::from_ymd_opt(2015, 10, 15).unwrap())
|
||||
/// );
|
||||
/// assert_eq!(d.checked_sub_signed(TimeDelta::days(1_000_000_000)), None);
|
||||
/// assert_eq!(d.checked_sub_signed(TimeDelta::days(-1_000_000_000)), None);
|
||||
/// assert_eq!(NaiveDate::MIN.checked_sub_signed(TimeDelta::days(1)), None);
|
||||
@ -1098,7 +1119,7 @@ impl NaiveDate {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{TimeDelta, NaiveDate};
|
||||
/// use chrono::{NaiveDate, TimeDelta};
|
||||
///
|
||||
/// let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap();
|
||||
/// let since = NaiveDate::signed_duration_since;
|
||||
@ -1108,8 +1129,8 @@ impl NaiveDate {
|
||||
/// assert_eq!(since(from_ymd(2014, 1, 1), from_ymd(2014, 1, 2)), TimeDelta::days(-1));
|
||||
/// assert_eq!(since(from_ymd(2014, 1, 1), from_ymd(2013, 9, 23)), TimeDelta::days(100));
|
||||
/// assert_eq!(since(from_ymd(2014, 1, 1), from_ymd(2013, 1, 1)), TimeDelta::days(365));
|
||||
/// assert_eq!(since(from_ymd(2014, 1, 1), from_ymd(2010, 1, 1)), TimeDelta::days(365*4 + 1));
|
||||
/// assert_eq!(since(from_ymd(2014, 1, 1), from_ymd(1614, 1, 1)), TimeDelta::days(365*400 + 97));
|
||||
/// assert_eq!(since(from_ymd(2014, 1, 1), from_ymd(2010, 1, 1)), TimeDelta::days(365 * 4 + 1));
|
||||
/// assert_eq!(since(from_ymd(2014, 1, 1), from_ymd(1614, 1, 1)), TimeDelta::days(365 * 400 + 97));
|
||||
/// ```
|
||||
#[must_use]
|
||||
pub const fn signed_duration_since(self, rhs: NaiveDate) -> TimeDelta {
|
||||
@ -1151,13 +1172,13 @@ impl NaiveDate {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::NaiveDate;
|
||||
/// use chrono::format::strftime::StrftimeItems;
|
||||
/// use chrono::NaiveDate;
|
||||
///
|
||||
/// let fmt = StrftimeItems::new("%Y-%m-%d");
|
||||
/// let d = NaiveDate::from_ymd_opt(2015, 9, 5).unwrap();
|
||||
/// assert_eq!(d.format_with_items(fmt.clone()).to_string(), "2015-09-05");
|
||||
/// assert_eq!(d.format("%Y-%m-%d").to_string(), "2015-09-05");
|
||||
/// assert_eq!(d.format("%Y-%m-%d").to_string(), "2015-09-05");
|
||||
/// ```
|
||||
///
|
||||
/// The resulting `DelayedFormat` can be formatted directly via the `Display` trait.
|
||||
@ -1266,8 +1287,8 @@ impl NaiveDate {
|
||||
///
|
||||
/// let mut count = 0;
|
||||
/// for (idx, d) in NaiveDate::from_ymd_opt(2016, 2, 27).unwrap().iter_days().take(4).enumerate() {
|
||||
/// assert_eq!(d, expected[idx]);
|
||||
/// count += 1;
|
||||
/// assert_eq!(d, expected[idx]);
|
||||
/// count += 1;
|
||||
/// }
|
||||
/// assert_eq!(count, 4);
|
||||
///
|
||||
@ -1297,8 +1318,8 @@ impl NaiveDate {
|
||||
///
|
||||
/// let mut count = 0;
|
||||
/// for (idx, d) in NaiveDate::from_ymd_opt(2016, 2, 27).unwrap().iter_weeks().take(4).enumerate() {
|
||||
/// assert_eq!(d, expected[idx]);
|
||||
/// count += 1;
|
||||
/// assert_eq!(d, expected[idx]);
|
||||
/// count += 1;
|
||||
/// }
|
||||
/// assert_eq!(count, 4);
|
||||
///
|
||||
@ -1430,7 +1451,7 @@ impl Datelike for NaiveDate {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{NaiveDate, Datelike};
|
||||
/// use chrono::{Datelike, NaiveDate};
|
||||
///
|
||||
/// assert_eq!(NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().year(), 2015);
|
||||
/// assert_eq!(NaiveDate::from_ymd_opt(-308, 3, 14).unwrap().year(), -308); // 309 BCE
|
||||
@ -1447,7 +1468,7 @@ impl Datelike for NaiveDate {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{NaiveDate, Datelike};
|
||||
/// use chrono::{Datelike, NaiveDate};
|
||||
///
|
||||
/// assert_eq!(NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().month(), 9);
|
||||
/// assert_eq!(NaiveDate::from_ymd_opt(-308, 3, 14).unwrap().month(), 3);
|
||||
@ -1464,7 +1485,7 @@ impl Datelike for NaiveDate {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{NaiveDate, Datelike};
|
||||
/// use chrono::{Datelike, NaiveDate};
|
||||
///
|
||||
/// assert_eq!(NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().month0(), 8);
|
||||
/// assert_eq!(NaiveDate::from_ymd_opt(-308, 3, 14).unwrap().month0(), 2);
|
||||
@ -1481,7 +1502,7 @@ impl Datelike for NaiveDate {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{NaiveDate, Datelike};
|
||||
/// use chrono::{Datelike, NaiveDate};
|
||||
///
|
||||
/// assert_eq!(NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().day(), 8);
|
||||
/// assert_eq!(NaiveDate::from_ymd_opt(-308, 3, 14).unwrap().day(), 14);
|
||||
@ -1492,7 +1513,7 @@ impl Datelike for NaiveDate {
|
||||
/// (Note that this panics when `year` is out of range.)
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{NaiveDate, Datelike};
|
||||
/// use chrono::{Datelike, NaiveDate};
|
||||
///
|
||||
/// fn ndays_in_month(year: i32, month: u32) -> u32 {
|
||||
/// // the first day of the next month...
|
||||
@ -1521,7 +1542,7 @@ impl Datelike for NaiveDate {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{NaiveDate, Datelike};
|
||||
/// use chrono::{Datelike, NaiveDate};
|
||||
///
|
||||
/// assert_eq!(NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().day0(), 7);
|
||||
/// assert_eq!(NaiveDate::from_ymd_opt(-308, 3, 14).unwrap().day0(), 13);
|
||||
@ -1538,7 +1559,7 @@ impl Datelike for NaiveDate {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{NaiveDate, Datelike};
|
||||
/// use chrono::{Datelike, NaiveDate};
|
||||
///
|
||||
/// assert_eq!(NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().ordinal(), 251);
|
||||
/// assert_eq!(NaiveDate::from_ymd_opt(-308, 3, 14).unwrap().ordinal(), 74);
|
||||
@ -1549,7 +1570,7 @@ impl Datelike for NaiveDate {
|
||||
/// (Note that this panics when `year` is out of range.)
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{NaiveDate, Datelike};
|
||||
/// use chrono::{Datelike, NaiveDate};
|
||||
///
|
||||
/// fn ndays_in_year(year: i32) -> u32 {
|
||||
/// // the first day of the next year...
|
||||
@ -1577,7 +1598,7 @@ impl Datelike for NaiveDate {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{NaiveDate, Datelike};
|
||||
/// use chrono::{Datelike, NaiveDate};
|
||||
///
|
||||
/// assert_eq!(NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().ordinal0(), 250);
|
||||
/// assert_eq!(NaiveDate::from_ymd_opt(-308, 3, 14).unwrap().ordinal0(), 73);
|
||||
@ -1592,7 +1613,7 @@ impl Datelike for NaiveDate {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{NaiveDate, Datelike, Weekday};
|
||||
/// use chrono::{Datelike, NaiveDate, Weekday};
|
||||
///
|
||||
/// assert_eq!(NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().weekday(), Weekday::Tue);
|
||||
/// assert_eq!(NaiveDate::from_ymd_opt(-308, 3, 14).unwrap().weekday(), Weekday::Fri);
|
||||
@ -1617,12 +1638,16 @@ impl Datelike for NaiveDate {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{NaiveDate, Datelike};
|
||||
/// use chrono::{Datelike, NaiveDate};
|
||||
///
|
||||
/// assert_eq!(NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().with_year(2016),
|
||||
/// Some(NaiveDate::from_ymd_opt(2016, 9, 8).unwrap()));
|
||||
/// assert_eq!(NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().with_year(-308),
|
||||
/// Some(NaiveDate::from_ymd_opt(-308, 9, 8).unwrap()));
|
||||
/// assert_eq!(
|
||||
/// NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().with_year(2016),
|
||||
/// Some(NaiveDate::from_ymd_opt(2016, 9, 8).unwrap())
|
||||
/// );
|
||||
/// assert_eq!(
|
||||
/// NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().with_year(-308),
|
||||
/// Some(NaiveDate::from_ymd_opt(-308, 9, 8).unwrap())
|
||||
/// );
|
||||
/// ```
|
||||
///
|
||||
/// A leap day (February 29) is a good example that this method can return `None`.
|
||||
@ -1653,12 +1678,15 @@ impl Datelike for NaiveDate {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{NaiveDate, Datelike};
|
||||
/// use chrono::{Datelike, NaiveDate};
|
||||
///
|
||||
/// assert_eq!(NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().with_month(10),
|
||||
/// Some(NaiveDate::from_ymd_opt(2015, 10, 8).unwrap()));
|
||||
/// assert_eq!(
|
||||
/// NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().with_month(10),
|
||||
/// Some(NaiveDate::from_ymd_opt(2015, 10, 8).unwrap())
|
||||
/// );
|
||||
/// assert_eq!(NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().with_month(13), None); // no month 13
|
||||
/// assert_eq!(NaiveDate::from_ymd_opt(2015, 9, 30).unwrap().with_month(2), None); // no February 30
|
||||
/// assert_eq!(NaiveDate::from_ymd_opt(2015, 9, 30).unwrap().with_month(2), None);
|
||||
/// // no February 30
|
||||
/// ```
|
||||
#[inline]
|
||||
fn with_month(&self, month: u32) -> Option<NaiveDate> {
|
||||
@ -1675,12 +1703,15 @@ impl Datelike for NaiveDate {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{NaiveDate, Datelike};
|
||||
/// use chrono::{Datelike, NaiveDate};
|
||||
///
|
||||
/// assert_eq!(NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().with_month0(9),
|
||||
/// Some(NaiveDate::from_ymd_opt(2015, 10, 8).unwrap()));
|
||||
/// assert_eq!(
|
||||
/// NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().with_month0(9),
|
||||
/// Some(NaiveDate::from_ymd_opt(2015, 10, 8).unwrap())
|
||||
/// );
|
||||
/// assert_eq!(NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().with_month0(12), None); // no month 13
|
||||
/// assert_eq!(NaiveDate::from_ymd_opt(2015, 9, 30).unwrap().with_month0(1), None); // no February 30
|
||||
/// assert_eq!(NaiveDate::from_ymd_opt(2015, 9, 30).unwrap().with_month0(1), None);
|
||||
/// // no February 30
|
||||
/// ```
|
||||
#[inline]
|
||||
fn with_month0(&self, month0: u32) -> Option<NaiveDate> {
|
||||
@ -1697,12 +1728,14 @@ impl Datelike for NaiveDate {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{NaiveDate, Datelike};
|
||||
/// use chrono::{Datelike, NaiveDate};
|
||||
///
|
||||
/// assert_eq!(NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().with_day(30),
|
||||
/// Some(NaiveDate::from_ymd_opt(2015, 9, 30).unwrap()));
|
||||
/// assert_eq!(NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().with_day(31),
|
||||
/// None); // no September 31
|
||||
/// assert_eq!(
|
||||
/// NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().with_day(30),
|
||||
/// Some(NaiveDate::from_ymd_opt(2015, 9, 30).unwrap())
|
||||
/// );
|
||||
/// assert_eq!(NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().with_day(31), None);
|
||||
/// // no September 31
|
||||
/// ```
|
||||
#[inline]
|
||||
fn with_day(&self, day: u32) -> Option<NaiveDate> {
|
||||
@ -1718,12 +1751,14 @@ impl Datelike for NaiveDate {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{NaiveDate, Datelike};
|
||||
/// use chrono::{Datelike, NaiveDate};
|
||||
///
|
||||
/// assert_eq!(NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().with_day0(29),
|
||||
/// Some(NaiveDate::from_ymd_opt(2015, 9, 30).unwrap()));
|
||||
/// assert_eq!(NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().with_day0(30),
|
||||
/// None); // no September 31
|
||||
/// assert_eq!(
|
||||
/// NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().with_day0(29),
|
||||
/// Some(NaiveDate::from_ymd_opt(2015, 9, 30).unwrap())
|
||||
/// );
|
||||
/// assert_eq!(NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().with_day0(30), None);
|
||||
/// // no September 31
|
||||
/// ```
|
||||
#[inline]
|
||||
fn with_day0(&self, day0: u32) -> Option<NaiveDate> {
|
||||
@ -1807,18 +1842,18 @@ impl Datelike for NaiveDate {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{TimeDelta, NaiveDate};
|
||||
/// use chrono::{NaiveDate, TimeDelta};
|
||||
///
|
||||
/// let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap();
|
||||
///
|
||||
/// assert_eq!(from_ymd(2014, 1, 1) + TimeDelta::zero(), from_ymd(2014, 1, 1));
|
||||
/// assert_eq!(from_ymd(2014, 1, 1) + TimeDelta::seconds(86399), from_ymd(2014, 1, 1));
|
||||
/// assert_eq!(from_ymd(2014, 1, 1) + TimeDelta::seconds(-86399), from_ymd(2014, 1, 1));
|
||||
/// assert_eq!(from_ymd(2014, 1, 1) + TimeDelta::days(1), from_ymd(2014, 1, 2));
|
||||
/// assert_eq!(from_ymd(2014, 1, 1) + TimeDelta::days(-1), from_ymd(2013, 12, 31));
|
||||
/// assert_eq!(from_ymd(2014, 1, 1) + TimeDelta::days(364), from_ymd(2014, 12, 31));
|
||||
/// assert_eq!(from_ymd(2014, 1, 1) + TimeDelta::days(365*4 + 1), from_ymd(2018, 1, 1));
|
||||
/// assert_eq!(from_ymd(2014, 1, 1) + TimeDelta::days(365*400 + 97), from_ymd(2414, 1, 1));
|
||||
/// assert_eq!(from_ymd(2014, 1, 1) + TimeDelta::zero(), from_ymd(2014, 1, 1));
|
||||
/// assert_eq!(from_ymd(2014, 1, 1) + TimeDelta::seconds(86399), from_ymd(2014, 1, 1));
|
||||
/// assert_eq!(from_ymd(2014, 1, 1) + TimeDelta::seconds(-86399), from_ymd(2014, 1, 1));
|
||||
/// assert_eq!(from_ymd(2014, 1, 1) + TimeDelta::days(1), from_ymd(2014, 1, 2));
|
||||
/// assert_eq!(from_ymd(2014, 1, 1) + TimeDelta::days(-1), from_ymd(2013, 12, 31));
|
||||
/// assert_eq!(from_ymd(2014, 1, 1) + TimeDelta::days(364), from_ymd(2014, 12, 31));
|
||||
/// assert_eq!(from_ymd(2014, 1, 1) + TimeDelta::days(365 * 4 + 1), from_ymd(2018, 1, 1));
|
||||
/// assert_eq!(from_ymd(2014, 1, 1) + TimeDelta::days(365 * 400 + 97), from_ymd(2414, 1, 1));
|
||||
/// ```
|
||||
///
|
||||
/// [`NaiveDate::checked_add_signed`]: crate::NaiveDate::checked_add_signed
|
||||
@ -1860,7 +1895,7 @@ impl AddAssign<TimeDelta> for NaiveDate {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{NaiveDate, Months};
|
||||
/// use chrono::{Months, NaiveDate};
|
||||
///
|
||||
/// let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap();
|
||||
///
|
||||
@ -1892,7 +1927,7 @@ impl Add<Months> for NaiveDate {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{NaiveDate, Months};
|
||||
/// use chrono::{Months, NaiveDate};
|
||||
///
|
||||
/// let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap();
|
||||
///
|
||||
@ -1950,18 +1985,18 @@ impl Sub<Days> for NaiveDate {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{TimeDelta, NaiveDate};
|
||||
/// use chrono::{NaiveDate, TimeDelta};
|
||||
///
|
||||
/// let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap();
|
||||
///
|
||||
/// assert_eq!(from_ymd(2014, 1, 1) - TimeDelta::zero(), from_ymd(2014, 1, 1));
|
||||
/// assert_eq!(from_ymd(2014, 1, 1) - TimeDelta::seconds(86399), from_ymd(2014, 1, 1));
|
||||
/// assert_eq!(from_ymd(2014, 1, 1) - TimeDelta::seconds(-86399), from_ymd(2014, 1, 1));
|
||||
/// assert_eq!(from_ymd(2014, 1, 1) - TimeDelta::days(1), from_ymd(2013, 12, 31));
|
||||
/// assert_eq!(from_ymd(2014, 1, 1) - TimeDelta::days(-1), from_ymd(2014, 1, 2));
|
||||
/// assert_eq!(from_ymd(2014, 1, 1) - TimeDelta::days(364), from_ymd(2013, 1, 2));
|
||||
/// assert_eq!(from_ymd(2014, 1, 1) - TimeDelta::days(365*4 + 1), from_ymd(2010, 1, 1));
|
||||
/// assert_eq!(from_ymd(2014, 1, 1) - TimeDelta::days(365*400 + 97), from_ymd(1614, 1, 1));
|
||||
/// assert_eq!(from_ymd(2014, 1, 1) - TimeDelta::zero(), from_ymd(2014, 1, 1));
|
||||
/// assert_eq!(from_ymd(2014, 1, 1) - TimeDelta::seconds(86399), from_ymd(2014, 1, 1));
|
||||
/// assert_eq!(from_ymd(2014, 1, 1) - TimeDelta::seconds(-86399), from_ymd(2014, 1, 1));
|
||||
/// assert_eq!(from_ymd(2014, 1, 1) - TimeDelta::days(1), from_ymd(2013, 12, 31));
|
||||
/// assert_eq!(from_ymd(2014, 1, 1) - TimeDelta::days(-1), from_ymd(2014, 1, 2));
|
||||
/// assert_eq!(from_ymd(2014, 1, 1) - TimeDelta::days(364), from_ymd(2013, 1, 2));
|
||||
/// assert_eq!(from_ymd(2014, 1, 1) - TimeDelta::days(365 * 4 + 1), from_ymd(2010, 1, 1));
|
||||
/// assert_eq!(from_ymd(2014, 1, 1) - TimeDelta::days(365 * 400 + 97), from_ymd(1614, 1, 1));
|
||||
/// ```
|
||||
///
|
||||
/// [`NaiveDate::checked_sub_signed`]: crate::NaiveDate::checked_sub_signed
|
||||
@ -2003,7 +2038,7 @@ impl SubAssign<TimeDelta> for NaiveDate {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{TimeDelta, NaiveDate};
|
||||
/// use chrono::{NaiveDate, TimeDelta};
|
||||
///
|
||||
/// let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap();
|
||||
///
|
||||
@ -2012,8 +2047,8 @@ impl SubAssign<TimeDelta> for NaiveDate {
|
||||
/// assert_eq!(from_ymd(2014, 1, 1) - from_ymd(2014, 1, 2), TimeDelta::days(-1));
|
||||
/// assert_eq!(from_ymd(2014, 1, 1) - from_ymd(2013, 9, 23), TimeDelta::days(100));
|
||||
/// assert_eq!(from_ymd(2014, 1, 1) - from_ymd(2013, 1, 1), TimeDelta::days(365));
|
||||
/// assert_eq!(from_ymd(2014, 1, 1) - from_ymd(2010, 1, 1), TimeDelta::days(365*4 + 1));
|
||||
/// assert_eq!(from_ymd(2014, 1, 1) - from_ymd(1614, 1, 1), TimeDelta::days(365*400 + 97));
|
||||
/// assert_eq!(from_ymd(2014, 1, 1) - from_ymd(2010, 1, 1), TimeDelta::days(365 * 4 + 1));
|
||||
/// assert_eq!(from_ymd(2014, 1, 1) - from_ymd(1614, 1, 1), TimeDelta::days(365 * 400 + 97));
|
||||
/// ```
|
||||
impl Sub<NaiveDate> for NaiveDate {
|
||||
type Output = TimeDelta;
|
||||
@ -2109,8 +2144,8 @@ impl FusedIterator for NaiveDateWeeksIterator {}
|
||||
/// ```
|
||||
/// use chrono::NaiveDate;
|
||||
///
|
||||
/// assert_eq!(format!("{:?}", NaiveDate::from_ymd_opt(2015, 9, 5).unwrap()), "2015-09-05");
|
||||
/// assert_eq!(format!("{:?}", NaiveDate::from_ymd_opt( 0, 1, 1).unwrap()), "0000-01-01");
|
||||
/// assert_eq!(format!("{:?}", NaiveDate::from_ymd_opt(2015, 9, 5).unwrap()), "2015-09-05");
|
||||
/// assert_eq!(format!("{:?}", NaiveDate::from_ymd_opt(0, 1, 1).unwrap()), "0000-01-01");
|
||||
/// assert_eq!(format!("{:?}", NaiveDate::from_ymd_opt(9999, 12, 31).unwrap()), "9999-12-31");
|
||||
/// ```
|
||||
///
|
||||
@ -2118,7 +2153,7 @@ impl FusedIterator for NaiveDateWeeksIterator {}
|
||||
///
|
||||
/// ```
|
||||
/// # use chrono::NaiveDate;
|
||||
/// assert_eq!(format!("{:?}", NaiveDate::from_ymd_opt( -1, 1, 1).unwrap()), "-0001-01-01");
|
||||
/// assert_eq!(format!("{:?}", NaiveDate::from_ymd_opt(-1, 1, 1).unwrap()), "-0001-01-01");
|
||||
/// assert_eq!(format!("{:?}", NaiveDate::from_ymd_opt(10000, 12, 31).unwrap()), "+10000-12-31");
|
||||
/// ```
|
||||
impl fmt::Debug for NaiveDate {
|
||||
@ -2152,8 +2187,8 @@ impl fmt::Debug for NaiveDate {
|
||||
/// ```
|
||||
/// use chrono::NaiveDate;
|
||||
///
|
||||
/// assert_eq!(format!("{}", NaiveDate::from_ymd_opt(2015, 9, 5).unwrap()), "2015-09-05");
|
||||
/// assert_eq!(format!("{}", NaiveDate::from_ymd_opt( 0, 1, 1).unwrap()), "0000-01-01");
|
||||
/// assert_eq!(format!("{}", NaiveDate::from_ymd_opt(2015, 9, 5).unwrap()), "2015-09-05");
|
||||
/// assert_eq!(format!("{}", NaiveDate::from_ymd_opt(0, 1, 1).unwrap()), "0000-01-01");
|
||||
/// assert_eq!(format!("{}", NaiveDate::from_ymd_opt(9999, 12, 31).unwrap()), "9999-12-31");
|
||||
/// ```
|
||||
///
|
||||
@ -2161,7 +2196,7 @@ impl fmt::Debug for NaiveDate {
|
||||
///
|
||||
/// ```
|
||||
/// # use chrono::NaiveDate;
|
||||
/// assert_eq!(format!("{}", NaiveDate::from_ymd_opt( -1, 1, 1).unwrap()), "-0001-01-01");
|
||||
/// assert_eq!(format!("{}", NaiveDate::from_ymd_opt(-1, 1, 1).unwrap()), "-0001-01-01");
|
||||
/// assert_eq!(format!("{}", NaiveDate::from_ymd_opt(10000, 12, 31).unwrap()), "+10000-12-31");
|
||||
/// ```
|
||||
impl fmt::Display for NaiveDate {
|
||||
|
@ -58,7 +58,8 @@ pub const MAX_DATETIME: NaiveDateTime = NaiveDateTime::MAX;
|
||||
/// ```
|
||||
/// use chrono::{NaiveDate, NaiveDateTime};
|
||||
///
|
||||
/// let dt: NaiveDateTime = NaiveDate::from_ymd_opt(2016, 7, 8).unwrap().and_hms_opt(9, 10, 11).unwrap();
|
||||
/// let dt: NaiveDateTime =
|
||||
/// NaiveDate::from_ymd_opt(2016, 7, 8).unwrap().and_hms_opt(9, 10, 11).unwrap();
|
||||
/// # let _ = dt;
|
||||
/// ```
|
||||
///
|
||||
@ -95,7 +96,7 @@ impl NaiveDateTime {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{NaiveDate, NaiveTime, NaiveDateTime};
|
||||
/// use chrono::{NaiveDate, NaiveDateTime, NaiveTime};
|
||||
///
|
||||
/// let d = NaiveDate::from_ymd_opt(2015, 6, 3).unwrap();
|
||||
/// let t = NaiveTime::from_hms_milli_opt(12, 34, 56, 789).unwrap();
|
||||
@ -284,14 +285,21 @@ impl NaiveDateTime {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{NaiveDateTime, NaiveDate};
|
||||
/// use chrono::{NaiveDate, NaiveDateTime};
|
||||
///
|
||||
/// let parse_from_str = NaiveDateTime::parse_from_str;
|
||||
///
|
||||
/// assert_eq!(parse_from_str("2015-09-05 23:56:04", "%Y-%m-%d %H:%M:%S"),
|
||||
/// Ok(NaiveDate::from_ymd_opt(2015, 9, 5).unwrap().and_hms_opt(23, 56, 4).unwrap()));
|
||||
/// assert_eq!(parse_from_str("5sep2015pm012345.6789", "%d%b%Y%p%I%M%S%.f"),
|
||||
/// Ok(NaiveDate::from_ymd_opt(2015, 9, 5).unwrap().and_hms_micro_opt(13, 23, 45, 678_900).unwrap()));
|
||||
/// assert_eq!(
|
||||
/// parse_from_str("2015-09-05 23:56:04", "%Y-%m-%d %H:%M:%S"),
|
||||
/// Ok(NaiveDate::from_ymd_opt(2015, 9, 5).unwrap().and_hms_opt(23, 56, 4).unwrap())
|
||||
/// );
|
||||
/// assert_eq!(
|
||||
/// parse_from_str("5sep2015pm012345.6789", "%d%b%Y%p%I%M%S%.f"),
|
||||
/// Ok(NaiveDate::from_ymd_opt(2015, 9, 5)
|
||||
/// .unwrap()
|
||||
/// .and_hms_micro_opt(13, 23, 45, 678_900)
|
||||
/// .unwrap())
|
||||
/// );
|
||||
/// ```
|
||||
///
|
||||
/// Offset is ignored for the purpose of parsing.
|
||||
@ -299,8 +307,10 @@ impl NaiveDateTime {
|
||||
/// ```
|
||||
/// # use chrono::{NaiveDateTime, NaiveDate};
|
||||
/// # let parse_from_str = NaiveDateTime::parse_from_str;
|
||||
/// assert_eq!(parse_from_str("2014-5-17T12:34:56+09:30", "%Y-%m-%dT%H:%M:%S%z"),
|
||||
/// Ok(NaiveDate::from_ymd_opt(2014, 5, 17).unwrap().and_hms_opt(12, 34, 56).unwrap()));
|
||||
/// assert_eq!(
|
||||
/// parse_from_str("2014-5-17T12:34:56+09:30", "%Y-%m-%dT%H:%M:%S%z"),
|
||||
/// Ok(NaiveDate::from_ymd_opt(2014, 5, 17).unwrap().and_hms_opt(12, 34, 56).unwrap())
|
||||
/// );
|
||||
/// ```
|
||||
///
|
||||
/// [Leap seconds](./struct.NaiveTime.html#leap-second-handling) are correctly handled by
|
||||
@ -310,8 +320,13 @@ impl NaiveDateTime {
|
||||
/// ```
|
||||
/// # use chrono::{NaiveDateTime, NaiveDate};
|
||||
/// # let parse_from_str = NaiveDateTime::parse_from_str;
|
||||
/// assert_eq!(parse_from_str("2015-07-01 08:59:60.123", "%Y-%m-%d %H:%M:%S%.f"),
|
||||
/// Ok(NaiveDate::from_ymd_opt(2015, 7, 1).unwrap().and_hms_milli_opt(8, 59, 59, 1_123).unwrap()));
|
||||
/// assert_eq!(
|
||||
/// parse_from_str("2015-07-01 08:59:60.123", "%Y-%m-%d %H:%M:%S%.f"),
|
||||
/// Ok(NaiveDate::from_ymd_opt(2015, 7, 1)
|
||||
/// .unwrap()
|
||||
/// .and_hms_milli_opt(8, 59, 59, 1_123)
|
||||
/// .unwrap())
|
||||
/// );
|
||||
/// ```
|
||||
///
|
||||
/// Missing seconds are assumed to be zero,
|
||||
@ -320,8 +335,10 @@ impl NaiveDateTime {
|
||||
/// ```
|
||||
/// # use chrono::{NaiveDateTime, NaiveDate};
|
||||
/// # let parse_from_str = NaiveDateTime::parse_from_str;
|
||||
/// assert_eq!(parse_from_str("94/9/4 7:15", "%y/%m/%d %H:%M"),
|
||||
/// Ok(NaiveDate::from_ymd_opt(1994, 9, 4).unwrap().and_hms_opt(7, 15, 0).unwrap()));
|
||||
/// assert_eq!(
|
||||
/// parse_from_str("94/9/4 7:15", "%y/%m/%d %H:%M"),
|
||||
/// Ok(NaiveDate::from_ymd_opt(1994, 9, 4).unwrap().and_hms_opt(7, 15, 0).unwrap())
|
||||
/// );
|
||||
///
|
||||
/// assert!(parse_from_str("04m33s", "%Mm%Ss").is_err());
|
||||
/// assert!(parse_from_str("94/9/4 12", "%y/%m/%d %H").is_err());
|
||||
@ -347,7 +364,7 @@ impl NaiveDateTime {
|
||||
/// let fmt = "%Y-%m-%d %H:%M:%S";
|
||||
/// assert!(parse_from_str("10000-09-09 01:46:39", fmt).is_err());
|
||||
/// assert!(parse_from_str("+10000-09-09 01:46:39", fmt).is_ok());
|
||||
///```
|
||||
/// ```
|
||||
pub fn parse_from_str(s: &str, fmt: &str) -> ParseResult<NaiveDateTime> {
|
||||
let mut parsed = Parsed::new();
|
||||
parse(&mut parsed, s, StrftimeItems::new(fmt))?;
|
||||
@ -366,7 +383,10 @@ impl NaiveDateTime {
|
||||
/// ```rust
|
||||
/// # use chrono::{NaiveDate, NaiveDateTime};
|
||||
/// let (datetime, remainder) = NaiveDateTime::parse_and_remainder(
|
||||
/// "2015-02-18 23:16:09 trailing text", "%Y-%m-%d %H:%M:%S").unwrap();
|
||||
/// "2015-02-18 23:16:09 trailing text",
|
||||
/// "%Y-%m-%d %H:%M:%S",
|
||||
/// )
|
||||
/// .unwrap();
|
||||
/// assert_eq!(
|
||||
/// datetime,
|
||||
/// NaiveDate::from_ymd_opt(2015, 2, 18).unwrap().and_hms_opt(23, 16, 9).unwrap()
|
||||
@ -453,10 +473,12 @@ impl NaiveDateTime {
|
||||
/// let dt = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap().and_hms_milli_opt(0, 0, 1, 444).unwrap();
|
||||
/// assert_eq!(dt.timestamp_millis(), 1_444);
|
||||
///
|
||||
/// let dt = NaiveDate::from_ymd_opt(2001, 9, 9).unwrap().and_hms_milli_opt(1, 46, 40, 555).unwrap();
|
||||
/// let dt =
|
||||
/// NaiveDate::from_ymd_opt(2001, 9, 9).unwrap().and_hms_milli_opt(1, 46, 40, 555).unwrap();
|
||||
/// assert_eq!(dt.timestamp_millis(), 1_000_000_000_555);
|
||||
///
|
||||
/// let dt = NaiveDate::from_ymd_opt(1969, 12, 31).unwrap().and_hms_milli_opt(23, 59, 59, 100).unwrap();
|
||||
/// let dt =
|
||||
/// NaiveDate::from_ymd_opt(1969, 12, 31).unwrap().and_hms_milli_opt(23, 59, 59, 100).unwrap();
|
||||
/// assert_eq!(dt.timestamp_millis(), -900);
|
||||
/// ```
|
||||
#[inline]
|
||||
@ -479,7 +501,8 @@ impl NaiveDateTime {
|
||||
/// let dt = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap().and_hms_micro_opt(0, 0, 1, 444).unwrap();
|
||||
/// assert_eq!(dt.timestamp_micros(), 1_000_444);
|
||||
///
|
||||
/// let dt = NaiveDate::from_ymd_opt(2001, 9, 9).unwrap().and_hms_micro_opt(1, 46, 40, 555).unwrap();
|
||||
/// let dt =
|
||||
/// NaiveDate::from_ymd_opt(2001, 9, 9).unwrap().and_hms_micro_opt(1, 46, 40, 555).unwrap();
|
||||
/// assert_eq!(dt.timestamp_micros(), 1_000_000_000_000_555);
|
||||
/// ```
|
||||
#[inline]
|
||||
@ -577,10 +600,16 @@ impl NaiveDateTime {
|
||||
/// ```
|
||||
/// use chrono::NaiveDate;
|
||||
///
|
||||
/// let dt = NaiveDate::from_ymd_opt(2016, 7, 8).unwrap().and_hms_nano_opt(9, 10, 11, 123_456_789).unwrap();
|
||||
/// let dt = NaiveDate::from_ymd_opt(2016, 7, 8)
|
||||
/// .unwrap()
|
||||
/// .and_hms_nano_opt(9, 10, 11, 123_456_789)
|
||||
/// .unwrap();
|
||||
/// assert_eq!(dt.timestamp_subsec_millis(), 123);
|
||||
///
|
||||
/// let dt = NaiveDate::from_ymd_opt(2015, 7, 1).unwrap().and_hms_nano_opt(8, 59, 59, 1_234_567_890).unwrap();
|
||||
/// let dt = NaiveDate::from_ymd_opt(2015, 7, 1)
|
||||
/// .unwrap()
|
||||
/// .and_hms_nano_opt(8, 59, 59, 1_234_567_890)
|
||||
/// .unwrap();
|
||||
/// assert_eq!(dt.timestamp_subsec_millis(), 1_234);
|
||||
/// ```
|
||||
#[inline]
|
||||
@ -599,10 +628,16 @@ impl NaiveDateTime {
|
||||
/// ```
|
||||
/// use chrono::NaiveDate;
|
||||
///
|
||||
/// let dt = NaiveDate::from_ymd_opt(2016, 7, 8).unwrap().and_hms_nano_opt(9, 10, 11, 123_456_789).unwrap();
|
||||
/// let dt = NaiveDate::from_ymd_opt(2016, 7, 8)
|
||||
/// .unwrap()
|
||||
/// .and_hms_nano_opt(9, 10, 11, 123_456_789)
|
||||
/// .unwrap();
|
||||
/// assert_eq!(dt.timestamp_subsec_micros(), 123_456);
|
||||
///
|
||||
/// let dt = NaiveDate::from_ymd_opt(2015, 7, 1).unwrap().and_hms_nano_opt(8, 59, 59, 1_234_567_890).unwrap();
|
||||
/// let dt = NaiveDate::from_ymd_opt(2015, 7, 1)
|
||||
/// .unwrap()
|
||||
/// .and_hms_nano_opt(8, 59, 59, 1_234_567_890)
|
||||
/// .unwrap();
|
||||
/// assert_eq!(dt.timestamp_subsec_micros(), 1_234_567);
|
||||
/// ```
|
||||
#[inline]
|
||||
@ -621,10 +656,16 @@ impl NaiveDateTime {
|
||||
/// ```
|
||||
/// use chrono::NaiveDate;
|
||||
///
|
||||
/// let dt = NaiveDate::from_ymd_opt(2016, 7, 8).unwrap().and_hms_nano_opt(9, 10, 11, 123_456_789).unwrap();
|
||||
/// let dt = NaiveDate::from_ymd_opt(2016, 7, 8)
|
||||
/// .unwrap()
|
||||
/// .and_hms_nano_opt(9, 10, 11, 123_456_789)
|
||||
/// .unwrap();
|
||||
/// assert_eq!(dt.timestamp_subsec_nanos(), 123_456_789);
|
||||
///
|
||||
/// let dt = NaiveDate::from_ymd_opt(2015, 7, 1).unwrap().and_hms_nano_opt(8, 59, 59, 1_234_567_890).unwrap();
|
||||
/// let dt = NaiveDate::from_ymd_opt(2015, 7, 1)
|
||||
/// .unwrap()
|
||||
/// .and_hms_nano_opt(8, 59, 59, 1_234_567_890)
|
||||
/// .unwrap();
|
||||
/// assert_eq!(dt.timestamp_subsec_nanos(), 1_234_567_890);
|
||||
/// ```
|
||||
#[inline]
|
||||
@ -647,26 +688,26 @@ impl NaiveDateTime {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{TimeDelta, NaiveDate};
|
||||
/// use chrono::{NaiveDate, TimeDelta};
|
||||
///
|
||||
/// let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap();
|
||||
///
|
||||
/// let d = from_ymd(2016, 7, 8);
|
||||
/// let hms = |h, m, s| d.and_hms_opt(h, m, s).unwrap();
|
||||
/// assert_eq!(hms(3, 5, 7).checked_add_signed(TimeDelta::zero()),
|
||||
/// Some(hms(3, 5, 7)));
|
||||
/// assert_eq!(hms(3, 5, 7).checked_add_signed(TimeDelta::seconds(1)),
|
||||
/// Some(hms(3, 5, 8)));
|
||||
/// assert_eq!(hms(3, 5, 7).checked_add_signed(TimeDelta::seconds(-1)),
|
||||
/// Some(hms(3, 5, 6)));
|
||||
/// assert_eq!(hms(3, 5, 7).checked_add_signed(TimeDelta::seconds(3600 + 60)),
|
||||
/// Some(hms(4, 6, 7)));
|
||||
/// assert_eq!(hms(3, 5, 7).checked_add_signed(TimeDelta::seconds(86_400)),
|
||||
/// Some(from_ymd(2016, 7, 9).and_hms_opt(3, 5, 7).unwrap()));
|
||||
/// assert_eq!(hms(3, 5, 7).checked_add_signed(TimeDelta::zero()), Some(hms(3, 5, 7)));
|
||||
/// assert_eq!(hms(3, 5, 7).checked_add_signed(TimeDelta::seconds(1)), Some(hms(3, 5, 8)));
|
||||
/// assert_eq!(hms(3, 5, 7).checked_add_signed(TimeDelta::seconds(-1)), Some(hms(3, 5, 6)));
|
||||
/// assert_eq!(hms(3, 5, 7).checked_add_signed(TimeDelta::seconds(3600 + 60)), Some(hms(4, 6, 7)));
|
||||
/// assert_eq!(
|
||||
/// hms(3, 5, 7).checked_add_signed(TimeDelta::seconds(86_400)),
|
||||
/// Some(from_ymd(2016, 7, 9).and_hms_opt(3, 5, 7).unwrap())
|
||||
/// );
|
||||
///
|
||||
/// let hmsm = |h, m, s, milli| d.and_hms_milli_opt(h, m, s, milli).unwrap();
|
||||
/// assert_eq!(hmsm(3, 5, 7, 980).checked_add_signed(TimeDelta::milliseconds(450)),
|
||||
/// Some(hmsm(3, 5, 8, 430)));
|
||||
/// assert_eq!(
|
||||
/// hmsm(3, 5, 7, 980).checked_add_signed(TimeDelta::milliseconds(450)),
|
||||
/// Some(hmsm(3, 5, 8, 430))
|
||||
/// );
|
||||
/// ```
|
||||
///
|
||||
/// Overflow returns `None`.
|
||||
@ -727,13 +768,19 @@ impl NaiveDateTime {
|
||||
/// use chrono::{Months, NaiveDate};
|
||||
///
|
||||
/// assert_eq!(
|
||||
/// NaiveDate::from_ymd_opt(2014, 1, 1).unwrap().and_hms_opt(1, 0, 0).unwrap()
|
||||
/// NaiveDate::from_ymd_opt(2014, 1, 1)
|
||||
/// .unwrap()
|
||||
/// .and_hms_opt(1, 0, 0)
|
||||
/// .unwrap()
|
||||
/// .checked_add_months(Months::new(1)),
|
||||
/// Some(NaiveDate::from_ymd_opt(2014, 2, 1).unwrap().and_hms_opt(1, 0, 0).unwrap())
|
||||
/// );
|
||||
///
|
||||
/// assert_eq!(
|
||||
/// NaiveDate::from_ymd_opt(2014, 1, 1).unwrap().and_hms_opt(1, 0, 0).unwrap()
|
||||
/// NaiveDate::from_ymd_opt(2014, 1, 1)
|
||||
/// .unwrap()
|
||||
/// .and_hms_opt(1, 0, 0)
|
||||
/// .unwrap()
|
||||
/// .checked_add_months(Months::new(core::i32::MAX as u32 + 1)),
|
||||
/// None
|
||||
/// );
|
||||
@ -821,26 +868,26 @@ impl NaiveDateTime {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{TimeDelta, NaiveDate};
|
||||
/// use chrono::{NaiveDate, TimeDelta};
|
||||
///
|
||||
/// let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap();
|
||||
///
|
||||
/// let d = from_ymd(2016, 7, 8);
|
||||
/// let hms = |h, m, s| d.and_hms_opt(h, m, s).unwrap();
|
||||
/// assert_eq!(hms(3, 5, 7).checked_sub_signed(TimeDelta::zero()),
|
||||
/// Some(hms(3, 5, 7)));
|
||||
/// assert_eq!(hms(3, 5, 7).checked_sub_signed(TimeDelta::seconds(1)),
|
||||
/// Some(hms(3, 5, 6)));
|
||||
/// assert_eq!(hms(3, 5, 7).checked_sub_signed(TimeDelta::seconds(-1)),
|
||||
/// Some(hms(3, 5, 8)));
|
||||
/// assert_eq!(hms(3, 5, 7).checked_sub_signed(TimeDelta::seconds(3600 + 60)),
|
||||
/// Some(hms(2, 4, 7)));
|
||||
/// assert_eq!(hms(3, 5, 7).checked_sub_signed(TimeDelta::seconds(86_400)),
|
||||
/// Some(from_ymd(2016, 7, 7).and_hms_opt(3, 5, 7).unwrap()));
|
||||
/// assert_eq!(hms(3, 5, 7).checked_sub_signed(TimeDelta::zero()), Some(hms(3, 5, 7)));
|
||||
/// assert_eq!(hms(3, 5, 7).checked_sub_signed(TimeDelta::seconds(1)), Some(hms(3, 5, 6)));
|
||||
/// assert_eq!(hms(3, 5, 7).checked_sub_signed(TimeDelta::seconds(-1)), Some(hms(3, 5, 8)));
|
||||
/// assert_eq!(hms(3, 5, 7).checked_sub_signed(TimeDelta::seconds(3600 + 60)), Some(hms(2, 4, 7)));
|
||||
/// assert_eq!(
|
||||
/// hms(3, 5, 7).checked_sub_signed(TimeDelta::seconds(86_400)),
|
||||
/// Some(from_ymd(2016, 7, 7).and_hms_opt(3, 5, 7).unwrap())
|
||||
/// );
|
||||
///
|
||||
/// let hmsm = |h, m, s, milli| d.and_hms_milli_opt(h, m, s, milli).unwrap();
|
||||
/// assert_eq!(hmsm(3, 5, 7, 450).checked_sub_signed(TimeDelta::milliseconds(670)),
|
||||
/// Some(hmsm(3, 5, 6, 780)));
|
||||
/// assert_eq!(
|
||||
/// hmsm(3, 5, 7, 450).checked_sub_signed(TimeDelta::milliseconds(670)),
|
||||
/// Some(hmsm(3, 5, 6, 780))
|
||||
/// );
|
||||
/// ```
|
||||
///
|
||||
/// Overflow returns `None`.
|
||||
@ -897,13 +944,19 @@ impl NaiveDateTime {
|
||||
/// use chrono::{Months, NaiveDate};
|
||||
///
|
||||
/// assert_eq!(
|
||||
/// NaiveDate::from_ymd_opt(2014, 1, 1).unwrap().and_hms_opt(1, 0, 0).unwrap()
|
||||
/// NaiveDate::from_ymd_opt(2014, 1, 1)
|
||||
/// .unwrap()
|
||||
/// .and_hms_opt(1, 0, 0)
|
||||
/// .unwrap()
|
||||
/// .checked_sub_months(Months::new(1)),
|
||||
/// Some(NaiveDate::from_ymd_opt(2013, 12, 1).unwrap().and_hms_opt(1, 0, 0).unwrap())
|
||||
/// );
|
||||
///
|
||||
/// assert_eq!(
|
||||
/// NaiveDate::from_ymd_opt(2014, 1, 1).unwrap().and_hms_opt(1, 0, 0).unwrap()
|
||||
/// NaiveDate::from_ymd_opt(2014, 1, 1)
|
||||
/// .unwrap()
|
||||
/// .and_hms_opt(1, 0, 0)
|
||||
/// .unwrap()
|
||||
/// .checked_sub_months(Months::new(core::i32::MAX as u32 + 1)),
|
||||
/// None
|
||||
/// );
|
||||
@ -941,18 +994,24 @@ impl NaiveDateTime {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{TimeDelta, NaiveDate};
|
||||
/// use chrono::{NaiveDate, TimeDelta};
|
||||
///
|
||||
/// let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap();
|
||||
///
|
||||
/// let d = from_ymd(2016, 7, 8);
|
||||
/// assert_eq!(d.and_hms_opt(3, 5, 7).unwrap().signed_duration_since(d.and_hms_opt(2, 4, 6).unwrap()),
|
||||
/// TimeDelta::seconds(3600 + 60 + 1));
|
||||
/// assert_eq!(
|
||||
/// d.and_hms_opt(3, 5, 7).unwrap().signed_duration_since(d.and_hms_opt(2, 4, 6).unwrap()),
|
||||
/// TimeDelta::seconds(3600 + 60 + 1)
|
||||
/// );
|
||||
///
|
||||
/// // July 8 is 190th day in the year 2016
|
||||
/// let d0 = from_ymd(2016, 1, 1);
|
||||
/// assert_eq!(d.and_hms_milli_opt(0, 7, 6, 500).unwrap().signed_duration_since(d0.and_hms_opt(0, 0, 0).unwrap()),
|
||||
/// TimeDelta::seconds(189 * 86_400 + 7 * 60 + 6) + TimeDelta::milliseconds(500));
|
||||
/// assert_eq!(
|
||||
/// d.and_hms_milli_opt(0, 7, 6, 500)
|
||||
/// .unwrap()
|
||||
/// .signed_duration_since(d0.and_hms_opt(0, 0, 0).unwrap()),
|
||||
/// TimeDelta::seconds(189 * 86_400 + 7 * 60 + 6) + TimeDelta::milliseconds(500)
|
||||
/// );
|
||||
/// ```
|
||||
///
|
||||
/// Leap seconds are handled, but the subtraction assumes that
|
||||
@ -962,10 +1021,14 @@ impl NaiveDateTime {
|
||||
/// # use chrono::{TimeDelta, NaiveDate};
|
||||
/// # let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap();
|
||||
/// let leap = from_ymd(2015, 6, 30).and_hms_milli_opt(23, 59, 59, 1_500).unwrap();
|
||||
/// assert_eq!(leap.signed_duration_since(from_ymd(2015, 6, 30).and_hms_opt(23, 0, 0).unwrap()),
|
||||
/// TimeDelta::seconds(3600) + TimeDelta::milliseconds(500));
|
||||
/// assert_eq!(from_ymd(2015, 7, 1).and_hms_opt(1, 0, 0).unwrap().signed_duration_since(leap),
|
||||
/// TimeDelta::seconds(3600) - TimeDelta::milliseconds(500));
|
||||
/// assert_eq!(
|
||||
/// leap.signed_duration_since(from_ymd(2015, 6, 30).and_hms_opt(23, 0, 0).unwrap()),
|
||||
/// TimeDelta::seconds(3600) + TimeDelta::milliseconds(500)
|
||||
/// );
|
||||
/// assert_eq!(
|
||||
/// from_ymd(2015, 7, 1).and_hms_opt(1, 0, 0).unwrap().signed_duration_since(leap),
|
||||
/// TimeDelta::seconds(3600) - TimeDelta::milliseconds(500)
|
||||
/// );
|
||||
/// ```
|
||||
#[must_use]
|
||||
pub const fn signed_duration_since(self, rhs: NaiveDateTime) -> TimeDelta {
|
||||
@ -986,13 +1049,13 @@ impl NaiveDateTime {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::NaiveDate;
|
||||
/// use chrono::format::strftime::StrftimeItems;
|
||||
/// use chrono::NaiveDate;
|
||||
///
|
||||
/// let fmt = StrftimeItems::new("%Y-%m-%d %H:%M:%S");
|
||||
/// let dt = NaiveDate::from_ymd_opt(2015, 9, 5).unwrap().and_hms_opt(23, 56, 4).unwrap();
|
||||
/// assert_eq!(dt.format_with_items(fmt.clone()).to_string(), "2015-09-05 23:56:04");
|
||||
/// assert_eq!(dt.format("%Y-%m-%d %H:%M:%S").to_string(), "2015-09-05 23:56:04");
|
||||
/// assert_eq!(dt.format("%Y-%m-%d %H:%M:%S").to_string(), "2015-09-05 23:56:04");
|
||||
/// ```
|
||||
///
|
||||
/// The resulting `DelayedFormat` can be formatted directly via the `Display` trait.
|
||||
@ -1068,10 +1131,15 @@ impl NaiveDateTime {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{NaiveDate, FixedOffset};
|
||||
/// use chrono::{FixedOffset, NaiveDate};
|
||||
/// let hour = 3600;
|
||||
/// let tz = FixedOffset::east_opt(5 * hour).unwrap();
|
||||
/// let dt = NaiveDate::from_ymd_opt(2015, 9, 5).unwrap().and_hms_opt(23, 56, 4).unwrap().and_local_timezone(tz).unwrap();
|
||||
/// let dt = NaiveDate::from_ymd_opt(2015, 9, 5)
|
||||
/// .unwrap()
|
||||
/// .and_hms_opt(23, 56, 4)
|
||||
/// .unwrap()
|
||||
/// .and_local_timezone(tz)
|
||||
/// .unwrap();
|
||||
/// assert_eq!(dt.timezone(), tz);
|
||||
/// ```
|
||||
#[must_use]
|
||||
@ -1085,7 +1153,8 @@ impl NaiveDateTime {
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{NaiveDate, Utc};
|
||||
/// let dt = NaiveDate::from_ymd_opt(2023, 1, 30).unwrap().and_hms_opt(19, 32, 33).unwrap().and_utc();
|
||||
/// let dt =
|
||||
/// NaiveDate::from_ymd_opt(2023, 1, 30).unwrap().and_hms_opt(19, 32, 33).unwrap().and_utc();
|
||||
/// assert_eq!(dt.timezone(), Utc);
|
||||
/// ```
|
||||
#[must_use]
|
||||
@ -1128,9 +1197,10 @@ impl Datelike for NaiveDateTime {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{NaiveDate, NaiveDateTime, Datelike};
|
||||
/// use chrono::{Datelike, NaiveDate, NaiveDateTime};
|
||||
///
|
||||
/// let dt: NaiveDateTime = NaiveDate::from_ymd_opt(2015, 9, 25).unwrap().and_hms_opt(12, 34, 56).unwrap();
|
||||
/// let dt: NaiveDateTime =
|
||||
/// NaiveDate::from_ymd_opt(2015, 9, 25).unwrap().and_hms_opt(12, 34, 56).unwrap();
|
||||
/// assert_eq!(dt.year(), 2015);
|
||||
/// ```
|
||||
#[inline]
|
||||
@ -1147,9 +1217,10 @@ impl Datelike for NaiveDateTime {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{NaiveDate, NaiveDateTime, Datelike};
|
||||
/// use chrono::{Datelike, NaiveDate, NaiveDateTime};
|
||||
///
|
||||
/// let dt: NaiveDateTime = NaiveDate::from_ymd_opt(2015, 9, 25).unwrap().and_hms_opt(12, 34, 56).unwrap();
|
||||
/// let dt: NaiveDateTime =
|
||||
/// NaiveDate::from_ymd_opt(2015, 9, 25).unwrap().and_hms_opt(12, 34, 56).unwrap();
|
||||
/// assert_eq!(dt.month(), 9);
|
||||
/// ```
|
||||
#[inline]
|
||||
@ -1166,9 +1237,10 @@ impl Datelike for NaiveDateTime {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{NaiveDate, NaiveDateTime, Datelike};
|
||||
/// use chrono::{Datelike, NaiveDate, NaiveDateTime};
|
||||
///
|
||||
/// let dt: NaiveDateTime = NaiveDate::from_ymd_opt(2015, 9, 25).unwrap().and_hms_opt(12, 34, 56).unwrap();
|
||||
/// let dt: NaiveDateTime =
|
||||
/// NaiveDate::from_ymd_opt(2015, 9, 25).unwrap().and_hms_opt(12, 34, 56).unwrap();
|
||||
/// assert_eq!(dt.month0(), 8);
|
||||
/// ```
|
||||
#[inline]
|
||||
@ -1185,9 +1257,10 @@ impl Datelike for NaiveDateTime {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{NaiveDate, NaiveDateTime, Datelike};
|
||||
/// use chrono::{Datelike, NaiveDate, NaiveDateTime};
|
||||
///
|
||||
/// let dt: NaiveDateTime = NaiveDate::from_ymd_opt(2015, 9, 25).unwrap().and_hms_opt(12, 34, 56).unwrap();
|
||||
/// let dt: NaiveDateTime =
|
||||
/// NaiveDate::from_ymd_opt(2015, 9, 25).unwrap().and_hms_opt(12, 34, 56).unwrap();
|
||||
/// assert_eq!(dt.day(), 25);
|
||||
/// ```
|
||||
#[inline]
|
||||
@ -1204,9 +1277,10 @@ impl Datelike for NaiveDateTime {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{NaiveDate, NaiveDateTime, Datelike};
|
||||
/// use chrono::{Datelike, NaiveDate, NaiveDateTime};
|
||||
///
|
||||
/// let dt: NaiveDateTime = NaiveDate::from_ymd_opt(2015, 9, 25).unwrap().and_hms_opt(12, 34, 56).unwrap();
|
||||
/// let dt: NaiveDateTime =
|
||||
/// NaiveDate::from_ymd_opt(2015, 9, 25).unwrap().and_hms_opt(12, 34, 56).unwrap();
|
||||
/// assert_eq!(dt.day0(), 24);
|
||||
/// ```
|
||||
#[inline]
|
||||
@ -1223,9 +1297,10 @@ impl Datelike for NaiveDateTime {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{NaiveDate, NaiveDateTime, Datelike};
|
||||
/// use chrono::{Datelike, NaiveDate, NaiveDateTime};
|
||||
///
|
||||
/// let dt: NaiveDateTime = NaiveDate::from_ymd_opt(2015, 9, 25).unwrap().and_hms_opt(12, 34, 56).unwrap();
|
||||
/// let dt: NaiveDateTime =
|
||||
/// NaiveDate::from_ymd_opt(2015, 9, 25).unwrap().and_hms_opt(12, 34, 56).unwrap();
|
||||
/// assert_eq!(dt.ordinal(), 268);
|
||||
/// ```
|
||||
#[inline]
|
||||
@ -1242,9 +1317,10 @@ impl Datelike for NaiveDateTime {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{NaiveDate, NaiveDateTime, Datelike};
|
||||
/// use chrono::{Datelike, NaiveDate, NaiveDateTime};
|
||||
///
|
||||
/// let dt: NaiveDateTime = NaiveDate::from_ymd_opt(2015, 9, 25).unwrap().and_hms_opt(12, 34, 56).unwrap();
|
||||
/// let dt: NaiveDateTime =
|
||||
/// NaiveDate::from_ymd_opt(2015, 9, 25).unwrap().and_hms_opt(12, 34, 56).unwrap();
|
||||
/// assert_eq!(dt.ordinal0(), 267);
|
||||
/// ```
|
||||
#[inline]
|
||||
@ -1259,9 +1335,10 @@ impl Datelike for NaiveDateTime {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{NaiveDate, NaiveDateTime, Datelike, Weekday};
|
||||
/// use chrono::{Datelike, NaiveDate, NaiveDateTime, Weekday};
|
||||
///
|
||||
/// let dt: NaiveDateTime = NaiveDate::from_ymd_opt(2015, 9, 25).unwrap().and_hms_opt(12, 34, 56).unwrap();
|
||||
/// let dt: NaiveDateTime =
|
||||
/// NaiveDate::from_ymd_opt(2015, 9, 25).unwrap().and_hms_opt(12, 34, 56).unwrap();
|
||||
/// assert_eq!(dt.weekday(), Weekday::Fri);
|
||||
/// ```
|
||||
#[inline]
|
||||
@ -1287,11 +1364,18 @@ impl Datelike for NaiveDateTime {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{NaiveDate, NaiveDateTime, Datelike};
|
||||
/// use chrono::{Datelike, NaiveDate, NaiveDateTime};
|
||||
///
|
||||
/// let dt: NaiveDateTime = NaiveDate::from_ymd_opt(2015, 9, 25).unwrap().and_hms_opt(12, 34, 56).unwrap();
|
||||
/// assert_eq!(dt.with_year(2016), Some(NaiveDate::from_ymd_opt(2016, 9, 25).unwrap().and_hms_opt(12, 34, 56).unwrap()));
|
||||
/// assert_eq!(dt.with_year(-308), Some(NaiveDate::from_ymd_opt(-308, 9, 25).unwrap().and_hms_opt(12, 34, 56).unwrap()));
|
||||
/// let dt: NaiveDateTime =
|
||||
/// NaiveDate::from_ymd_opt(2015, 9, 25).unwrap().and_hms_opt(12, 34, 56).unwrap();
|
||||
/// assert_eq!(
|
||||
/// dt.with_year(2016),
|
||||
/// Some(NaiveDate::from_ymd_opt(2016, 9, 25).unwrap().and_hms_opt(12, 34, 56).unwrap())
|
||||
/// );
|
||||
/// assert_eq!(
|
||||
/// dt.with_year(-308),
|
||||
/// Some(NaiveDate::from_ymd_opt(-308, 9, 25).unwrap().and_hms_opt(12, 34, 56).unwrap())
|
||||
/// );
|
||||
/// ```
|
||||
#[inline]
|
||||
fn with_year(&self, year: i32) -> Option<NaiveDateTime> {
|
||||
@ -1309,10 +1393,14 @@ impl Datelike for NaiveDateTime {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{NaiveDate, NaiveDateTime, Datelike};
|
||||
/// use chrono::{Datelike, NaiveDate, NaiveDateTime};
|
||||
///
|
||||
/// let dt: NaiveDateTime = NaiveDate::from_ymd_opt(2015, 9, 30).unwrap().and_hms_opt(12, 34, 56).unwrap();
|
||||
/// assert_eq!(dt.with_month(10), Some(NaiveDate::from_ymd_opt(2015, 10, 30).unwrap().and_hms_opt(12, 34, 56).unwrap()));
|
||||
/// let dt: NaiveDateTime =
|
||||
/// NaiveDate::from_ymd_opt(2015, 9, 30).unwrap().and_hms_opt(12, 34, 56).unwrap();
|
||||
/// assert_eq!(
|
||||
/// dt.with_month(10),
|
||||
/// Some(NaiveDate::from_ymd_opt(2015, 10, 30).unwrap().and_hms_opt(12, 34, 56).unwrap())
|
||||
/// );
|
||||
/// assert_eq!(dt.with_month(13), None); // no month 13
|
||||
/// assert_eq!(dt.with_month(2), None); // no February 30
|
||||
/// ```
|
||||
@ -1333,10 +1421,14 @@ impl Datelike for NaiveDateTime {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{NaiveDate, NaiveDateTime, Datelike};
|
||||
/// use chrono::{Datelike, NaiveDate, NaiveDateTime};
|
||||
///
|
||||
/// let dt: NaiveDateTime = NaiveDate::from_ymd_opt(2015, 9, 30).unwrap().and_hms_opt(12, 34, 56).unwrap();
|
||||
/// assert_eq!(dt.with_month0(9), Some(NaiveDate::from_ymd_opt(2015, 10, 30).unwrap().and_hms_opt(12, 34, 56).unwrap()));
|
||||
/// let dt: NaiveDateTime =
|
||||
/// NaiveDate::from_ymd_opt(2015, 9, 30).unwrap().and_hms_opt(12, 34, 56).unwrap();
|
||||
/// assert_eq!(
|
||||
/// dt.with_month0(9),
|
||||
/// Some(NaiveDate::from_ymd_opt(2015, 10, 30).unwrap().and_hms_opt(12, 34, 56).unwrap())
|
||||
/// );
|
||||
/// assert_eq!(dt.with_month0(12), None); // no month 13
|
||||
/// assert_eq!(dt.with_month0(1), None); // no February 30
|
||||
/// ```
|
||||
@ -1356,10 +1448,14 @@ impl Datelike for NaiveDateTime {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{NaiveDate, NaiveDateTime, Datelike};
|
||||
/// use chrono::{Datelike, NaiveDate, NaiveDateTime};
|
||||
///
|
||||
/// let dt: NaiveDateTime = NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().and_hms_opt(12, 34, 56).unwrap();
|
||||
/// assert_eq!(dt.with_day(30), Some(NaiveDate::from_ymd_opt(2015, 9, 30).unwrap().and_hms_opt(12, 34, 56).unwrap()));
|
||||
/// let dt: NaiveDateTime =
|
||||
/// NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().and_hms_opt(12, 34, 56).unwrap();
|
||||
/// assert_eq!(
|
||||
/// dt.with_day(30),
|
||||
/// Some(NaiveDate::from_ymd_opt(2015, 9, 30).unwrap().and_hms_opt(12, 34, 56).unwrap())
|
||||
/// );
|
||||
/// assert_eq!(dt.with_day(31), None); // no September 31
|
||||
/// ```
|
||||
#[inline]
|
||||
@ -1378,10 +1474,14 @@ impl Datelike for NaiveDateTime {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{NaiveDate, NaiveDateTime, Datelike};
|
||||
/// use chrono::{Datelike, NaiveDate, NaiveDateTime};
|
||||
///
|
||||
/// let dt: NaiveDateTime = NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().and_hms_opt(12, 34, 56).unwrap();
|
||||
/// assert_eq!(dt.with_day0(29), Some(NaiveDate::from_ymd_opt(2015, 9, 30).unwrap().and_hms_opt(12, 34, 56).unwrap()));
|
||||
/// let dt: NaiveDateTime =
|
||||
/// NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().and_hms_opt(12, 34, 56).unwrap();
|
||||
/// assert_eq!(
|
||||
/// dt.with_day0(29),
|
||||
/// Some(NaiveDate::from_ymd_opt(2015, 9, 30).unwrap().and_hms_opt(12, 34, 56).unwrap())
|
||||
/// );
|
||||
/// assert_eq!(dt.with_day0(30), None); // no September 31
|
||||
/// ```
|
||||
#[inline]
|
||||
@ -1401,18 +1501,26 @@ impl Datelike for NaiveDateTime {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{NaiveDate, NaiveDateTime, Datelike};
|
||||
/// use chrono::{Datelike, NaiveDate, NaiveDateTime};
|
||||
///
|
||||
/// let dt: NaiveDateTime = NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().and_hms_opt(12, 34, 56).unwrap();
|
||||
/// assert_eq!(dt.with_ordinal(60),
|
||||
/// Some(NaiveDate::from_ymd_opt(2015, 3, 1).unwrap().and_hms_opt(12, 34, 56).unwrap()));
|
||||
/// let dt: NaiveDateTime =
|
||||
/// NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().and_hms_opt(12, 34, 56).unwrap();
|
||||
/// assert_eq!(
|
||||
/// dt.with_ordinal(60),
|
||||
/// Some(NaiveDate::from_ymd_opt(2015, 3, 1).unwrap().and_hms_opt(12, 34, 56).unwrap())
|
||||
/// );
|
||||
/// assert_eq!(dt.with_ordinal(366), None); // 2015 had only 365 days
|
||||
///
|
||||
/// let dt: NaiveDateTime = NaiveDate::from_ymd_opt(2016, 9, 8).unwrap().and_hms_opt(12, 34, 56).unwrap();
|
||||
/// assert_eq!(dt.with_ordinal(60),
|
||||
/// Some(NaiveDate::from_ymd_opt(2016, 2, 29).unwrap().and_hms_opt(12, 34, 56).unwrap()));
|
||||
/// assert_eq!(dt.with_ordinal(366),
|
||||
/// Some(NaiveDate::from_ymd_opt(2016, 12, 31).unwrap().and_hms_opt(12, 34, 56).unwrap()));
|
||||
/// let dt: NaiveDateTime =
|
||||
/// NaiveDate::from_ymd_opt(2016, 9, 8).unwrap().and_hms_opt(12, 34, 56).unwrap();
|
||||
/// assert_eq!(
|
||||
/// dt.with_ordinal(60),
|
||||
/// Some(NaiveDate::from_ymd_opt(2016, 2, 29).unwrap().and_hms_opt(12, 34, 56).unwrap())
|
||||
/// );
|
||||
/// assert_eq!(
|
||||
/// dt.with_ordinal(366),
|
||||
/// Some(NaiveDate::from_ymd_opt(2016, 12, 31).unwrap().and_hms_opt(12, 34, 56).unwrap())
|
||||
/// );
|
||||
/// ```
|
||||
#[inline]
|
||||
fn with_ordinal(&self, ordinal: u32) -> Option<NaiveDateTime> {
|
||||
@ -1431,18 +1539,26 @@ impl Datelike for NaiveDateTime {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{NaiveDate, NaiveDateTime, Datelike};
|
||||
/// use chrono::{Datelike, NaiveDate, NaiveDateTime};
|
||||
///
|
||||
/// let dt: NaiveDateTime = NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().and_hms_opt(12, 34, 56).unwrap();
|
||||
/// assert_eq!(dt.with_ordinal0(59),
|
||||
/// Some(NaiveDate::from_ymd_opt(2015, 3, 1).unwrap().and_hms_opt(12, 34, 56).unwrap()));
|
||||
/// let dt: NaiveDateTime =
|
||||
/// NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().and_hms_opt(12, 34, 56).unwrap();
|
||||
/// assert_eq!(
|
||||
/// dt.with_ordinal0(59),
|
||||
/// Some(NaiveDate::from_ymd_opt(2015, 3, 1).unwrap().and_hms_opt(12, 34, 56).unwrap())
|
||||
/// );
|
||||
/// assert_eq!(dt.with_ordinal0(365), None); // 2015 had only 365 days
|
||||
///
|
||||
/// let dt: NaiveDateTime = NaiveDate::from_ymd_opt(2016, 9, 8).unwrap().and_hms_opt(12, 34, 56).unwrap();
|
||||
/// assert_eq!(dt.with_ordinal0(59),
|
||||
/// Some(NaiveDate::from_ymd_opt(2016, 2, 29).unwrap().and_hms_opt(12, 34, 56).unwrap()));
|
||||
/// assert_eq!(dt.with_ordinal0(365),
|
||||
/// Some(NaiveDate::from_ymd_opt(2016, 12, 31).unwrap().and_hms_opt(12, 34, 56).unwrap()));
|
||||
/// let dt: NaiveDateTime =
|
||||
/// NaiveDate::from_ymd_opt(2016, 9, 8).unwrap().and_hms_opt(12, 34, 56).unwrap();
|
||||
/// assert_eq!(
|
||||
/// dt.with_ordinal0(59),
|
||||
/// Some(NaiveDate::from_ymd_opt(2016, 2, 29).unwrap().and_hms_opt(12, 34, 56).unwrap())
|
||||
/// );
|
||||
/// assert_eq!(
|
||||
/// dt.with_ordinal0(365),
|
||||
/// Some(NaiveDate::from_ymd_opt(2016, 12, 31).unwrap().and_hms_opt(12, 34, 56).unwrap())
|
||||
/// );
|
||||
/// ```
|
||||
#[inline]
|
||||
fn with_ordinal0(&self, ordinal0: u32) -> Option<NaiveDateTime> {
|
||||
@ -1460,7 +1576,8 @@ impl Timelike for NaiveDateTime {
|
||||
/// ```
|
||||
/// use chrono::{NaiveDate, NaiveDateTime, Timelike};
|
||||
///
|
||||
/// let dt: NaiveDateTime = NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().and_hms_milli_opt(12, 34, 56, 789).unwrap();
|
||||
/// let dt: NaiveDateTime =
|
||||
/// NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().and_hms_milli_opt(12, 34, 56, 789).unwrap();
|
||||
/// assert_eq!(dt.hour(), 12);
|
||||
/// ```
|
||||
#[inline]
|
||||
@ -1477,7 +1594,8 @@ impl Timelike for NaiveDateTime {
|
||||
/// ```
|
||||
/// use chrono::{NaiveDate, NaiveDateTime, Timelike};
|
||||
///
|
||||
/// let dt: NaiveDateTime = NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().and_hms_milli_opt(12, 34, 56, 789).unwrap();
|
||||
/// let dt: NaiveDateTime =
|
||||
/// NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().and_hms_milli_opt(12, 34, 56, 789).unwrap();
|
||||
/// assert_eq!(dt.minute(), 34);
|
||||
/// ```
|
||||
#[inline]
|
||||
@ -1494,7 +1612,8 @@ impl Timelike for NaiveDateTime {
|
||||
/// ```
|
||||
/// use chrono::{NaiveDate, NaiveDateTime, Timelike};
|
||||
///
|
||||
/// let dt: NaiveDateTime = NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().and_hms_milli_opt(12, 34, 56, 789).unwrap();
|
||||
/// let dt: NaiveDateTime =
|
||||
/// NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().and_hms_milli_opt(12, 34, 56, 789).unwrap();
|
||||
/// assert_eq!(dt.second(), 56);
|
||||
/// ```
|
||||
#[inline]
|
||||
@ -1513,7 +1632,8 @@ impl Timelike for NaiveDateTime {
|
||||
/// ```
|
||||
/// use chrono::{NaiveDate, NaiveDateTime, Timelike};
|
||||
///
|
||||
/// let dt: NaiveDateTime = NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().and_hms_milli_opt(12, 34, 56, 789).unwrap();
|
||||
/// let dt: NaiveDateTime =
|
||||
/// NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().and_hms_milli_opt(12, 34, 56, 789).unwrap();
|
||||
/// assert_eq!(dt.nanosecond(), 789_000_000);
|
||||
/// ```
|
||||
#[inline]
|
||||
@ -1534,9 +1654,14 @@ impl Timelike for NaiveDateTime {
|
||||
/// ```
|
||||
/// use chrono::{NaiveDate, NaiveDateTime, Timelike};
|
||||
///
|
||||
/// let dt: NaiveDateTime = NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().and_hms_milli_opt(12, 34, 56, 789).unwrap();
|
||||
/// assert_eq!(dt.with_hour(7),
|
||||
/// Some(NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().and_hms_milli_opt(7, 34, 56, 789).unwrap()));
|
||||
/// let dt: NaiveDateTime =
|
||||
/// NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().and_hms_milli_opt(12, 34, 56, 789).unwrap();
|
||||
/// assert_eq!(
|
||||
/// dt.with_hour(7),
|
||||
/// Some(
|
||||
/// NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().and_hms_milli_opt(7, 34, 56, 789).unwrap()
|
||||
/// )
|
||||
/// );
|
||||
/// assert_eq!(dt.with_hour(24), None);
|
||||
/// ```
|
||||
#[inline]
|
||||
@ -1557,9 +1682,17 @@ impl Timelike for NaiveDateTime {
|
||||
/// ```
|
||||
/// use chrono::{NaiveDate, NaiveDateTime, Timelike};
|
||||
///
|
||||
/// let dt: NaiveDateTime = NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().and_hms_milli_opt(12, 34, 56, 789).unwrap();
|
||||
/// assert_eq!(dt.with_minute(45),
|
||||
/// Some(NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().and_hms_milli_opt(12, 45, 56, 789).unwrap()));
|
||||
/// let dt: NaiveDateTime =
|
||||
/// NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().and_hms_milli_opt(12, 34, 56, 789).unwrap();
|
||||
/// assert_eq!(
|
||||
/// dt.with_minute(45),
|
||||
/// Some(
|
||||
/// NaiveDate::from_ymd_opt(2015, 9, 8)
|
||||
/// .unwrap()
|
||||
/// .and_hms_milli_opt(12, 45, 56, 789)
|
||||
/// .unwrap()
|
||||
/// )
|
||||
/// );
|
||||
/// assert_eq!(dt.with_minute(60), None);
|
||||
/// ```
|
||||
#[inline]
|
||||
@ -1583,9 +1716,17 @@ impl Timelike for NaiveDateTime {
|
||||
/// ```
|
||||
/// use chrono::{NaiveDate, NaiveDateTime, Timelike};
|
||||
///
|
||||
/// let dt: NaiveDateTime = NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().and_hms_milli_opt(12, 34, 56, 789).unwrap();
|
||||
/// assert_eq!(dt.with_second(17),
|
||||
/// Some(NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().and_hms_milli_opt(12, 34, 17, 789).unwrap()));
|
||||
/// let dt: NaiveDateTime =
|
||||
/// NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().and_hms_milli_opt(12, 34, 56, 789).unwrap();
|
||||
/// assert_eq!(
|
||||
/// dt.with_second(17),
|
||||
/// Some(
|
||||
/// NaiveDate::from_ymd_opt(2015, 9, 8)
|
||||
/// .unwrap()
|
||||
/// .and_hms_milli_opt(12, 34, 17, 789)
|
||||
/// .unwrap()
|
||||
/// )
|
||||
/// );
|
||||
/// assert_eq!(dt.with_second(60), None);
|
||||
/// ```
|
||||
#[inline]
|
||||
@ -1610,11 +1751,26 @@ impl Timelike for NaiveDateTime {
|
||||
/// ```
|
||||
/// use chrono::{NaiveDate, NaiveDateTime, Timelike};
|
||||
///
|
||||
/// let dt: NaiveDateTime = NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().and_hms_milli_opt(12, 34, 59, 789).unwrap();
|
||||
/// assert_eq!(dt.with_nanosecond(333_333_333),
|
||||
/// Some(NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().and_hms_nano_opt(12, 34, 59, 333_333_333).unwrap()));
|
||||
/// assert_eq!(dt.with_nanosecond(1_333_333_333), // leap second
|
||||
/// Some(NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().and_hms_nano_opt(12, 34, 59, 1_333_333_333).unwrap()));
|
||||
/// let dt: NaiveDateTime =
|
||||
/// NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().and_hms_milli_opt(12, 34, 59, 789).unwrap();
|
||||
/// assert_eq!(
|
||||
/// dt.with_nanosecond(333_333_333),
|
||||
/// Some(
|
||||
/// NaiveDate::from_ymd_opt(2015, 9, 8)
|
||||
/// .unwrap()
|
||||
/// .and_hms_nano_opt(12, 34, 59, 333_333_333)
|
||||
/// .unwrap()
|
||||
/// )
|
||||
/// );
|
||||
/// assert_eq!(
|
||||
/// dt.with_nanosecond(1_333_333_333), // leap second
|
||||
/// Some(
|
||||
/// NaiveDate::from_ymd_opt(2015, 9, 8)
|
||||
/// .unwrap()
|
||||
/// .and_hms_nano_opt(12, 34, 59, 1_333_333_333)
|
||||
/// .unwrap()
|
||||
/// )
|
||||
/// );
|
||||
/// assert_eq!(dt.with_nanosecond(2_000_000_000), None);
|
||||
/// ```
|
||||
#[inline]
|
||||
@ -1637,20 +1793,24 @@ impl Timelike for NaiveDateTime {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{TimeDelta, NaiveDate};
|
||||
/// use chrono::{NaiveDate, TimeDelta};
|
||||
///
|
||||
/// let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap();
|
||||
///
|
||||
/// let d = from_ymd(2016, 7, 8);
|
||||
/// let hms = |h, m, s| d.and_hms_opt(h, m, s).unwrap();
|
||||
/// assert_eq!(hms(3, 5, 7) + TimeDelta::zero(), hms(3, 5, 7));
|
||||
/// assert_eq!(hms(3, 5, 7) + TimeDelta::seconds(1), hms(3, 5, 8));
|
||||
/// assert_eq!(hms(3, 5, 7) + TimeDelta::seconds(-1), hms(3, 5, 6));
|
||||
/// assert_eq!(hms(3, 5, 7) + TimeDelta::zero(), hms(3, 5, 7));
|
||||
/// assert_eq!(hms(3, 5, 7) + TimeDelta::seconds(1), hms(3, 5, 8));
|
||||
/// assert_eq!(hms(3, 5, 7) + TimeDelta::seconds(-1), hms(3, 5, 6));
|
||||
/// assert_eq!(hms(3, 5, 7) + TimeDelta::seconds(3600 + 60), hms(4, 6, 7));
|
||||
/// assert_eq!(hms(3, 5, 7) + TimeDelta::seconds(86_400),
|
||||
/// from_ymd(2016, 7, 9).and_hms_opt(3, 5, 7).unwrap());
|
||||
/// assert_eq!(hms(3, 5, 7) + TimeDelta::days(365),
|
||||
/// from_ymd(2017, 7, 8).and_hms_opt(3, 5, 7).unwrap());
|
||||
/// assert_eq!(
|
||||
/// hms(3, 5, 7) + TimeDelta::seconds(86_400),
|
||||
/// from_ymd(2016, 7, 9).and_hms_opt(3, 5, 7).unwrap()
|
||||
/// );
|
||||
/// assert_eq!(
|
||||
/// hms(3, 5, 7) + TimeDelta::days(365),
|
||||
/// from_ymd(2017, 7, 8).and_hms_opt(3, 5, 7).unwrap()
|
||||
/// );
|
||||
///
|
||||
/// let hmsm = |h, m, s, milli| d.and_hms_milli_opt(h, m, s, milli).unwrap();
|
||||
/// assert_eq!(hmsm(3, 5, 7, 980) + TimeDelta::milliseconds(450), hmsm(3, 5, 8, 430));
|
||||
@ -1774,23 +1934,28 @@ impl Add<FixedOffset> for NaiveDateTime {
|
||||
/// NaiveDate::from_ymd_opt(2014, 2, 1).unwrap().and_hms_opt(1, 0, 0).unwrap()
|
||||
/// );
|
||||
/// assert_eq!(
|
||||
/// NaiveDate::from_ymd_opt(2014, 1, 1).unwrap().and_hms_opt(0, 2, 0).unwrap() + Months::new(11),
|
||||
/// NaiveDate::from_ymd_opt(2014, 1, 1).unwrap().and_hms_opt(0, 2, 0).unwrap()
|
||||
/// + Months::new(11),
|
||||
/// NaiveDate::from_ymd_opt(2014, 12, 1).unwrap().and_hms_opt(0, 2, 0).unwrap()
|
||||
/// );
|
||||
/// assert_eq!(
|
||||
/// NaiveDate::from_ymd_opt(2014, 1, 1).unwrap().and_hms_opt(0, 0, 3).unwrap() + Months::new(12),
|
||||
/// NaiveDate::from_ymd_opt(2014, 1, 1).unwrap().and_hms_opt(0, 0, 3).unwrap()
|
||||
/// + Months::new(12),
|
||||
/// NaiveDate::from_ymd_opt(2015, 1, 1).unwrap().and_hms_opt(0, 0, 3).unwrap()
|
||||
/// );
|
||||
/// assert_eq!(
|
||||
/// NaiveDate::from_ymd_opt(2014, 1, 1).unwrap().and_hms_opt(0, 0, 4).unwrap() + Months::new(13),
|
||||
/// NaiveDate::from_ymd_opt(2014, 1, 1).unwrap().and_hms_opt(0, 0, 4).unwrap()
|
||||
/// + Months::new(13),
|
||||
/// NaiveDate::from_ymd_opt(2015, 2, 1).unwrap().and_hms_opt(0, 0, 4).unwrap()
|
||||
/// );
|
||||
/// assert_eq!(
|
||||
/// NaiveDate::from_ymd_opt(2014, 1, 31).unwrap().and_hms_opt(0, 5, 0).unwrap() + Months::new(1),
|
||||
/// NaiveDate::from_ymd_opt(2014, 1, 31).unwrap().and_hms_opt(0, 5, 0).unwrap()
|
||||
/// + Months::new(1),
|
||||
/// NaiveDate::from_ymd_opt(2014, 2, 28).unwrap().and_hms_opt(0, 5, 0).unwrap()
|
||||
/// );
|
||||
/// assert_eq!(
|
||||
/// NaiveDate::from_ymd_opt(2020, 1, 31).unwrap().and_hms_opt(6, 0, 0).unwrap() + Months::new(1),
|
||||
/// NaiveDate::from_ymd_opt(2020, 1, 31).unwrap().and_hms_opt(6, 0, 0).unwrap()
|
||||
/// + Months::new(1),
|
||||
/// NaiveDate::from_ymd_opt(2020, 2, 29).unwrap().and_hms_opt(6, 0, 0).unwrap()
|
||||
/// );
|
||||
/// ```
|
||||
@ -1818,20 +1983,24 @@ impl Add<Months> for NaiveDateTime {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{TimeDelta, NaiveDate};
|
||||
/// use chrono::{NaiveDate, TimeDelta};
|
||||
///
|
||||
/// let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap();
|
||||
///
|
||||
/// let d = from_ymd(2016, 7, 8);
|
||||
/// let hms = |h, m, s| d.and_hms_opt(h, m, s).unwrap();
|
||||
/// assert_eq!(hms(3, 5, 7) - TimeDelta::zero(), hms(3, 5, 7));
|
||||
/// assert_eq!(hms(3, 5, 7) - TimeDelta::seconds(1), hms(3, 5, 6));
|
||||
/// assert_eq!(hms(3, 5, 7) - TimeDelta::seconds(-1), hms(3, 5, 8));
|
||||
/// assert_eq!(hms(3, 5, 7) - TimeDelta::zero(), hms(3, 5, 7));
|
||||
/// assert_eq!(hms(3, 5, 7) - TimeDelta::seconds(1), hms(3, 5, 6));
|
||||
/// assert_eq!(hms(3, 5, 7) - TimeDelta::seconds(-1), hms(3, 5, 8));
|
||||
/// assert_eq!(hms(3, 5, 7) - TimeDelta::seconds(3600 + 60), hms(2, 4, 7));
|
||||
/// assert_eq!(hms(3, 5, 7) - TimeDelta::seconds(86_400),
|
||||
/// from_ymd(2016, 7, 7).and_hms_opt(3, 5, 7).unwrap());
|
||||
/// assert_eq!(hms(3, 5, 7) - TimeDelta::days(365),
|
||||
/// from_ymd(2015, 7, 9).and_hms_opt(3, 5, 7).unwrap());
|
||||
/// assert_eq!(
|
||||
/// hms(3, 5, 7) - TimeDelta::seconds(86_400),
|
||||
/// from_ymd(2016, 7, 7).and_hms_opt(3, 5, 7).unwrap()
|
||||
/// );
|
||||
/// assert_eq!(
|
||||
/// hms(3, 5, 7) - TimeDelta::days(365),
|
||||
/// from_ymd(2015, 7, 9).and_hms_opt(3, 5, 7).unwrap()
|
||||
/// );
|
||||
///
|
||||
/// let hmsm = |h, m, s, milli| d.and_hms_milli_opt(h, m, s, milli).unwrap();
|
||||
/// assert_eq!(hmsm(3, 5, 7, 450) - TimeDelta::milliseconds(670), hmsm(3, 5, 6, 780));
|
||||
@ -1951,15 +2120,18 @@ impl Sub<FixedOffset> for NaiveDateTime {
|
||||
/// use chrono::{Months, NaiveDate};
|
||||
///
|
||||
/// assert_eq!(
|
||||
/// NaiveDate::from_ymd_opt(2014, 01, 01).unwrap().and_hms_opt(01, 00, 00).unwrap() - Months::new(11),
|
||||
/// NaiveDate::from_ymd_opt(2014, 01, 01).unwrap().and_hms_opt(01, 00, 00).unwrap()
|
||||
/// - Months::new(11),
|
||||
/// NaiveDate::from_ymd_opt(2013, 02, 01).unwrap().and_hms_opt(01, 00, 00).unwrap()
|
||||
/// );
|
||||
/// assert_eq!(
|
||||
/// NaiveDate::from_ymd_opt(2014, 01, 01).unwrap().and_hms_opt(00, 02, 00).unwrap() - Months::new(12),
|
||||
/// NaiveDate::from_ymd_opt(2014, 01, 01).unwrap().and_hms_opt(00, 02, 00).unwrap()
|
||||
/// - Months::new(12),
|
||||
/// NaiveDate::from_ymd_opt(2013, 01, 01).unwrap().and_hms_opt(00, 02, 00).unwrap()
|
||||
/// );
|
||||
/// assert_eq!(
|
||||
/// NaiveDate::from_ymd_opt(2014, 01, 01).unwrap().and_hms_opt(00, 00, 03).unwrap() - Months::new(13),
|
||||
/// NaiveDate::from_ymd_opt(2014, 01, 01).unwrap().and_hms_opt(00, 00, 03).unwrap()
|
||||
/// - Months::new(13),
|
||||
/// NaiveDate::from_ymd_opt(2012, 12, 01).unwrap().and_hms_opt(00, 00, 03).unwrap()
|
||||
/// );
|
||||
/// ```
|
||||
@ -1985,17 +2157,22 @@ impl Sub<Months> for NaiveDateTime {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{TimeDelta, NaiveDate};
|
||||
/// use chrono::{NaiveDate, TimeDelta};
|
||||
///
|
||||
/// let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap();
|
||||
///
|
||||
/// let d = from_ymd(2016, 7, 8);
|
||||
/// assert_eq!(d.and_hms_opt(3, 5, 7).unwrap() - d.and_hms_opt(2, 4, 6).unwrap(), TimeDelta::seconds(3600 + 60 + 1));
|
||||
/// assert_eq!(
|
||||
/// d.and_hms_opt(3, 5, 7).unwrap() - d.and_hms_opt(2, 4, 6).unwrap(),
|
||||
/// TimeDelta::seconds(3600 + 60 + 1)
|
||||
/// );
|
||||
///
|
||||
/// // July 8 is 190th day in the year 2016
|
||||
/// let d0 = from_ymd(2016, 1, 1);
|
||||
/// assert_eq!(d.and_hms_milli_opt(0, 7, 6, 500).unwrap() - d0.and_hms_opt(0, 0, 0).unwrap(),
|
||||
/// TimeDelta::seconds(189 * 86_400 + 7 * 60 + 6) + TimeDelta::milliseconds(500));
|
||||
/// assert_eq!(
|
||||
/// d.and_hms_milli_opt(0, 7, 6, 500).unwrap() - d0.and_hms_opt(0, 0, 0).unwrap(),
|
||||
/// TimeDelta::seconds(189 * 86_400 + 7 * 60 + 6) + TimeDelta::milliseconds(500)
|
||||
/// );
|
||||
/// ```
|
||||
///
|
||||
/// Leap seconds are handled, but the subtraction assumes that no other leap
|
||||
@ -2005,10 +2182,14 @@ impl Sub<Months> for NaiveDateTime {
|
||||
/// # use chrono::{TimeDelta, NaiveDate};
|
||||
/// # let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap();
|
||||
/// let leap = from_ymd(2015, 6, 30).and_hms_milli_opt(23, 59, 59, 1_500).unwrap();
|
||||
/// assert_eq!(leap - from_ymd(2015, 6, 30).and_hms_opt(23, 0, 0).unwrap(),
|
||||
/// TimeDelta::seconds(3600) + TimeDelta::milliseconds(500));
|
||||
/// assert_eq!(from_ymd(2015, 7, 1).and_hms_opt(1, 0, 0).unwrap() - leap,
|
||||
/// TimeDelta::seconds(3600) - TimeDelta::milliseconds(500));
|
||||
/// assert_eq!(
|
||||
/// leap - from_ymd(2015, 6, 30).and_hms_opt(23, 0, 0).unwrap(),
|
||||
/// TimeDelta::seconds(3600) + TimeDelta::milliseconds(500)
|
||||
/// );
|
||||
/// assert_eq!(
|
||||
/// from_ymd(2015, 7, 1).and_hms_opt(1, 0, 0).unwrap() - leap,
|
||||
/// TimeDelta::seconds(3600) - TimeDelta::milliseconds(500)
|
||||
/// );
|
||||
/// ```
|
||||
impl Sub<NaiveDateTime> for NaiveDateTime {
|
||||
type Output = TimeDelta;
|
||||
@ -2071,7 +2252,8 @@ impl Sub<Days> for NaiveDateTime {
|
||||
///
|
||||
/// ```
|
||||
/// # use chrono::NaiveDate;
|
||||
/// let dt = NaiveDate::from_ymd_opt(2015, 6, 30).unwrap().and_hms_milli_opt(23, 59, 59, 1_500).unwrap();
|
||||
/// let dt =
|
||||
/// NaiveDate::from_ymd_opt(2015, 6, 30).unwrap().and_hms_milli_opt(23, 59, 59, 1_500).unwrap();
|
||||
/// assert_eq!(format!("{:?}", dt), "2015-06-30T23:59:60.500");
|
||||
/// ```
|
||||
impl fmt::Debug for NaiveDateTime {
|
||||
@ -2104,7 +2286,8 @@ impl fmt::Debug for NaiveDateTime {
|
||||
///
|
||||
/// ```
|
||||
/// # use chrono::NaiveDate;
|
||||
/// let dt = NaiveDate::from_ymd_opt(2015, 6, 30).unwrap().and_hms_milli_opt(23, 59, 59, 1_500).unwrap();
|
||||
/// let dt =
|
||||
/// NaiveDate::from_ymd_opt(2015, 6, 30).unwrap().and_hms_milli_opt(23, 59, 59, 1_500).unwrap();
|
||||
/// assert_eq!(format!("{}", dt), "2015-06-30 23:59:60.500");
|
||||
/// ```
|
||||
impl fmt::Display for NaiveDateTime {
|
||||
|
@ -64,13 +64,14 @@ impl<'de> de::Deserialize<'de> for NaiveDateTime {
|
||||
/// #[derive(Deserialize, Serialize)]
|
||||
/// struct S {
|
||||
/// #[serde(with = "ts_nanoseconds")]
|
||||
/// time: NaiveDateTime
|
||||
/// time: NaiveDateTime,
|
||||
/// }
|
||||
///
|
||||
/// let time = NaiveDate::from_ymd_opt(2018, 5, 17).unwrap().and_hms_nano_opt(02, 04, 59, 918355733).unwrap();
|
||||
/// let my_s = S {
|
||||
/// time: time.clone(),
|
||||
/// };
|
||||
/// let time = NaiveDate::from_ymd_opt(2018, 5, 17)
|
||||
/// .unwrap()
|
||||
/// .and_hms_nano_opt(02, 04, 59, 918355733)
|
||||
/// .unwrap();
|
||||
/// let my_s = S { time: time.clone() };
|
||||
///
|
||||
/// let as_string = serde_json::to_string(&my_s)?;
|
||||
/// assert_eq!(as_string, r#"{"time":1526522699918355733}"#);
|
||||
@ -106,11 +107,14 @@ pub mod ts_nanoseconds {
|
||||
/// #[derive(Serialize)]
|
||||
/// struct S {
|
||||
/// #[serde(serialize_with = "to_nano_ts")]
|
||||
/// time: NaiveDateTime
|
||||
/// time: NaiveDateTime,
|
||||
/// }
|
||||
///
|
||||
/// let my_s = S {
|
||||
/// time: NaiveDate::from_ymd_opt(2018, 5, 17).unwrap().and_hms_nano_opt(02, 04, 59, 918355733).unwrap(),
|
||||
/// time: NaiveDate::from_ymd_opt(2018, 5, 17)
|
||||
/// .unwrap()
|
||||
/// .and_hms_nano_opt(02, 04, 59, 918355733)
|
||||
/// .unwrap(),
|
||||
/// };
|
||||
/// let as_string = serde_json::to_string(&my_s)?;
|
||||
/// assert_eq!(as_string, r#"{"time":1526522699918355733}"#);
|
||||
@ -138,7 +142,7 @@ pub mod ts_nanoseconds {
|
||||
/// #[derive(Debug, PartialEq, Deserialize)]
|
||||
/// struct S {
|
||||
/// #[serde(deserialize_with = "from_nano_ts")]
|
||||
/// time: NaiveDateTime
|
||||
/// time: NaiveDateTime,
|
||||
/// }
|
||||
///
|
||||
/// let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918355733 }"#)?;
|
||||
@ -201,13 +205,16 @@ pub mod ts_nanoseconds {
|
||||
/// #[derive(Deserialize, Serialize)]
|
||||
/// struct S {
|
||||
/// #[serde(with = "ts_nanoseconds_option")]
|
||||
/// time: Option<NaiveDateTime>
|
||||
/// time: Option<NaiveDateTime>,
|
||||
/// }
|
||||
///
|
||||
/// let time = Some(NaiveDate::from_ymd_opt(2018, 5, 17).unwrap().and_hms_nano_opt(02, 04, 59, 918355733).unwrap());
|
||||
/// let my_s = S {
|
||||
/// time: time.clone(),
|
||||
/// };
|
||||
/// let time = Some(
|
||||
/// NaiveDate::from_ymd_opt(2018, 5, 17)
|
||||
/// .unwrap()
|
||||
/// .and_hms_nano_opt(02, 04, 59, 918355733)
|
||||
/// .unwrap(),
|
||||
/// );
|
||||
/// let my_s = S { time: time.clone() };
|
||||
///
|
||||
/// let as_string = serde_json::to_string(&my_s)?;
|
||||
/// assert_eq!(as_string, r#"{"time":1526522699918355733}"#);
|
||||
@ -243,11 +250,16 @@ pub mod ts_nanoseconds_option {
|
||||
/// #[derive(Serialize)]
|
||||
/// struct S {
|
||||
/// #[serde(serialize_with = "to_nano_tsopt")]
|
||||
/// time: Option<NaiveDateTime>
|
||||
/// time: Option<NaiveDateTime>,
|
||||
/// }
|
||||
///
|
||||
/// let my_s = S {
|
||||
/// time: Some(NaiveDate::from_ymd_opt(2018, 5, 17).unwrap().and_hms_nano_opt(02, 04, 59, 918355733).unwrap()),
|
||||
/// time: Some(
|
||||
/// NaiveDate::from_ymd_opt(2018, 5, 17)
|
||||
/// .unwrap()
|
||||
/// .and_hms_nano_opt(02, 04, 59, 918355733)
|
||||
/// .unwrap(),
|
||||
/// ),
|
||||
/// };
|
||||
/// let as_string = serde_json::to_string(&my_s)?;
|
||||
/// assert_eq!(as_string, r#"{"time":1526522699918355733}"#);
|
||||
@ -278,7 +290,7 @@ pub mod ts_nanoseconds_option {
|
||||
/// #[derive(Debug, PartialEq, Deserialize)]
|
||||
/// struct S {
|
||||
/// #[serde(deserialize_with = "from_nano_tsopt")]
|
||||
/// time: Option<NaiveDateTime>
|
||||
/// time: Option<NaiveDateTime>,
|
||||
/// }
|
||||
///
|
||||
/// let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918355733 }"#)?;
|
||||
@ -341,13 +353,14 @@ pub mod ts_nanoseconds_option {
|
||||
/// #[derive(Deserialize, Serialize)]
|
||||
/// struct S {
|
||||
/// #[serde(with = "ts_microseconds")]
|
||||
/// time: NaiveDateTime
|
||||
/// time: NaiveDateTime,
|
||||
/// }
|
||||
///
|
||||
/// let time = NaiveDate::from_ymd_opt(2018, 5, 17).unwrap().and_hms_micro_opt(02, 04, 59, 918355).unwrap();
|
||||
/// let my_s = S {
|
||||
/// time: time.clone(),
|
||||
/// };
|
||||
/// let time = NaiveDate::from_ymd_opt(2018, 5, 17)
|
||||
/// .unwrap()
|
||||
/// .and_hms_micro_opt(02, 04, 59, 918355)
|
||||
/// .unwrap();
|
||||
/// let my_s = S { time: time.clone() };
|
||||
///
|
||||
/// let as_string = serde_json::to_string(&my_s)?;
|
||||
/// assert_eq!(as_string, r#"{"time":1526522699918355}"#);
|
||||
@ -375,11 +388,14 @@ pub mod ts_microseconds {
|
||||
/// #[derive(Serialize)]
|
||||
/// struct S {
|
||||
/// #[serde(serialize_with = "to_micro_ts")]
|
||||
/// time: NaiveDateTime
|
||||
/// time: NaiveDateTime,
|
||||
/// }
|
||||
///
|
||||
/// let my_s = S {
|
||||
/// time: NaiveDate::from_ymd_opt(2018, 5, 17).unwrap().and_hms_micro_opt(02, 04, 59, 918355).unwrap(),
|
||||
/// time: NaiveDate::from_ymd_opt(2018, 5, 17)
|
||||
/// .unwrap()
|
||||
/// .and_hms_micro_opt(02, 04, 59, 918355)
|
||||
/// .unwrap(),
|
||||
/// };
|
||||
/// let as_string = serde_json::to_string(&my_s)?;
|
||||
/// assert_eq!(as_string, r#"{"time":1526522699918355}"#);
|
||||
@ -405,7 +421,7 @@ pub mod ts_microseconds {
|
||||
/// #[derive(Debug, PartialEq, Deserialize)]
|
||||
/// struct S {
|
||||
/// #[serde(deserialize_with = "from_micro_ts")]
|
||||
/// time: NaiveDateTime
|
||||
/// time: NaiveDateTime,
|
||||
/// }
|
||||
///
|
||||
/// let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918355 }"#)?;
|
||||
@ -465,13 +481,16 @@ pub mod ts_microseconds {
|
||||
/// #[derive(Deserialize, Serialize)]
|
||||
/// struct S {
|
||||
/// #[serde(with = "ts_microseconds_option")]
|
||||
/// time: Option<NaiveDateTime>
|
||||
/// time: Option<NaiveDateTime>,
|
||||
/// }
|
||||
///
|
||||
/// let time = Some(NaiveDate::from_ymd_opt(2018, 5, 17).unwrap().and_hms_micro_opt(02, 04, 59, 918355).unwrap());
|
||||
/// let my_s = S {
|
||||
/// time: time.clone(),
|
||||
/// };
|
||||
/// let time = Some(
|
||||
/// NaiveDate::from_ymd_opt(2018, 5, 17)
|
||||
/// .unwrap()
|
||||
/// .and_hms_micro_opt(02, 04, 59, 918355)
|
||||
/// .unwrap(),
|
||||
/// );
|
||||
/// let my_s = S { time: time.clone() };
|
||||
///
|
||||
/// let as_string = serde_json::to_string(&my_s)?;
|
||||
/// assert_eq!(as_string, r#"{"time":1526522699918355}"#);
|
||||
@ -499,11 +518,16 @@ pub mod ts_microseconds_option {
|
||||
/// #[derive(Serialize)]
|
||||
/// struct S {
|
||||
/// #[serde(serialize_with = "to_micro_tsopt")]
|
||||
/// time: Option<NaiveDateTime>
|
||||
/// time: Option<NaiveDateTime>,
|
||||
/// }
|
||||
///
|
||||
/// let my_s = S {
|
||||
/// time: Some(NaiveDate::from_ymd_opt(2018, 5, 17).unwrap().and_hms_micro_opt(02, 04, 59, 918355).unwrap()),
|
||||
/// time: Some(
|
||||
/// NaiveDate::from_ymd_opt(2018, 5, 17)
|
||||
/// .unwrap()
|
||||
/// .and_hms_micro_opt(02, 04, 59, 918355)
|
||||
/// .unwrap(),
|
||||
/// ),
|
||||
/// };
|
||||
/// let as_string = serde_json::to_string(&my_s)?;
|
||||
/// assert_eq!(as_string, r#"{"time":1526522699918355}"#);
|
||||
@ -532,7 +556,7 @@ pub mod ts_microseconds_option {
|
||||
/// #[derive(Debug, PartialEq, Deserialize)]
|
||||
/// struct S {
|
||||
/// #[serde(deserialize_with = "from_micro_tsopt")]
|
||||
/// time: Option<NaiveDateTime>
|
||||
/// time: Option<NaiveDateTime>,
|
||||
/// }
|
||||
///
|
||||
/// let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918355 }"#)?;
|
||||
@ -595,13 +619,12 @@ pub mod ts_microseconds_option {
|
||||
/// #[derive(Deserialize, Serialize)]
|
||||
/// struct S {
|
||||
/// #[serde(with = "ts_milliseconds")]
|
||||
/// time: NaiveDateTime
|
||||
/// time: NaiveDateTime,
|
||||
/// }
|
||||
///
|
||||
/// let time = NaiveDate::from_ymd_opt(2018, 5, 17).unwrap().and_hms_milli_opt(02, 04, 59, 918).unwrap();
|
||||
/// let my_s = S {
|
||||
/// time: time.clone(),
|
||||
/// };
|
||||
/// let time =
|
||||
/// NaiveDate::from_ymd_opt(2018, 5, 17).unwrap().and_hms_milli_opt(02, 04, 59, 918).unwrap();
|
||||
/// let my_s = S { time: time.clone() };
|
||||
///
|
||||
/// let as_string = serde_json::to_string(&my_s)?;
|
||||
/// assert_eq!(as_string, r#"{"time":1526522699918}"#);
|
||||
@ -629,11 +652,14 @@ pub mod ts_milliseconds {
|
||||
/// #[derive(Serialize)]
|
||||
/// struct S {
|
||||
/// #[serde(serialize_with = "to_milli_ts")]
|
||||
/// time: NaiveDateTime
|
||||
/// time: NaiveDateTime,
|
||||
/// }
|
||||
///
|
||||
/// let my_s = S {
|
||||
/// time: NaiveDate::from_ymd_opt(2018, 5, 17).unwrap().and_hms_milli_opt(02, 04, 59, 918).unwrap(),
|
||||
/// time: NaiveDate::from_ymd_opt(2018, 5, 17)
|
||||
/// .unwrap()
|
||||
/// .and_hms_milli_opt(02, 04, 59, 918)
|
||||
/// .unwrap(),
|
||||
/// };
|
||||
/// let as_string = serde_json::to_string(&my_s)?;
|
||||
/// assert_eq!(as_string, r#"{"time":1526522699918}"#);
|
||||
@ -659,7 +685,7 @@ pub mod ts_milliseconds {
|
||||
/// #[derive(Debug, PartialEq, Deserialize)]
|
||||
/// struct S {
|
||||
/// #[serde(deserialize_with = "from_milli_ts")]
|
||||
/// time: NaiveDateTime
|
||||
/// time: NaiveDateTime,
|
||||
/// }
|
||||
///
|
||||
/// let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918 }"#)?;
|
||||
@ -719,13 +745,13 @@ pub mod ts_milliseconds {
|
||||
/// #[derive(Deserialize, Serialize)]
|
||||
/// struct S {
|
||||
/// #[serde(with = "ts_milliseconds_option")]
|
||||
/// time: Option<NaiveDateTime>
|
||||
/// time: Option<NaiveDateTime>,
|
||||
/// }
|
||||
///
|
||||
/// let time = Some(NaiveDate::from_ymd_opt(2018, 5, 17).unwrap().and_hms_milli_opt(02, 04, 59, 918).unwrap());
|
||||
/// let my_s = S {
|
||||
/// time: time.clone(),
|
||||
/// };
|
||||
/// let time = Some(
|
||||
/// NaiveDate::from_ymd_opt(2018, 5, 17).unwrap().and_hms_milli_opt(02, 04, 59, 918).unwrap(),
|
||||
/// );
|
||||
/// let my_s = S { time: time.clone() };
|
||||
///
|
||||
/// let as_string = serde_json::to_string(&my_s)?;
|
||||
/// assert_eq!(as_string, r#"{"time":1526522699918}"#);
|
||||
@ -753,11 +779,16 @@ pub mod ts_milliseconds_option {
|
||||
/// #[derive(Serialize)]
|
||||
/// struct S {
|
||||
/// #[serde(serialize_with = "to_milli_tsopt")]
|
||||
/// time: Option<NaiveDateTime>
|
||||
/// time: Option<NaiveDateTime>,
|
||||
/// }
|
||||
///
|
||||
/// let my_s = S {
|
||||
/// time: Some(NaiveDate::from_ymd_opt(2018, 5, 17).unwrap().and_hms_milli_opt(02, 04, 59, 918).unwrap()),
|
||||
/// time: Some(
|
||||
/// NaiveDate::from_ymd_opt(2018, 5, 17)
|
||||
/// .unwrap()
|
||||
/// .and_hms_milli_opt(02, 04, 59, 918)
|
||||
/// .unwrap(),
|
||||
/// ),
|
||||
/// };
|
||||
/// let as_string = serde_json::to_string(&my_s)?;
|
||||
/// assert_eq!(as_string, r#"{"time":1526522699918}"#);
|
||||
@ -786,7 +817,7 @@ pub mod ts_milliseconds_option {
|
||||
/// #[derive(Debug, PartialEq, Deserialize)]
|
||||
/// struct S {
|
||||
/// #[serde(deserialize_with = "from_milli_tsopt")]
|
||||
/// time: Option<NaiveDateTime>
|
||||
/// time: Option<NaiveDateTime>,
|
||||
/// }
|
||||
///
|
||||
/// let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918 }"#)?;
|
||||
@ -849,13 +880,11 @@ pub mod ts_milliseconds_option {
|
||||
/// #[derive(Deserialize, Serialize)]
|
||||
/// struct S {
|
||||
/// #[serde(with = "ts_seconds")]
|
||||
/// time: NaiveDateTime
|
||||
/// time: NaiveDateTime,
|
||||
/// }
|
||||
///
|
||||
/// let time = NaiveDate::from_ymd_opt(2015, 5, 15).unwrap().and_hms_opt(10, 0, 0).unwrap();
|
||||
/// let my_s = S {
|
||||
/// time: time.clone(),
|
||||
/// };
|
||||
/// let my_s = S { time: time.clone() };
|
||||
///
|
||||
/// let as_string = serde_json::to_string(&my_s)?;
|
||||
/// assert_eq!(as_string, r#"{"time":1431684000}"#);
|
||||
@ -883,12 +912,11 @@ pub mod ts_seconds {
|
||||
/// #[derive(Serialize)]
|
||||
/// struct S {
|
||||
/// #[serde(serialize_with = "to_ts")]
|
||||
/// time: NaiveDateTime
|
||||
/// time: NaiveDateTime,
|
||||
/// }
|
||||
///
|
||||
/// let my_s = S {
|
||||
/// time: NaiveDate::from_ymd_opt(2015, 5, 15).unwrap().and_hms_opt(10, 0, 0).unwrap(),
|
||||
/// };
|
||||
/// let my_s =
|
||||
/// S { time: NaiveDate::from_ymd_opt(2015, 5, 15).unwrap().and_hms_opt(10, 0, 0).unwrap() };
|
||||
/// let as_string = serde_json::to_string(&my_s)?;
|
||||
/// assert_eq!(as_string, r#"{"time":1431684000}"#);
|
||||
/// # Ok::<(), serde_json::Error>(())
|
||||
@ -913,7 +941,7 @@ pub mod ts_seconds {
|
||||
/// #[derive(Debug, PartialEq, Deserialize)]
|
||||
/// struct S {
|
||||
/// #[serde(deserialize_with = "from_ts")]
|
||||
/// time: NaiveDateTime
|
||||
/// time: NaiveDateTime,
|
||||
/// }
|
||||
///
|
||||
/// let my_s: S = serde_json::from_str(r#"{ "time": 1431684000 }"#)?;
|
||||
@ -967,13 +995,11 @@ pub mod ts_seconds {
|
||||
/// #[derive(Deserialize, Serialize)]
|
||||
/// struct S {
|
||||
/// #[serde(with = "ts_seconds_option")]
|
||||
/// time: Option<NaiveDateTime>
|
||||
/// time: Option<NaiveDateTime>,
|
||||
/// }
|
||||
///
|
||||
/// let time = Some(NaiveDate::from_ymd_opt(2018, 5, 17).unwrap().and_hms_opt(02, 04, 59).unwrap());
|
||||
/// let my_s = S {
|
||||
/// time: time.clone(),
|
||||
/// };
|
||||
/// let my_s = S { time: time.clone() };
|
||||
///
|
||||
/// let as_string = serde_json::to_string(&my_s)?;
|
||||
/// assert_eq!(as_string, r#"{"time":1526522699}"#);
|
||||
@ -1001,7 +1027,7 @@ pub mod ts_seconds_option {
|
||||
/// #[derive(Serialize)]
|
||||
/// struct S {
|
||||
/// #[serde(serialize_with = "to_tsopt")]
|
||||
/// time: Option<NaiveDateTime>
|
||||
/// time: Option<NaiveDateTime>,
|
||||
/// }
|
||||
///
|
||||
/// let my_s = S {
|
||||
@ -1034,7 +1060,7 @@ pub mod ts_seconds_option {
|
||||
/// #[derive(Debug, PartialEq, Deserialize)]
|
||||
/// struct S {
|
||||
/// #[serde(deserialize_with = "from_tsopt")]
|
||||
/// time: Option<NaiveDateTime>
|
||||
/// time: Option<NaiveDateTime>,
|
||||
/// }
|
||||
///
|
||||
/// let my_s: S = serde_json::from_str(r#"{ "time": 1431684000 }"#)?;
|
||||
|
@ -62,7 +62,7 @@ impl IsoWeek {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{NaiveDate, Datelike, Weekday};
|
||||
/// use chrono::{Datelike, NaiveDate, Weekday};
|
||||
///
|
||||
/// let d = NaiveDate::from_isoywd_opt(2015, 1, Weekday::Mon).unwrap();
|
||||
/// assert_eq!(d.iso_week().year(), 2015);
|
||||
@ -89,7 +89,7 @@ impl IsoWeek {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{NaiveDate, Datelike, Weekday};
|
||||
/// use chrono::{Datelike, NaiveDate, Weekday};
|
||||
///
|
||||
/// let d = NaiveDate::from_isoywd_opt(2015, 15, Weekday::Mon).unwrap();
|
||||
/// assert_eq!(d.iso_week().week(), 15);
|
||||
@ -106,7 +106,7 @@ impl IsoWeek {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{NaiveDate, Datelike, Weekday};
|
||||
/// use chrono::{Datelike, NaiveDate, Weekday};
|
||||
///
|
||||
/// let d = NaiveDate::from_isoywd_opt(2015, 15, Weekday::Mon).unwrap();
|
||||
/// assert_eq!(d.iso_week().week0(), 14);
|
||||
@ -124,19 +124,28 @@ impl IsoWeek {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{NaiveDate, Datelike};
|
||||
/// use chrono::{Datelike, NaiveDate};
|
||||
///
|
||||
/// assert_eq!(format!("{:?}", NaiveDate::from_ymd_opt(2015, 9, 5).unwrap().iso_week()), "2015-W36");
|
||||
/// assert_eq!(format!("{:?}", NaiveDate::from_ymd_opt( 0, 1, 3).unwrap().iso_week()), "0000-W01");
|
||||
/// assert_eq!(format!("{:?}", NaiveDate::from_ymd_opt(9999, 12, 31).unwrap().iso_week()), "9999-W52");
|
||||
/// assert_eq!(
|
||||
/// format!("{:?}", NaiveDate::from_ymd_opt(2015, 9, 5).unwrap().iso_week()),
|
||||
/// "2015-W36"
|
||||
/// );
|
||||
/// assert_eq!(format!("{:?}", NaiveDate::from_ymd_opt(0, 1, 3).unwrap().iso_week()), "0000-W01");
|
||||
/// assert_eq!(
|
||||
/// format!("{:?}", NaiveDate::from_ymd_opt(9999, 12, 31).unwrap().iso_week()),
|
||||
/// "9999-W52"
|
||||
/// );
|
||||
/// ```
|
||||
///
|
||||
/// ISO 8601 requires an explicit sign for years before 1 BCE or after 9999 CE.
|
||||
///
|
||||
/// ```
|
||||
/// # use chrono::{NaiveDate, Datelike};
|
||||
/// assert_eq!(format!("{:?}", NaiveDate::from_ymd_opt( 0, 1, 2).unwrap().iso_week()), "-0001-W52");
|
||||
/// assert_eq!(format!("{:?}", NaiveDate::from_ymd_opt(10000, 12, 31).unwrap().iso_week()), "+10000-W52");
|
||||
/// assert_eq!(format!("{:?}", NaiveDate::from_ymd_opt(0, 1, 2).unwrap().iso_week()), "-0001-W52");
|
||||
/// assert_eq!(
|
||||
/// format!("{:?}", NaiveDate::from_ymd_opt(10000, 12, 31).unwrap().iso_week()),
|
||||
/// "+10000-W52"
|
||||
/// );
|
||||
/// ```
|
||||
impl fmt::Debug for IsoWeek {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
|
@ -80,9 +80,17 @@ mod tests;
|
||||
///
|
||||
/// let t = NaiveTime::from_hms_milli_opt(8, 59, 59, 1_000).unwrap();
|
||||
///
|
||||
/// let dt1 = NaiveDate::from_ymd_opt(2015, 7, 1).unwrap().and_hms_micro_opt(8, 59, 59, 1_000_000).unwrap();
|
||||
/// let dt1 = NaiveDate::from_ymd_opt(2015, 7, 1)
|
||||
/// .unwrap()
|
||||
/// .and_hms_micro_opt(8, 59, 59, 1_000_000)
|
||||
/// .unwrap();
|
||||
///
|
||||
/// let dt2 = NaiveDate::from_ymd_opt(2015, 6, 30).unwrap().and_hms_nano_opt(23, 59, 59, 1_000_000_000).unwrap().and_local_timezone(Utc).unwrap();
|
||||
/// let dt2 = NaiveDate::from_ymd_opt(2015, 6, 30)
|
||||
/// .unwrap()
|
||||
/// .and_hms_nano_opt(23, 59, 59, 1_000_000_000)
|
||||
/// .unwrap()
|
||||
/// .and_local_timezone(Utc)
|
||||
/// .unwrap();
|
||||
/// # let _ = (t, dt1, dt2);
|
||||
/// ```
|
||||
///
|
||||
@ -164,9 +172,14 @@ mod tests;
|
||||
/// will be represented as the second part being 60, as required by ISO 8601.
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{Utc, NaiveDate};
|
||||
/// use chrono::{NaiveDate, Utc};
|
||||
///
|
||||
/// let dt = NaiveDate::from_ymd_opt(2015, 6, 30).unwrap().and_hms_milli_opt(23, 59, 59, 1_000).unwrap().and_local_timezone(Utc).unwrap();
|
||||
/// let dt = NaiveDate::from_ymd_opt(2015, 6, 30)
|
||||
/// .unwrap()
|
||||
/// .and_hms_milli_opt(23, 59, 59, 1_000)
|
||||
/// .unwrap()
|
||||
/// .and_local_timezone(Utc)
|
||||
/// .unwrap();
|
||||
/// assert_eq!(format!("{:?}", dt), "2015-06-30T23:59:60Z");
|
||||
/// ```
|
||||
///
|
||||
@ -485,10 +498,14 @@ impl NaiveTime {
|
||||
///
|
||||
/// let parse_from_str = NaiveTime::parse_from_str;
|
||||
///
|
||||
/// assert_eq!(parse_from_str("23:56:04", "%H:%M:%S"),
|
||||
/// Ok(NaiveTime::from_hms_opt(23, 56, 4).unwrap()));
|
||||
/// assert_eq!(parse_from_str("pm012345.6789", "%p%I%M%S%.f"),
|
||||
/// Ok(NaiveTime::from_hms_micro_opt(13, 23, 45, 678_900).unwrap()));
|
||||
/// assert_eq!(
|
||||
/// parse_from_str("23:56:04", "%H:%M:%S"),
|
||||
/// Ok(NaiveTime::from_hms_opt(23, 56, 4).unwrap())
|
||||
/// );
|
||||
/// assert_eq!(
|
||||
/// parse_from_str("pm012345.6789", "%p%I%M%S%.f"),
|
||||
/// Ok(NaiveTime::from_hms_micro_opt(13, 23, 45, 678_900).unwrap())
|
||||
/// );
|
||||
/// ```
|
||||
///
|
||||
/// Date and offset is ignored for the purpose of parsing.
|
||||
@ -496,8 +513,10 @@ impl NaiveTime {
|
||||
/// ```
|
||||
/// # use chrono::NaiveTime;
|
||||
/// # let parse_from_str = NaiveTime::parse_from_str;
|
||||
/// assert_eq!(parse_from_str("2014-5-17T12:34:56+09:30", "%Y-%m-%dT%H:%M:%S%z"),
|
||||
/// Ok(NaiveTime::from_hms_opt(12, 34, 56).unwrap()));
|
||||
/// assert_eq!(
|
||||
/// parse_from_str("2014-5-17T12:34:56+09:30", "%Y-%m-%dT%H:%M:%S%z"),
|
||||
/// Ok(NaiveTime::from_hms_opt(12, 34, 56).unwrap())
|
||||
/// );
|
||||
/// ```
|
||||
///
|
||||
/// [Leap seconds](#leap-second-handling) are correctly handled by
|
||||
@ -507,8 +526,10 @@ impl NaiveTime {
|
||||
/// ```
|
||||
/// # use chrono::NaiveTime;
|
||||
/// # let parse_from_str = NaiveTime::parse_from_str;
|
||||
/// assert_eq!(parse_from_str("08:59:60.123", "%H:%M:%S%.f"),
|
||||
/// Ok(NaiveTime::from_hms_milli_opt(8, 59, 59, 1_123).unwrap()));
|
||||
/// assert_eq!(
|
||||
/// parse_from_str("08:59:60.123", "%H:%M:%S%.f"),
|
||||
/// Ok(NaiveTime::from_hms_milli_opt(8, 59, 59, 1_123).unwrap())
|
||||
/// );
|
||||
/// ```
|
||||
///
|
||||
/// Missing seconds are assumed to be zero,
|
||||
@ -517,8 +538,7 @@ impl NaiveTime {
|
||||
/// ```
|
||||
/// # use chrono::NaiveTime;
|
||||
/// # let parse_from_str = NaiveTime::parse_from_str;
|
||||
/// assert_eq!(parse_from_str("7:15", "%H:%M"),
|
||||
/// Ok(NaiveTime::from_hms_opt(7, 15, 0).unwrap()));
|
||||
/// assert_eq!(parse_from_str("7:15", "%H:%M"), Ok(NaiveTime::from_hms_opt(7, 15, 0).unwrap()));
|
||||
///
|
||||
/// assert!(parse_from_str("04m33s", "%Mm%Ss").is_err());
|
||||
/// assert!(parse_from_str("12", "%H").is_err());
|
||||
@ -552,8 +572,8 @@ impl NaiveTime {
|
||||
///
|
||||
/// ```rust
|
||||
/// # use chrono::{NaiveTime};
|
||||
/// let (time, remainder) = NaiveTime::parse_and_remainder(
|
||||
/// "3h4m33s trailing text", "%-Hh%-Mm%-Ss").unwrap();
|
||||
/// let (time, remainder) =
|
||||
/// NaiveTime::parse_and_remainder("3h4m33s trailing text", "%-Hh%-Mm%-Ss").unwrap();
|
||||
/// assert_eq!(time, NaiveTime::from_hms_opt(3, 4, 33).unwrap());
|
||||
/// assert_eq!(remainder, " trailing text");
|
||||
/// ```
|
||||
@ -569,16 +589,22 @@ impl NaiveTime {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{TimeDelta, NaiveTime};
|
||||
/// use chrono::{NaiveTime, TimeDelta};
|
||||
///
|
||||
/// let from_hms = |h, m, s| { NaiveTime::from_hms_opt(h, m, s).unwrap() };
|
||||
/// let from_hms = |h, m, s| NaiveTime::from_hms_opt(h, m, s).unwrap();
|
||||
///
|
||||
/// assert_eq!(from_hms(3, 4, 5).overflowing_add_signed(TimeDelta::hours(11)),
|
||||
/// (from_hms(14, 4, 5), 0));
|
||||
/// assert_eq!(from_hms(3, 4, 5).overflowing_add_signed(TimeDelta::hours(23)),
|
||||
/// (from_hms(2, 4, 5), 86_400));
|
||||
/// assert_eq!(from_hms(3, 4, 5).overflowing_add_signed(TimeDelta::hours(-7)),
|
||||
/// (from_hms(20, 4, 5), -86_400));
|
||||
/// assert_eq!(
|
||||
/// from_hms(3, 4, 5).overflowing_add_signed(TimeDelta::hours(11)),
|
||||
/// (from_hms(14, 4, 5), 0)
|
||||
/// );
|
||||
/// assert_eq!(
|
||||
/// from_hms(3, 4, 5).overflowing_add_signed(TimeDelta::hours(23)),
|
||||
/// (from_hms(2, 4, 5), 86_400)
|
||||
/// );
|
||||
/// assert_eq!(
|
||||
/// from_hms(3, 4, 5).overflowing_add_signed(TimeDelta::hours(-7)),
|
||||
/// (from_hms(20, 4, 5), -86_400)
|
||||
/// );
|
||||
/// ```
|
||||
#[must_use]
|
||||
pub const fn overflowing_add_signed(&self, rhs: TimeDelta) -> (NaiveTime, i64) {
|
||||
@ -625,16 +651,22 @@ impl NaiveTime {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{TimeDelta, NaiveTime};
|
||||
/// use chrono::{NaiveTime, TimeDelta};
|
||||
///
|
||||
/// let from_hms = |h, m, s| { NaiveTime::from_hms_opt(h, m, s).unwrap() };
|
||||
/// let from_hms = |h, m, s| NaiveTime::from_hms_opt(h, m, s).unwrap();
|
||||
///
|
||||
/// assert_eq!(from_hms(3, 4, 5).overflowing_sub_signed(TimeDelta::hours(2)),
|
||||
/// (from_hms(1, 4, 5), 0));
|
||||
/// assert_eq!(from_hms(3, 4, 5).overflowing_sub_signed(TimeDelta::hours(17)),
|
||||
/// (from_hms(10, 4, 5), 86_400));
|
||||
/// assert_eq!(from_hms(3, 4, 5).overflowing_sub_signed(TimeDelta::hours(-22)),
|
||||
/// (from_hms(1, 4, 5), -86_400));
|
||||
/// assert_eq!(
|
||||
/// from_hms(3, 4, 5).overflowing_sub_signed(TimeDelta::hours(2)),
|
||||
/// (from_hms(1, 4, 5), 0)
|
||||
/// );
|
||||
/// assert_eq!(
|
||||
/// from_hms(3, 4, 5).overflowing_sub_signed(TimeDelta::hours(17)),
|
||||
/// (from_hms(10, 4, 5), 86_400)
|
||||
/// );
|
||||
/// assert_eq!(
|
||||
/// from_hms(3, 4, 5).overflowing_sub_signed(TimeDelta::hours(-22)),
|
||||
/// (from_hms(1, 4, 5), -86_400)
|
||||
/// );
|
||||
/// ```
|
||||
#[inline]
|
||||
#[must_use]
|
||||
@ -656,27 +688,31 @@ impl NaiveTime {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{TimeDelta, NaiveTime};
|
||||
/// use chrono::{NaiveTime, TimeDelta};
|
||||
///
|
||||
/// let from_hmsm = |h, m, s, milli| { NaiveTime::from_hms_milli_opt(h, m, s, milli).unwrap() };
|
||||
/// let from_hmsm = |h, m, s, milli| NaiveTime::from_hms_milli_opt(h, m, s, milli).unwrap();
|
||||
/// let since = NaiveTime::signed_duration_since;
|
||||
///
|
||||
/// assert_eq!(since(from_hmsm(3, 5, 7, 900), from_hmsm(3, 5, 7, 900)),
|
||||
/// TimeDelta::zero());
|
||||
/// assert_eq!(since(from_hmsm(3, 5, 7, 900), from_hmsm(3, 5, 7, 875)),
|
||||
/// TimeDelta::milliseconds(25));
|
||||
/// assert_eq!(since(from_hmsm(3, 5, 7, 900), from_hmsm(3, 5, 6, 925)),
|
||||
/// TimeDelta::milliseconds(975));
|
||||
/// assert_eq!(since(from_hmsm(3, 5, 7, 900), from_hmsm(3, 5, 0, 900)),
|
||||
/// TimeDelta::seconds(7));
|
||||
/// assert_eq!(since(from_hmsm(3, 5, 7, 900), from_hmsm(3, 0, 7, 900)),
|
||||
/// TimeDelta::seconds(5 * 60));
|
||||
/// assert_eq!(since(from_hmsm(3, 5, 7, 900), from_hmsm(0, 5, 7, 900)),
|
||||
/// TimeDelta::seconds(3 * 3600));
|
||||
/// assert_eq!(since(from_hmsm(3, 5, 7, 900), from_hmsm(4, 5, 7, 900)),
|
||||
/// TimeDelta::seconds(-3600));
|
||||
/// assert_eq!(since(from_hmsm(3, 5, 7, 900), from_hmsm(2, 4, 6, 800)),
|
||||
/// TimeDelta::seconds(3600 + 60 + 1) + TimeDelta::milliseconds(100));
|
||||
/// assert_eq!(since(from_hmsm(3, 5, 7, 900), from_hmsm(3, 5, 7, 900)), TimeDelta::zero());
|
||||
/// assert_eq!(
|
||||
/// since(from_hmsm(3, 5, 7, 900), from_hmsm(3, 5, 7, 875)),
|
||||
/// TimeDelta::milliseconds(25)
|
||||
/// );
|
||||
/// assert_eq!(
|
||||
/// since(from_hmsm(3, 5, 7, 900), from_hmsm(3, 5, 6, 925)),
|
||||
/// TimeDelta::milliseconds(975)
|
||||
/// );
|
||||
/// assert_eq!(since(from_hmsm(3, 5, 7, 900), from_hmsm(3, 5, 0, 900)), TimeDelta::seconds(7));
|
||||
/// assert_eq!(since(from_hmsm(3, 5, 7, 900), from_hmsm(3, 0, 7, 900)), TimeDelta::seconds(5 * 60));
|
||||
/// assert_eq!(
|
||||
/// since(from_hmsm(3, 5, 7, 900), from_hmsm(0, 5, 7, 900)),
|
||||
/// TimeDelta::seconds(3 * 3600)
|
||||
/// );
|
||||
/// assert_eq!(since(from_hmsm(3, 5, 7, 900), from_hmsm(4, 5, 7, 900)), TimeDelta::seconds(-3600));
|
||||
/// assert_eq!(
|
||||
/// since(from_hmsm(3, 5, 7, 900), from_hmsm(2, 4, 6, 800)),
|
||||
/// TimeDelta::seconds(3600 + 60 + 1) + TimeDelta::milliseconds(100)
|
||||
/// );
|
||||
/// ```
|
||||
///
|
||||
/// Leap seconds are handled, but the subtraction assumes that
|
||||
@ -760,13 +796,13 @@ impl NaiveTime {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::NaiveTime;
|
||||
/// use chrono::format::strftime::StrftimeItems;
|
||||
/// use chrono::NaiveTime;
|
||||
///
|
||||
/// let fmt = StrftimeItems::new("%H:%M:%S");
|
||||
/// let t = NaiveTime::from_hms_opt(23, 56, 4).unwrap();
|
||||
/// assert_eq!(t.format_with_items(fmt.clone()).to_string(), "23:56:04");
|
||||
/// assert_eq!(t.format("%H:%M:%S").to_string(), "23:56:04");
|
||||
/// assert_eq!(t.format("%H:%M:%S").to_string(), "23:56:04");
|
||||
/// ```
|
||||
///
|
||||
/// The resulting `DelayedFormat` can be formatted directly via the `Display` trait.
|
||||
@ -927,7 +963,10 @@ impl Timelike for NaiveTime {
|
||||
/// use chrono::{NaiveTime, Timelike};
|
||||
///
|
||||
/// assert_eq!(NaiveTime::from_hms_opt(0, 0, 0).unwrap().nanosecond(), 0);
|
||||
/// assert_eq!(NaiveTime::from_hms_nano_opt(23, 56, 4, 12_345_678).unwrap().nanosecond(), 12_345_678);
|
||||
/// assert_eq!(
|
||||
/// NaiveTime::from_hms_nano_opt(23, 56, 4, 12_345_678).unwrap().nanosecond(),
|
||||
/// 12_345_678
|
||||
/// );
|
||||
/// ```
|
||||
///
|
||||
/// Leap seconds may have seemingly out-of-range return values.
|
||||
@ -982,7 +1021,10 @@ impl Timelike for NaiveTime {
|
||||
/// use chrono::{NaiveTime, Timelike};
|
||||
///
|
||||
/// let dt = NaiveTime::from_hms_nano_opt(23, 56, 4, 12_345_678).unwrap();
|
||||
/// assert_eq!(dt.with_minute(45), Some(NaiveTime::from_hms_nano_opt(23, 45, 4, 12_345_678).unwrap()));
|
||||
/// assert_eq!(
|
||||
/// dt.with_minute(45),
|
||||
/// Some(NaiveTime::from_hms_nano_opt(23, 45, 4, 12_345_678).unwrap())
|
||||
/// );
|
||||
/// assert_eq!(dt.with_minute(60), None);
|
||||
/// ```
|
||||
#[inline]
|
||||
@ -1009,7 +1051,10 @@ impl Timelike for NaiveTime {
|
||||
/// use chrono::{NaiveTime, Timelike};
|
||||
///
|
||||
/// let dt = NaiveTime::from_hms_nano_opt(23, 56, 4, 12_345_678).unwrap();
|
||||
/// assert_eq!(dt.with_second(17), Some(NaiveTime::from_hms_nano_opt(23, 56, 17, 12_345_678).unwrap()));
|
||||
/// assert_eq!(
|
||||
/// dt.with_second(17),
|
||||
/// Some(NaiveTime::from_hms_nano_opt(23, 56, 17, 12_345_678).unwrap())
|
||||
/// );
|
||||
/// assert_eq!(dt.with_second(60), None);
|
||||
/// ```
|
||||
#[inline]
|
||||
@ -1036,8 +1081,10 @@ impl Timelike for NaiveTime {
|
||||
/// use chrono::{NaiveTime, Timelike};
|
||||
///
|
||||
/// let dt = NaiveTime::from_hms_nano_opt(23, 56, 4, 12_345_678).unwrap();
|
||||
/// assert_eq!(dt.with_nanosecond(333_333_333),
|
||||
/// Some(NaiveTime::from_hms_nano_opt(23, 56, 4, 333_333_333).unwrap()));
|
||||
/// assert_eq!(
|
||||
/// dt.with_nanosecond(333_333_333),
|
||||
/// Some(NaiveTime::from_hms_nano_opt(23, 56, 4, 333_333_333).unwrap())
|
||||
/// );
|
||||
/// assert_eq!(dt.with_nanosecond(2_000_000_000), None);
|
||||
/// ```
|
||||
///
|
||||
@ -1067,12 +1114,15 @@ impl Timelike for NaiveTime {
|
||||
/// ```
|
||||
/// use chrono::{NaiveTime, Timelike};
|
||||
///
|
||||
/// assert_eq!(NaiveTime::from_hms_opt(1, 2, 3).unwrap().num_seconds_from_midnight(),
|
||||
/// 3723);
|
||||
/// assert_eq!(NaiveTime::from_hms_nano_opt(23, 56, 4, 12_345_678).unwrap().num_seconds_from_midnight(),
|
||||
/// 86164);
|
||||
/// assert_eq!(NaiveTime::from_hms_milli_opt(23, 59, 59, 1_000).unwrap().num_seconds_from_midnight(),
|
||||
/// 86399);
|
||||
/// assert_eq!(NaiveTime::from_hms_opt(1, 2, 3).unwrap().num_seconds_from_midnight(), 3723);
|
||||
/// assert_eq!(
|
||||
/// NaiveTime::from_hms_nano_opt(23, 56, 4, 12_345_678).unwrap().num_seconds_from_midnight(),
|
||||
/// 86164
|
||||
/// );
|
||||
/// assert_eq!(
|
||||
/// NaiveTime::from_hms_milli_opt(23, 59, 59, 1_000).unwrap().num_seconds_from_midnight(),
|
||||
/// 86399
|
||||
/// );
|
||||
/// ```
|
||||
#[inline]
|
||||
fn num_seconds_from_midnight(&self) -> u32 {
|
||||
@ -1092,18 +1142,21 @@ impl Timelike for NaiveTime {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{TimeDelta, NaiveTime};
|
||||
/// use chrono::{NaiveTime, TimeDelta};
|
||||
///
|
||||
/// let from_hmsm = |h, m, s, milli| { NaiveTime::from_hms_milli_opt(h, m, s, milli).unwrap() };
|
||||
/// let from_hmsm = |h, m, s, milli| NaiveTime::from_hms_milli_opt(h, m, s, milli).unwrap();
|
||||
///
|
||||
/// assert_eq!(from_hmsm(3, 5, 7, 0) + TimeDelta::zero(), from_hmsm(3, 5, 7, 0));
|
||||
/// assert_eq!(from_hmsm(3, 5, 7, 0) + TimeDelta::seconds(1), from_hmsm(3, 5, 8, 0));
|
||||
/// assert_eq!(from_hmsm(3, 5, 7, 0) + TimeDelta::seconds(-1), from_hmsm(3, 5, 6, 0));
|
||||
/// assert_eq!(from_hmsm(3, 5, 7, 0) + TimeDelta::seconds(60 + 4), from_hmsm(3, 6, 11, 0));
|
||||
/// assert_eq!(from_hmsm(3, 5, 7, 0) + TimeDelta::seconds(7*60*60 - 6*60), from_hmsm(9, 59, 7, 0));
|
||||
/// assert_eq!(from_hmsm(3, 5, 7, 0) + TimeDelta::milliseconds(80), from_hmsm(3, 5, 7, 80));
|
||||
/// assert_eq!(from_hmsm(3, 5, 7, 950) + TimeDelta::milliseconds(280), from_hmsm(3, 5, 8, 230));
|
||||
/// assert_eq!(from_hmsm(3, 5, 7, 950) + TimeDelta::milliseconds(-980), from_hmsm(3, 5, 6, 970));
|
||||
/// assert_eq!(from_hmsm(3, 5, 7, 0) + TimeDelta::zero(), from_hmsm(3, 5, 7, 0));
|
||||
/// assert_eq!(from_hmsm(3, 5, 7, 0) + TimeDelta::seconds(1), from_hmsm(3, 5, 8, 0));
|
||||
/// assert_eq!(from_hmsm(3, 5, 7, 0) + TimeDelta::seconds(-1), from_hmsm(3, 5, 6, 0));
|
||||
/// assert_eq!(from_hmsm(3, 5, 7, 0) + TimeDelta::seconds(60 + 4), from_hmsm(3, 6, 11, 0));
|
||||
/// assert_eq!(
|
||||
/// from_hmsm(3, 5, 7, 0) + TimeDelta::seconds(7 * 60 * 60 - 6 * 60),
|
||||
/// from_hmsm(9, 59, 7, 0)
|
||||
/// );
|
||||
/// assert_eq!(from_hmsm(3, 5, 7, 0) + TimeDelta::milliseconds(80), from_hmsm(3, 5, 7, 80));
|
||||
/// assert_eq!(from_hmsm(3, 5, 7, 950) + TimeDelta::milliseconds(280), from_hmsm(3, 5, 8, 230));
|
||||
/// assert_eq!(from_hmsm(3, 5, 7, 950) + TimeDelta::milliseconds(-980), from_hmsm(3, 5, 6, 970));
|
||||
/// ```
|
||||
///
|
||||
/// The addition wraps around.
|
||||
@ -1207,16 +1260,19 @@ impl Add<FixedOffset> for NaiveTime {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{TimeDelta, NaiveTime};
|
||||
/// use chrono::{NaiveTime, TimeDelta};
|
||||
///
|
||||
/// let from_hmsm = |h, m, s, milli| { NaiveTime::from_hms_milli_opt(h, m, s, milli).unwrap() };
|
||||
/// let from_hmsm = |h, m, s, milli| NaiveTime::from_hms_milli_opt(h, m, s, milli).unwrap();
|
||||
///
|
||||
/// assert_eq!(from_hmsm(3, 5, 7, 0) - TimeDelta::zero(), from_hmsm(3, 5, 7, 0));
|
||||
/// assert_eq!(from_hmsm(3, 5, 7, 0) - TimeDelta::seconds(1), from_hmsm(3, 5, 6, 0));
|
||||
/// assert_eq!(from_hmsm(3, 5, 7, 0) - TimeDelta::seconds(60 + 5), from_hmsm(3, 4, 2, 0));
|
||||
/// assert_eq!(from_hmsm(3, 5, 7, 0) - TimeDelta::seconds(2*60*60 + 6*60), from_hmsm(0, 59, 7, 0));
|
||||
/// assert_eq!(from_hmsm(3, 5, 7, 0) - TimeDelta::milliseconds(80), from_hmsm(3, 5, 6, 920));
|
||||
/// assert_eq!(from_hmsm(3, 5, 7, 950) - TimeDelta::milliseconds(280), from_hmsm(3, 5, 7, 670));
|
||||
/// assert_eq!(from_hmsm(3, 5, 7, 0) - TimeDelta::zero(), from_hmsm(3, 5, 7, 0));
|
||||
/// assert_eq!(from_hmsm(3, 5, 7, 0) - TimeDelta::seconds(1), from_hmsm(3, 5, 6, 0));
|
||||
/// assert_eq!(from_hmsm(3, 5, 7, 0) - TimeDelta::seconds(60 + 5), from_hmsm(3, 4, 2, 0));
|
||||
/// assert_eq!(
|
||||
/// from_hmsm(3, 5, 7, 0) - TimeDelta::seconds(2 * 60 * 60 + 6 * 60),
|
||||
/// from_hmsm(0, 59, 7, 0)
|
||||
/// );
|
||||
/// assert_eq!(from_hmsm(3, 5, 7, 0) - TimeDelta::milliseconds(80), from_hmsm(3, 5, 6, 920));
|
||||
/// assert_eq!(from_hmsm(3, 5, 7, 950) - TimeDelta::milliseconds(280), from_hmsm(3, 5, 7, 670));
|
||||
/// ```
|
||||
///
|
||||
/// The subtraction wraps around.
|
||||
@ -1320,9 +1376,9 @@ impl Sub<FixedOffset> for NaiveTime {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{TimeDelta, NaiveTime};
|
||||
/// use chrono::{NaiveTime, TimeDelta};
|
||||
///
|
||||
/// let from_hmsm = |h, m, s, milli| { NaiveTime::from_hms_milli_opt(h, m, s, milli).unwrap() };
|
||||
/// let from_hmsm = |h, m, s, milli| NaiveTime::from_hms_milli_opt(h, m, s, milli).unwrap();
|
||||
///
|
||||
/// assert_eq!(from_hmsm(3, 5, 7, 900) - from_hmsm(3, 5, 7, 900), TimeDelta::zero());
|
||||
/// assert_eq!(from_hmsm(3, 5, 7, 900) - from_hmsm(3, 5, 7, 875), TimeDelta::milliseconds(25));
|
||||
@ -1331,8 +1387,10 @@ impl Sub<FixedOffset> for NaiveTime {
|
||||
/// assert_eq!(from_hmsm(3, 5, 7, 900) - from_hmsm(3, 0, 7, 900), TimeDelta::seconds(5 * 60));
|
||||
/// assert_eq!(from_hmsm(3, 5, 7, 900) - from_hmsm(0, 5, 7, 900), TimeDelta::seconds(3 * 3600));
|
||||
/// assert_eq!(from_hmsm(3, 5, 7, 900) - from_hmsm(4, 5, 7, 900), TimeDelta::seconds(-3600));
|
||||
/// assert_eq!(from_hmsm(3, 5, 7, 900) - from_hmsm(2, 4, 6, 800),
|
||||
/// TimeDelta::seconds(3600 + 60 + 1) + TimeDelta::milliseconds(100));
|
||||
/// assert_eq!(
|
||||
/// from_hmsm(3, 5, 7, 900) - from_hmsm(2, 4, 6, 800),
|
||||
/// TimeDelta::seconds(3600 + 60 + 1) + TimeDelta::milliseconds(100)
|
||||
/// );
|
||||
/// ```
|
||||
///
|
||||
/// Leap seconds are handled, but the subtraction assumes that
|
||||
@ -1374,17 +1432,29 @@ impl Sub<NaiveTime> for NaiveTime {
|
||||
/// ```
|
||||
/// use chrono::NaiveTime;
|
||||
///
|
||||
/// assert_eq!(format!("{:?}", NaiveTime::from_hms_opt(23, 56, 4).unwrap()), "23:56:04");
|
||||
/// assert_eq!(format!("{:?}", NaiveTime::from_hms_milli_opt(23, 56, 4, 12).unwrap()), "23:56:04.012");
|
||||
/// assert_eq!(format!("{:?}", NaiveTime::from_hms_micro_opt(23, 56, 4, 1234).unwrap()), "23:56:04.001234");
|
||||
/// assert_eq!(format!("{:?}", NaiveTime::from_hms_nano_opt(23, 56, 4, 123456).unwrap()), "23:56:04.000123456");
|
||||
/// assert_eq!(format!("{:?}", NaiveTime::from_hms_opt(23, 56, 4).unwrap()), "23:56:04");
|
||||
/// assert_eq!(
|
||||
/// format!("{:?}", NaiveTime::from_hms_milli_opt(23, 56, 4, 12).unwrap()),
|
||||
/// "23:56:04.012"
|
||||
/// );
|
||||
/// assert_eq!(
|
||||
/// format!("{:?}", NaiveTime::from_hms_micro_opt(23, 56, 4, 1234).unwrap()),
|
||||
/// "23:56:04.001234"
|
||||
/// );
|
||||
/// assert_eq!(
|
||||
/// format!("{:?}", NaiveTime::from_hms_nano_opt(23, 56, 4, 123456).unwrap()),
|
||||
/// "23:56:04.000123456"
|
||||
/// );
|
||||
/// ```
|
||||
///
|
||||
/// Leap seconds may also be used.
|
||||
///
|
||||
/// ```
|
||||
/// # use chrono::NaiveTime;
|
||||
/// assert_eq!(format!("{:?}", NaiveTime::from_hms_milli_opt(6, 59, 59, 1_500).unwrap()), "06:59:60.500");
|
||||
/// assert_eq!(
|
||||
/// format!("{:?}", NaiveTime::from_hms_milli_opt(6, 59, 59, 1_500).unwrap()),
|
||||
/// "06:59:60.500"
|
||||
/// );
|
||||
/// ```
|
||||
impl fmt::Debug for NaiveTime {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
@ -1430,17 +1500,29 @@ impl fmt::Debug for NaiveTime {
|
||||
/// ```
|
||||
/// use chrono::NaiveTime;
|
||||
///
|
||||
/// assert_eq!(format!("{}", NaiveTime::from_hms_opt(23, 56, 4).unwrap()), "23:56:04");
|
||||
/// assert_eq!(format!("{}", NaiveTime::from_hms_milli_opt(23, 56, 4, 12).unwrap()), "23:56:04.012");
|
||||
/// assert_eq!(format!("{}", NaiveTime::from_hms_micro_opt(23, 56, 4, 1234).unwrap()), "23:56:04.001234");
|
||||
/// assert_eq!(format!("{}", NaiveTime::from_hms_nano_opt(23, 56, 4, 123456).unwrap()), "23:56:04.000123456");
|
||||
/// assert_eq!(format!("{}", NaiveTime::from_hms_opt(23, 56, 4).unwrap()), "23:56:04");
|
||||
/// assert_eq!(
|
||||
/// format!("{}", NaiveTime::from_hms_milli_opt(23, 56, 4, 12).unwrap()),
|
||||
/// "23:56:04.012"
|
||||
/// );
|
||||
/// assert_eq!(
|
||||
/// format!("{}", NaiveTime::from_hms_micro_opt(23, 56, 4, 1234).unwrap()),
|
||||
/// "23:56:04.001234"
|
||||
/// );
|
||||
/// assert_eq!(
|
||||
/// format!("{}", NaiveTime::from_hms_nano_opt(23, 56, 4, 123456).unwrap()),
|
||||
/// "23:56:04.000123456"
|
||||
/// );
|
||||
/// ```
|
||||
///
|
||||
/// Leap seconds may also be used.
|
||||
///
|
||||
/// ```
|
||||
/// # use chrono::NaiveTime;
|
||||
/// assert_eq!(format!("{}", NaiveTime::from_hms_milli_opt(6, 59, 59, 1_500).unwrap()), "06:59:60.500");
|
||||
/// assert_eq!(
|
||||
/// format!("{}", NaiveTime::from_hms_milli_opt(6, 59, 59, 1_500).unwrap()),
|
||||
/// "06:59:60.500"
|
||||
/// );
|
||||
/// ```
|
||||
impl fmt::Display for NaiveTime {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
|
@ -53,10 +53,8 @@ impl FixedOffset {
|
||||
#[cfg_attr(feature = "std", doc = "```")]
|
||||
/// use chrono::{FixedOffset, TimeZone};
|
||||
/// let hour = 3600;
|
||||
/// let datetime = FixedOffset::east_opt(5 * hour)
|
||||
/// .unwrap()
|
||||
/// .with_ymd_and_hms(2016, 11, 08, 0, 0, 0)
|
||||
/// .unwrap();
|
||||
/// let datetime =
|
||||
/// FixedOffset::east_opt(5 * hour).unwrap().with_ymd_and_hms(2016, 11, 08, 0, 0, 0).unwrap();
|
||||
/// assert_eq!(&datetime.to_rfc3339(), "2016-11-08T00:00:00+05:00")
|
||||
/// ```
|
||||
#[must_use]
|
||||
@ -89,10 +87,8 @@ impl FixedOffset {
|
||||
#[cfg_attr(feature = "std", doc = "```")]
|
||||
/// use chrono::{FixedOffset, TimeZone};
|
||||
/// let hour = 3600;
|
||||
/// let datetime = FixedOffset::west_opt(5 * hour)
|
||||
/// .unwrap()
|
||||
/// .with_ymd_and_hms(2016, 11, 08, 0, 0, 0)
|
||||
/// .unwrap();
|
||||
/// let datetime =
|
||||
/// FixedOffset::west_opt(5 * hour).unwrap().with_ymd_and_hms(2016, 11, 08, 0, 0, 0).unwrap();
|
||||
/// assert_eq!(&datetime.to_rfc3339(), "2016-11-08T00:00:00-05:00")
|
||||
/// ```
|
||||
#[must_use]
|
||||
|
@ -100,7 +100,7 @@ mod tz_info;
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{Local, DateTime, TimeZone};
|
||||
/// use chrono::{DateTime, Local, TimeZone};
|
||||
///
|
||||
/// let dt1: DateTime<Local> = Local::now();
|
||||
/// let dt2: DateTime<Local> = Local.timestamp_opt(0, 0).unwrap();
|
||||
|
@ -375,7 +375,7 @@ pub trait TimeZone: Sized + Clone {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{Utc, TimeZone};
|
||||
/// use chrono::{TimeZone, Utc};
|
||||
///
|
||||
/// assert_eq!(Utc.timestamp_opt(1431648000, 0).unwrap().to_string(), "2015-05-15 00:00:00 UTC");
|
||||
/// ```
|
||||
@ -407,7 +407,7 @@ pub trait TimeZone: Sized + Clone {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{Utc, TimeZone, LocalResult};
|
||||
/// use chrono::{LocalResult, TimeZone, Utc};
|
||||
/// match Utc.timestamp_millis_opt(1431648000) {
|
||||
/// LocalResult::Single(dt) => assert_eq!(dt.timestamp(), 1431648),
|
||||
/// _ => panic!("Incorrect timestamp_millis"),
|
||||
@ -428,7 +428,7 @@ pub trait TimeZone: Sized + Clone {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{Utc, TimeZone};
|
||||
/// use chrono::{TimeZone, Utc};
|
||||
///
|
||||
/// assert_eq!(Utc.timestamp_nanos(1431648000000000).timestamp(), 1431648);
|
||||
/// ```
|
||||
@ -447,7 +447,7 @@ pub trait TimeZone: Sized + Clone {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{Utc, TimeZone};
|
||||
/// use chrono::{TimeZone, Utc};
|
||||
///
|
||||
/// assert_eq!(Utc.timestamp_micros(1431648000000).unwrap().timestamp(), 1431648);
|
||||
/// ```
|
||||
|
@ -33,7 +33,7 @@ use crate::{Date, DateTime};
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{TimeZone, NaiveDateTime, Utc};
|
||||
/// use chrono::{NaiveDateTime, TimeZone, Utc};
|
||||
///
|
||||
/// let dt = Utc.from_utc_datetime(&NaiveDateTime::from_timestamp_opt(61, 0).unwrap());
|
||||
///
|
||||
|
35
src/round.rs
35
src/round.rs
@ -23,7 +23,12 @@ pub trait SubsecRound {
|
||||
/// # Example
|
||||
/// ``` rust
|
||||
/// # use chrono::{SubsecRound, Timelike, Utc, NaiveDate};
|
||||
/// let dt = NaiveDate::from_ymd_opt(2018, 1, 11).unwrap().and_hms_milli_opt(12, 0, 0, 154).unwrap().and_local_timezone(Utc).unwrap();
|
||||
/// let dt = NaiveDate::from_ymd_opt(2018, 1, 11)
|
||||
/// .unwrap()
|
||||
/// .and_hms_milli_opt(12, 0, 0, 154)
|
||||
/// .unwrap()
|
||||
/// .and_local_timezone(Utc)
|
||||
/// .unwrap();
|
||||
/// assert_eq!(dt.round_subsecs(2).nanosecond(), 150_000_000);
|
||||
/// assert_eq!(dt.round_subsecs(1).nanosecond(), 200_000_000);
|
||||
/// ```
|
||||
@ -35,7 +40,12 @@ pub trait SubsecRound {
|
||||
/// # Example
|
||||
/// ``` rust
|
||||
/// # use chrono::{SubsecRound, Timelike, Utc, NaiveDate};
|
||||
/// let dt = NaiveDate::from_ymd_opt(2018, 1, 11).unwrap().and_hms_milli_opt(12, 0, 0, 154).unwrap().and_local_timezone(Utc).unwrap();
|
||||
/// let dt = NaiveDate::from_ymd_opt(2018, 1, 11)
|
||||
/// .unwrap()
|
||||
/// .and_hms_milli_opt(12, 0, 0, 154)
|
||||
/// .unwrap()
|
||||
/// .and_local_timezone(Utc)
|
||||
/// .unwrap();
|
||||
/// assert_eq!(dt.trunc_subsecs(2).nanosecond(), 150_000_000);
|
||||
/// assert_eq!(dt.trunc_subsecs(1).nanosecond(), 100_000_000);
|
||||
/// ```
|
||||
@ -110,7 +120,12 @@ pub trait DurationRound: Sized {
|
||||
/// # Example
|
||||
/// ``` rust
|
||||
/// # use chrono::{DurationRound, TimeDelta, Utc, NaiveDate};
|
||||
/// let dt = NaiveDate::from_ymd_opt(2018, 1, 11).unwrap().and_hms_milli_opt(12, 0, 0, 154).unwrap().and_local_timezone(Utc).unwrap();
|
||||
/// let dt = NaiveDate::from_ymd_opt(2018, 1, 11)
|
||||
/// .unwrap()
|
||||
/// .and_hms_milli_opt(12, 0, 0, 154)
|
||||
/// .unwrap()
|
||||
/// .and_local_timezone(Utc)
|
||||
/// .unwrap();
|
||||
/// assert_eq!(
|
||||
/// dt.duration_round(TimeDelta::milliseconds(10)).unwrap().to_string(),
|
||||
/// "2018-01-11 12:00:00.150 UTC"
|
||||
@ -127,7 +142,12 @@ pub trait DurationRound: Sized {
|
||||
/// # Example
|
||||
/// ``` rust
|
||||
/// # use chrono::{DurationRound, TimeDelta, Utc, NaiveDate};
|
||||
/// let dt = NaiveDate::from_ymd_opt(2018, 1, 11).unwrap().and_hms_milli_opt(12, 0, 0, 154).unwrap().and_local_timezone(Utc).unwrap();
|
||||
/// let dt = NaiveDate::from_ymd_opt(2018, 1, 11)
|
||||
/// .unwrap()
|
||||
/// .and_hms_milli_opt(12, 0, 0, 154)
|
||||
/// .unwrap()
|
||||
/// .and_local_timezone(Utc)
|
||||
/// .unwrap();
|
||||
/// assert_eq!(
|
||||
/// dt.duration_trunc(TimeDelta::milliseconds(10)).unwrap().to_string(),
|
||||
/// "2018-01-11 12:00:00.150 UTC"
|
||||
@ -238,7 +258,12 @@ pub enum RoundingError {
|
||||
///
|
||||
/// ``` rust
|
||||
/// # use chrono::{DurationRound, TimeDelta, RoundingError, Utc, NaiveDate};
|
||||
/// let dt = NaiveDate::from_ymd_opt(2260, 12, 31).unwrap().and_hms_nano_opt(23, 59, 59, 1_75_500_000).unwrap().and_local_timezone(Utc).unwrap();
|
||||
/// let dt = NaiveDate::from_ymd_opt(2260, 12, 31)
|
||||
/// .unwrap()
|
||||
/// .and_hms_nano_opt(23, 59, 59, 1_75_500_000)
|
||||
/// .unwrap()
|
||||
/// .and_local_timezone(Utc)
|
||||
/// .unwrap();
|
||||
///
|
||||
/// assert_eq!(
|
||||
/// dt.duration_round(TimeDelta::days(300 * 365)),
|
||||
|
@ -101,7 +101,7 @@ pub trait Datelike: Sized {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{NaiveDate, Datelike};
|
||||
/// use chrono::{Datelike, NaiveDate};
|
||||
///
|
||||
/// assert_eq!(
|
||||
/// NaiveDate::from_ymd_opt(2020, 5, 13).unwrap().with_year(2023).unwrap(),
|
||||
@ -134,7 +134,7 @@ pub trait Datelike: Sized {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{NaiveDate, Datelike};
|
||||
/// use chrono::{Datelike, NaiveDate};
|
||||
///
|
||||
/// assert_eq!(
|
||||
/// NaiveDate::from_ymd_opt(2023, 5, 12).unwrap().with_month(9).unwrap(),
|
||||
@ -146,7 +146,7 @@ pub trait Datelike: Sized {
|
||||
///
|
||||
/// Don't combine multiple `Datelike::with_*` methods. The intermediate value may not exist.
|
||||
/// ```
|
||||
/// use chrono::{NaiveDate, Datelike};
|
||||
/// use chrono::{Datelike, NaiveDate};
|
||||
///
|
||||
/// fn with_year_month(date: NaiveDate, year: i32, month: u32) -> Option<NaiveDate> {
|
||||
/// date.with_year(year)?.with_month(month)
|
||||
@ -238,7 +238,7 @@ pub trait Datelike: Sized {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{NaiveDate, Datelike};
|
||||
/// use chrono::{Datelike, NaiveDate};
|
||||
///
|
||||
/// assert_eq!(NaiveDate::from_ymd_opt(1970, 1, 1).unwrap().num_days_from_ce(), 719_163);
|
||||
/// assert_eq!(NaiveDate::from_ymd_opt(2, 1, 1).unwrap().num_days_from_ce(), 366);
|
||||
|
Loading…
x
Reference in New Issue
Block a user