Avoid use of deprecated API

This commit is contained in:
Dirkjan Ochtman 2022-10-20 12:40:19 +02:00
parent 77317d5255
commit 22b4d32498
21 changed files with 575 additions and 394 deletions

View File

@ -36,13 +36,27 @@ fn bench_datetime_from_str(c: &mut Criterion) {
fn bench_datetime_to_rfc2822(c: &mut Criterion) {
let pst = FixedOffset::east_opt(8 * 60 * 60).unwrap();
let dt = pst.ymd_opt(2018, 1, 11).unwrap().and_hms_nano_opt(10, 5, 13, 84_660_000).unwrap();
let dt = pst
.from_local_datetime(
&NaiveDate::from_ymd_opt(2018, 1, 11)
.unwrap()
.and_hms_nano_opt(10, 5, 13, 84_660_000)
.unwrap(),
)
.unwrap();
c.bench_function("bench_datetime_to_rfc2822", |b| b.iter(|| black_box(dt).to_rfc2822()));
}
fn bench_datetime_to_rfc3339(c: &mut Criterion) {
let pst = FixedOffset::east_opt(8 * 60 * 60).unwrap();
let dt = pst.ymd_opt(2018, 1, 11).and_hms_nano_opt(10, 5, 13, 84_660_000).unwrap();
let dt = pst
.from_local_datetime(
&NaiveDate::from_ymd_opt(2018, 1, 11)
.unwrap()
.and_hms_nano_opt(10, 5, 13, 84_660_000)
.unwrap(),
)
.unwrap();
c.bench_function("bench_datetime_to_rfc3339", |b| b.iter(|| black_box(dt).to_rfc3339()));
}

View File

@ -3,5 +3,5 @@
use chrono::{TimeZone, Utc};
pub fn create_time() {
let _ = Utc.ymd_opt(2019, 1, 1).unwrap().and_hms_opt(0, 0, 0).unwrap();
let _ = Utc.with_ymd_and_hms(2019, 1, 1, 0, 0, 0).unwrap();
}

View File

@ -2,6 +2,7 @@
// See README.md and LICENSE.txt for details.
//! ISO 8601 calendar date with time zone.
#![allow(deprecated)]
#[cfg(any(feature = "alloc", feature = "std", test))]
use core::borrow::Borrow;

View File

@ -30,6 +30,7 @@ use crate::naive::{Days, IsoWeek, NaiveDate, NaiveDateTime, NaiveTime};
use crate::offset::Local;
use crate::offset::{FixedOffset, Offset, TimeZone, Utc};
use crate::oldtime::Duration as OldDuration;
#[allow(deprecated)]
use crate::Date;
use crate::Months;
use crate::{Datelike, Timelike, Weekday};
@ -167,8 +168,8 @@ impl<Tz: TimeZone> DateTime<Tz> {
/// ```
/// use chrono::prelude::*;
///
/// let date: DateTime<Utc> = Utc.ymd_opt(2020, 1, 1).unwrap().and_hms_opt(0, 0, 0).unwrap();
/// let other: DateTime<FixedOffset> = FixedOffset::east_opt(23).unwrap().ymd_opt(2020, 1, 1).unwrap().and_hms_opt(0, 0, 0).unwrap();
/// 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();
/// assert_eq!(date.date_naive(), other.date_naive());
/// ```
#[inline]
@ -201,13 +202,12 @@ impl<Tz: TimeZone> DateTime<Tz> {
/// # Example
///
/// ```
/// use chrono::Utc;
/// use chrono::TimeZone;
/// use chrono::{Utc, TimeZone, NaiveDate};
///
/// let dt = Utc.ymd_opt(1970, 1, 1).unwrap().and_hms_milli_opt(0, 0, 1, 444).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 = Utc.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().and_local_timezone(Utc).unwrap();
/// assert_eq!(dt.timestamp_millis(), 1_000_000_000_555);
/// ```
#[inline]
@ -225,13 +225,12 @@ impl<Tz: TimeZone> DateTime<Tz> {
/// # Example
///
/// ```
/// use chrono::Utc;
/// use chrono::TimeZone;
/// use chrono::{Utc, TimeZone, NaiveDate};
///
/// let dt = Utc.ymd_opt(1970, 1, 1).unwrap().and_hms_micro_opt(0, 0, 1, 444).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 = Utc.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().and_local_timezone(Utc).unwrap();
/// assert_eq!(dt.timestamp_micros(), 1_000_000_000_000_555);
/// ```
#[inline]
@ -249,13 +248,12 @@ impl<Tz: TimeZone> DateTime<Tz> {
/// # Example
///
/// ```
/// use chrono::Utc;
/// use chrono::TimeZone;
/// use chrono::{Utc, TimeZone, NaiveDate};
///
/// let dt = Utc.ymd_opt(1970, 1, 1).unwrap().and_hms_nano_opt(0, 0, 1, 444).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(), 1_000_000_444);
///
/// let dt = Utc.ymd_opt(2001, 9, 9).unwrap().and_hms_nano_opt(1, 46, 40, 555).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(), 1_000_000_000_000_000_555);
/// ```
#[inline]
@ -529,10 +527,10 @@ impl DateTime<FixedOffset> {
/// representation of times in HTTP and email headers.
///
/// ```
/// # use chrono::{DateTime, FixedOffset, TimeZone};
/// # use chrono::{DateTime, FixedOffset, TimeZone, NaiveDate};
/// assert_eq!(
/// DateTime::parse_from_rfc2822("Wed, 18 Feb 2015 23:16:09 GMT").unwrap(),
/// FixedOffset::east_opt(0).unwrap().ymd_opt(2015, 2, 18).unwrap().and_hms_opt(23, 16, 9).unwrap()
/// FixedOffset::east_opt(0).unwrap().with_ymd_and_hms(2015, 2, 18, 23, 16, 9).unwrap()
/// );
/// ```
pub fn parse_from_rfc2822(s: &str) -> ParseResult<DateTime<FixedOffset>> {
@ -570,11 +568,11 @@ impl DateTime<FixedOffset> {
/// # Example
///
/// ```rust
/// use chrono::{DateTime, FixedOffset, TimeZone};
/// use chrono::{DateTime, FixedOffset, TimeZone, NaiveDate};
///
/// 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().ymd_opt(1983, 4, 13).unwrap().and_hms_milli_opt(12, 9, 14, 274).unwrap()));
/// 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();
@ -617,8 +615,8 @@ where
/// # Examples
///
/// ```rust
/// # use chrono::{DateTime, FixedOffset, SecondsFormat, TimeZone, Utc};
/// let dt = Utc.ymd_opt(2018, 1, 26).unwrap().and_hms_micro_opt(18, 30, 9, 453_829).unwrap();
/// # use chrono::{DateTime, 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),
@ -627,7 +625,7 @@ where
/// "2018-01-26T18:30:09Z");
///
/// let pst = FixedOffset::east_opt(8 * 60 * 60).unwrap();
/// let dt = pst.ymd_opt(2018, 1, 26).unwrap().and_hms_micro_opt(10, 30, 9, 453_829).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");
/// ```
@ -696,7 +694,7 @@ where
/// ```rust
/// use chrono::prelude::*;
///
/// let date_time: DateTime<Utc> = Utc.ymd_opt(2017, 04, 02).unwrap().and_hms_opt(12, 50, 32).unwrap();
/// let date_time: DateTime<Utc> = Utc.with_ymd_and_hms(2017, 04, 02, 12, 50, 32).unwrap();
/// let formatted = format!("{}", date_time.format("%d/%m/%Y %H:%M"));
/// assert_eq!(formatted, "02/04/2017 12:50");
/// ```
@ -880,8 +878,8 @@ impl<Tz: TimeZone, Tz2: TimeZone> PartialOrd<DateTime<Tz2>> for DateTime<Tz> {
/// ```
/// use chrono::prelude::*;
///
/// let earlier = Utc.ymd_opt(2015, 5, 15).unwrap().and_hms_opt(2, 0, 0).unwrap().with_timezone(&FixedOffset::west_opt(1 * 3600).unwrap());
/// let later = Utc.ymd_opt(2015, 5, 15).unwrap().and_hms_opt(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");
@ -1166,47 +1164,27 @@ where
#[test]
fn test_add_sub_months() {
let utc_dt = Utc.ymd_opt(2018, 9, 5).unwrap().and_hms_opt(23, 58, 0).unwrap();
assert_eq!(
utc_dt + Months::new(15),
Utc.ymd_opt(2019, 12, 5).unwrap().and_hms_opt(23, 58, 0).unwrap()
);
let utc_dt = Utc.with_ymd_and_hms(2018, 9, 5, 23, 58, 0).unwrap();
assert_eq!(utc_dt + Months::new(15), Utc.with_ymd_and_hms(2019, 12, 5, 23, 58, 0).unwrap());
let utc_dt = Utc.ymd_opt(2020, 1, 31).unwrap().and_hms_opt(23, 58, 0).unwrap();
assert_eq!(
utc_dt + Months::new(1),
Utc.ymd_opt(2020, 2, 29).unwrap().and_hms_opt(23, 58, 0).unwrap()
);
assert_eq!(
utc_dt + Months::new(2),
Utc.ymd_opt(2020, 3, 31).unwrap().and_hms_opt(23, 58, 0).unwrap()
);
let utc_dt = Utc.with_ymd_and_hms(2020, 1, 31, 23, 58, 0).unwrap();
assert_eq!(utc_dt + Months::new(1), Utc.with_ymd_and_hms(2020, 2, 29, 23, 58, 0).unwrap());
assert_eq!(utc_dt + Months::new(2), Utc.with_ymd_and_hms(2020, 3, 31, 23, 58, 0).unwrap());
let utc_dt = Utc.ymd_opt(2018, 9, 5).unwrap().and_hms_opt(23, 58, 0).unwrap();
assert_eq!(
utc_dt - Months::new(15),
Utc.ymd_opt(2017, 6, 5).unwrap().and_hms_opt(23, 58, 0).unwrap()
);
let utc_dt = Utc.with_ymd_and_hms(2018, 9, 5, 23, 58, 0).unwrap();
assert_eq!(utc_dt - Months::new(15), Utc.with_ymd_and_hms(2017, 6, 5, 23, 58, 0).unwrap());
let utc_dt = Utc.ymd_opt(2020, 3, 31).unwrap().and_hms_opt(23, 58, 0).unwrap();
assert_eq!(
utc_dt - Months::new(1),
Utc.ymd_opt(2020, 2, 29).unwrap().and_hms_opt(23, 58, 0).unwrap()
);
assert_eq!(
utc_dt - Months::new(2),
Utc.ymd_opt(2020, 1, 31).unwrap().and_hms_opt(23, 58, 0).unwrap()
);
let utc_dt = Utc.with_ymd_and_hms(2020, 3, 31, 23, 58, 0).unwrap();
assert_eq!(utc_dt - Months::new(1), Utc.with_ymd_and_hms(2020, 2, 29, 23, 58, 0).unwrap());
assert_eq!(utc_dt - Months::new(2), Utc.with_ymd_and_hms(2020, 1, 31, 23, 58, 0).unwrap());
}
#[test]
fn test_auto_conversion() {
let utc_dt = Utc.ymd_opt(2018, 9, 5).unwrap().and_hms_opt(23, 58, 0).unwrap();
let utc_dt = Utc.with_ymd_and_hms(2018, 9, 5, 23, 58, 0).unwrap();
let cdt_dt = FixedOffset::west_opt(5 * 60 * 60)
.unwrap()
.ymd_opt(2018, 9, 5)
.unwrap()
.and_hms_opt(18, 58, 0)
.with_ymd_and_hms(2018, 9, 5, 18, 58, 0)
.unwrap();
let utc_dt2: DateTime<Utc> = cdt_dt.into();
assert_eq!(utc_dt, utc_dt2);
@ -1220,30 +1198,20 @@ where
E: ::core::fmt::Debug,
{
assert_eq!(
to_string_utc(&Utc.ymd_opt(2014, 7, 24).unwrap().and_hms_opt(12, 34, 6).unwrap()).ok(),
to_string_utc(&Utc.with_ymd_and_hms(2014, 7, 24, 12, 34, 6).unwrap()).ok(),
Some(r#""2014-07-24T12:34:06Z""#.into())
);
assert_eq!(
to_string_fixed(
&FixedOffset::east_opt(3660)
.unwrap()
.ymd_opt(2014, 7, 24)
.unwrap()
.and_hms_opt(12, 34, 6)
.unwrap()
&FixedOffset::east_opt(3660).unwrap().with_ymd_and_hms(2014, 7, 24, 12, 34, 6).unwrap()
)
.ok(),
Some(r#""2014-07-24T12:34:06+01:01""#.into())
);
assert_eq!(
to_string_fixed(
&FixedOffset::east_opt(3650)
.unwrap()
.ymd_opt(2014, 7, 24)
.unwrap()
.and_hms_opt(12, 34, 6)
.unwrap()
&FixedOffset::east_opt(3650).unwrap().with_ymd_and_hms(2014, 7, 24, 12, 34, 6).unwrap()
)
.ok(),
Some(r#""2014-07-24T12:34:06+01:00:50""#.into())
@ -1268,22 +1236,17 @@ fn test_decodable_json<FUtc, FFixed, FLocal, E>(
assert_eq!(
norm(&utc_from_str(r#""2014-07-24T12:34:06Z""#).ok()),
norm(&Some(Utc.ymd_opt(2014, 7, 24).unwrap().and_hms_opt(12, 34, 6).unwrap()))
norm(&Some(Utc.with_ymd_and_hms(2014, 7, 24, 12, 34, 6).unwrap()))
);
assert_eq!(
norm(&utc_from_str(r#""2014-07-24T13:57:06+01:23""#).ok()),
norm(&Some(Utc.ymd_opt(2014, 7, 24).unwrap().and_hms_opt(12, 34, 6).unwrap()))
norm(&Some(Utc.with_ymd_and_hms(2014, 7, 24, 12, 34, 6).unwrap()))
);
assert_eq!(
norm(&fixed_from_str(r#""2014-07-24T12:34:06Z""#).ok()),
norm(&Some(
FixedOffset::east_opt(0)
.unwrap()
.ymd_opt(2014, 7, 24)
.unwrap()
.and_hms_opt(12, 34, 6)
.unwrap()
FixedOffset::east_opt(0).unwrap().with_ymd_and_hms(2014, 7, 24, 12, 34, 6).unwrap()
))
);
assert_eq!(
@ -1291,9 +1254,7 @@ fn test_decodable_json<FUtc, FFixed, FLocal, E>(
norm(&Some(
FixedOffset::east_opt(60 * 60 + 23 * 60)
.unwrap()
.ymd_opt(2014, 7, 24)
.unwrap()
.and_hms_opt(13, 57, 6)
.with_ymd_and_hms(2014, 7, 24, 13, 57, 6)
.unwrap()
))
);
@ -1302,11 +1263,11 @@ fn test_decodable_json<FUtc, FFixed, FLocal, E>(
// the conversion didn't change the instant itself
assert_eq!(
local_from_str(r#""2014-07-24T12:34:06Z""#).expect("local shouuld parse"),
Utc.ymd_opt(2014, 7, 24).unwrap().and_hms_opt(12, 34, 6).unwrap()
Utc.with_ymd_and_hms(2014, 7, 24, 12, 34, 6).unwrap()
);
assert_eq!(
local_from_str(r#""2014-07-24T13:57:06+01:23""#).expect("local should parse with offset"),
Utc.ymd_opt(2014, 7, 24).unwrap().and_hms_opt(12, 34, 6).unwrap()
Utc.with_ymd_and_hms(2014, 7, 24, 12, 34, 6).unwrap()
);
assert!(utc_from_str(r#""2014-07-32T12:34:06Z""#).is_err());
@ -1330,42 +1291,32 @@ fn test_decodable_json_timestamps<FUtc, FFixed, FLocal, E>(
assert_eq!(
norm(&utc_from_str("0").ok().map(DateTime::from)),
norm(&Some(Utc.ymd_opt(1970, 1, 1).unwrap().and_hms_opt(0, 0, 0).unwrap()))
norm(&Some(Utc.with_ymd_and_hms(1970, 1, 1, 0, 0, 0).unwrap()))
);
assert_eq!(
norm(&utc_from_str("-1").ok().map(DateTime::from)),
norm(&Some(Utc.ymd_opt(1969, 12, 31).unwrap().and_hms_opt(23, 59, 59).unwrap()))
norm(&Some(Utc.with_ymd_and_hms(1969, 12, 31, 23, 59, 59).unwrap()))
);
assert_eq!(
norm(&fixed_from_str("0").ok().map(DateTime::from)),
norm(&Some(
FixedOffset::east_opt(0)
.unwrap()
.ymd_opt(1970, 1, 1)
.unwrap()
.and_hms_opt(0, 0, 0)
.unwrap()
FixedOffset::east_opt(0).unwrap().with_ymd_and_hms(1970, 1, 1, 0, 0, 0).unwrap()
))
);
assert_eq!(
norm(&fixed_from_str("-1").ok().map(DateTime::from)),
norm(&Some(
FixedOffset::east_opt(0)
.unwrap()
.ymd_opt(1969, 12, 31)
.unwrap()
.and_hms_opt(23, 59, 59)
.unwrap()
FixedOffset::east_opt(0).unwrap().with_ymd_and_hms(1969, 12, 31, 23, 59, 59).unwrap()
))
);
assert_eq!(
*fixed_from_str("0").expect("0 timestamp should parse"),
Utc.ymd_opt(1970, 1, 1).unwrap().and_hms_opt(0, 0, 0).unwrap()
Utc.with_ymd_and_hms(1970, 1, 1, 0, 0, 0).unwrap()
);
assert_eq!(
*local_from_str("-1").expect("-1 timestamp should parse"),
Utc.ymd_opt(1969, 12, 31).unwrap().and_hms_opt(23, 59, 59).unwrap()
Utc.with_ymd_and_hms(1969, 12, 31, 23, 59, 59).unwrap()
);
}

View File

@ -122,7 +122,7 @@ impl<'de> de::Deserialize<'de> for DateTime<Local> {
/// # Example:
///
/// ```rust
/// # use chrono::{TimeZone, DateTime, Utc};
/// # use chrono::{TimeZone, DateTime, Utc, NaiveDate};
/// # use serde_derive::{Deserialize, Serialize};
/// use chrono::serde::ts_nanoseconds;
/// #[derive(Deserialize, Serialize)]
@ -131,7 +131,7 @@ impl<'de> de::Deserialize<'de> for DateTime<Local> {
/// time: DateTime<Utc>
/// }
///
/// let time = Utc.ymd_opt(2018, 5, 17).unwrap().and_hms_nano_opt(02, 04, 59, 918355733).unwrap();
/// 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(),
/// };
@ -158,7 +158,7 @@ pub mod ts_nanoseconds {
/// # Example:
///
/// ```rust
/// # use chrono::{TimeZone, DateTime, Utc};
/// # use chrono::{TimeZone, DateTime, Utc, NaiveDate};
/// # use serde_derive::Serialize;
/// use chrono::serde::ts_nanoseconds::serialize as to_nano_ts;
/// #[derive(Serialize)]
@ -168,7 +168,7 @@ pub mod ts_nanoseconds {
/// }
///
/// let my_s = S {
/// time: Utc.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().and_local_timezone(Utc).unwrap(),
/// };
/// let as_string = serde_json::to_string(&my_s)?;
/// assert_eq!(as_string, r#"{"time":1526522699918355733}"#);
@ -245,7 +245,7 @@ pub mod ts_nanoseconds {
/// # Example:
///
/// ```rust
/// # use chrono::{TimeZone, DateTime, Utc};
/// # use chrono::{TimeZone, DateTime, Utc, NaiveDate};
/// # use serde_derive::{Deserialize, Serialize};
/// use chrono::serde::ts_nanoseconds_option;
/// #[derive(Deserialize, Serialize)]
@ -254,7 +254,7 @@ pub mod ts_nanoseconds {
/// time: Option<DateTime<Utc>>
/// }
///
/// let time = Some(Utc.ymd_opt(2018, 5, 17).unwrap().and_hms_nano_opt(02, 04, 59, 918355733).unwrap());
/// 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(),
/// };
@ -280,7 +280,7 @@ pub mod ts_nanoseconds_option {
/// # Example:
///
/// ```rust
/// # use chrono::{TimeZone, DateTime, Utc};
/// # use chrono::{TimeZone, DateTime, Utc, NaiveDate};
/// # use serde_derive::Serialize;
/// use chrono::serde::ts_nanoseconds_option::serialize as to_nano_tsopt;
/// #[derive(Serialize)]
@ -290,7 +290,7 @@ pub mod ts_nanoseconds_option {
/// }
///
/// let my_s = S {
/// time: Some(Utc.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().and_local_timezone(Utc).unwrap()),
/// };
/// let as_string = serde_json::to_string(&my_s)?;
/// assert_eq!(as_string, r#"{"time":1526522699918355733}"#);
@ -374,7 +374,7 @@ pub mod ts_nanoseconds_option {
/// # Example:
///
/// ```rust
/// # use chrono::{TimeZone, DateTime, Utc};
/// # use chrono::{TimeZone, DateTime, Utc, NaiveDate};
/// # use serde_derive::{Deserialize, Serialize};
/// use chrono::serde::ts_microseconds;
/// #[derive(Deserialize, Serialize)]
@ -383,7 +383,7 @@ pub mod ts_nanoseconds_option {
/// time: DateTime<Utc>
/// }
///
/// let time = Utc.ymd_opt(2018, 5, 17).unwrap().and_hms_micro_opt(02, 04, 59, 918355).unwrap();
/// 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(),
/// };
@ -409,7 +409,7 @@ pub mod ts_microseconds {
/// # Example:
///
/// ```rust
/// # use chrono::{TimeZone, DateTime, Utc};
/// # use chrono::{TimeZone, DateTime, Utc, NaiveDate};
/// # use serde_derive::Serialize;
/// use chrono::serde::ts_microseconds::serialize as to_micro_ts;
/// #[derive(Serialize)]
@ -419,7 +419,7 @@ pub mod ts_microseconds {
/// }
///
/// let my_s = S {
/// time: Utc.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().and_local_timezone(Utc).unwrap(),
/// };
/// let as_string = serde_json::to_string(&my_s)?;
/// assert_eq!(as_string, r#"{"time":1526522699918355}"#);
@ -496,7 +496,7 @@ pub mod ts_microseconds {
/// # Example:
///
/// ```rust
/// # use chrono::{TimeZone, DateTime, Utc};
/// # use chrono::{TimeZone, DateTime, Utc, NaiveDate};
/// # use serde_derive::{Deserialize, Serialize};
/// use chrono::serde::ts_microseconds_option;
/// #[derive(Deserialize, Serialize)]
@ -505,7 +505,7 @@ pub mod ts_microseconds {
/// time: Option<DateTime<Utc>>
/// }
///
/// let time = Some(Utc.ymd_opt(2018, 5, 17).unwrap().and_hms_micro_opt(02, 04, 59, 918355).unwrap());
/// 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(),
/// };
@ -530,7 +530,7 @@ pub mod ts_microseconds_option {
/// # Example:
///
/// ```rust
/// # use chrono::{TimeZone, DateTime, Utc};
/// # use chrono::{TimeZone, DateTime, Utc, NaiveDate};
/// # use serde_derive::Serialize;
/// use chrono::serde::ts_microseconds_option::serialize as to_micro_tsopt;
/// #[derive(Serialize)]
@ -540,7 +540,7 @@ pub mod ts_microseconds_option {
/// }
///
/// let my_s = S {
/// time: Some(Utc.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().and_local_timezone(Utc).unwrap()),
/// };
/// let as_string = serde_json::to_string(&my_s)?;
/// assert_eq!(as_string, r#"{"time":1526522699918355}"#);
@ -624,7 +624,7 @@ pub mod ts_microseconds_option {
/// # Example
///
/// ```rust
/// # use chrono::{TimeZone, DateTime, Utc};
/// # use chrono::{TimeZone, DateTime, Utc, NaiveDate};
/// # use serde_derive::{Deserialize, Serialize};
/// use chrono::serde::ts_milliseconds;
/// #[derive(Deserialize, Serialize)]
@ -633,7 +633,7 @@ pub mod ts_microseconds_option {
/// time: DateTime<Utc>
/// }
///
/// let time = Utc.ymd_opt(2018, 5, 17).unwrap().and_hms_milli_opt(02, 04, 59, 918).unwrap();
/// 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(),
/// };
@ -659,7 +659,7 @@ pub mod ts_milliseconds {
/// # Example:
///
/// ```rust
/// # use chrono::{TimeZone, DateTime, Utc};
/// # use chrono::{TimeZone, DateTime, Utc, NaiveDate};
/// # use serde_derive::Serialize;
/// use chrono::serde::ts_milliseconds::serialize as to_milli_ts;
/// #[derive(Serialize)]
@ -669,7 +669,7 @@ pub mod ts_milliseconds {
/// }
///
/// let my_s = S {
/// time: Utc.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().and_local_timezone(Utc).unwrap(),
/// };
/// let as_string = serde_json::to_string(&my_s)?;
/// assert_eq!(as_string, r#"{"time":1526522699918}"#);
@ -743,7 +743,7 @@ pub mod ts_milliseconds {
/// # Example
///
/// ```rust
/// # use chrono::{TimeZone, DateTime, Utc};
/// # use chrono::{TimeZone, DateTime, Utc, NaiveDate};
/// # use serde_derive::{Deserialize, Serialize};
/// use chrono::serde::ts_milliseconds_option;
/// #[derive(Deserialize, Serialize)]
@ -752,7 +752,7 @@ pub mod ts_milliseconds {
/// time: Option<DateTime<Utc>>
/// }
///
/// let time = Some(Utc.ymd_opt(2018, 5, 17).unwrap().and_hms_milli_opt(02, 04, 59, 918).unwrap());
/// 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(),
/// };
@ -777,7 +777,7 @@ pub mod ts_milliseconds_option {
/// # Example:
///
/// ```rust
/// # use chrono::{TimeZone, DateTime, Utc};
/// # use chrono::{TimeZone, DateTime, Utc, NaiveDate};
/// # use serde_derive::Serialize;
/// use chrono::serde::ts_milliseconds_option::serialize as to_milli_tsopt;
/// #[derive(Serialize)]
@ -787,7 +787,7 @@ pub mod ts_milliseconds_option {
/// }
///
/// let my_s = S {
/// time: Some(Utc.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().and_local_timezone(Utc).unwrap()),
/// };
/// let as_string = serde_json::to_string(&my_s)?;
/// assert_eq!(as_string, r#"{"time":1526522699918}"#);
@ -893,7 +893,7 @@ pub mod ts_milliseconds_option {
/// time: DateTime<Utc>
/// }
///
/// let time = Utc.ymd_opt(2015, 5, 15).unwrap().and_hms_opt(10, 0, 0).unwrap();
/// let time = Utc.with_ymd_and_hms(2015, 5, 15, 10, 0, 0).unwrap();
/// let my_s = S {
/// time: time.clone(),
/// };
@ -929,7 +929,7 @@ pub mod ts_seconds {
/// }
///
/// let my_s = S {
/// time: Utc.ymd_opt(2015, 5, 15).unwrap().and_hms_opt(10, 0, 0).unwrap(),
/// 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}"#);
@ -1009,7 +1009,7 @@ pub mod ts_seconds {
/// time: Option<DateTime<Utc>>
/// }
///
/// let time = Some(Utc.ymd_opt(2015, 5, 15).unwrap().and_hms_opt(10, 0, 0).unwrap());
/// let time = Some(Utc.with_ymd_and_hms(2015, 5, 15, 10, 0, 0).unwrap());
/// let my_s = S {
/// time: time.clone(),
/// };
@ -1044,7 +1044,7 @@ pub mod ts_seconds_option {
/// }
///
/// let my_s = S {
/// time: Some(Utc.ymd_opt(2015, 5, 15).unwrap().and_hms_opt(10, 0, 0).unwrap()),
/// 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}"#);
@ -1142,7 +1142,7 @@ fn test_serde_bincode() {
// it is not self-describing.
use bincode::{deserialize, serialize};
let dt = Utc.ymd_opt(2014, 7, 24).unwrap().and_hms_opt(12, 34, 6).unwrap();
let dt = Utc.with_ymd_and_hms(2014, 7, 24, 12, 34, 6).unwrap();
let encoded = serialize(&dt).unwrap();
let decoded: DateTime<Utc> = deserialize(&encoded).unwrap();
assert_eq!(dt, decoded);

View File

@ -16,96 +16,90 @@ fn test_datetime_offset() {
let kst = FixedOffset::east_opt(9 * 60 * 60).unwrap();
assert_eq!(
format!("{}", Utc.ymd_opt(2014, 5, 6).unwrap().and_hms_opt(7, 8, 9).unwrap()),
format!("{}", Utc.with_ymd_and_hms(2014, 5, 6, 7, 8, 9).unwrap()),
"2014-05-06 07:08:09 UTC"
);
assert_eq!(
format!("{}", edt.ymd_opt(2014, 5, 6).unwrap().and_hms_opt(7, 8, 9).unwrap()),
format!("{}", edt.with_ymd_and_hms(2014, 5, 6, 7, 8, 9).unwrap()),
"2014-05-06 07:08:09 -04:00"
);
assert_eq!(
format!("{}", kst.ymd_opt(2014, 5, 6).unwrap().and_hms_opt(7, 8, 9).unwrap()),
format!("{}", kst.with_ymd_and_hms(2014, 5, 6, 7, 8, 9).unwrap()),
"2014-05-06 07:08:09 +09:00"
);
assert_eq!(
format!("{:?}", Utc.ymd_opt(2014, 5, 6).unwrap().and_hms_opt(7, 8, 9).unwrap()),
format!("{:?}", Utc.with_ymd_and_hms(2014, 5, 6, 7, 8, 9).unwrap()),
"2014-05-06T07:08:09Z"
);
assert_eq!(
format!("{:?}", edt.ymd_opt(2014, 5, 6).unwrap().and_hms_opt(7, 8, 9).unwrap()),
format!("{:?}", edt.with_ymd_and_hms(2014, 5, 6, 7, 8, 9).unwrap()),
"2014-05-06T07:08:09-04:00"
);
assert_eq!(
format!("{:?}", kst.ymd_opt(2014, 5, 6).unwrap().and_hms_opt(7, 8, 9).unwrap()),
format!("{:?}", kst.with_ymd_and_hms(2014, 5, 6, 7, 8, 9).unwrap()),
"2014-05-06T07:08:09+09:00"
);
// edge cases
assert_eq!(
format!("{:?}", Utc.ymd_opt(2014, 5, 6).unwrap().and_hms_opt(0, 0, 0).unwrap()),
format!("{:?}", Utc.with_ymd_and_hms(2014, 5, 6, 0, 0, 0).unwrap()),
"2014-05-06T00:00:00Z"
);
assert_eq!(
format!("{:?}", edt.ymd_opt(2014, 5, 6).unwrap().and_hms_opt(0, 0, 0).unwrap()),
format!("{:?}", edt.with_ymd_and_hms(2014, 5, 6, 0, 0, 0).unwrap()),
"2014-05-06T00:00:00-04:00"
);
assert_eq!(
format!("{:?}", kst.ymd_opt(2014, 5, 6).unwrap().and_hms_opt(0, 0, 0).unwrap()),
format!("{:?}", kst.with_ymd_and_hms(2014, 5, 6, 0, 0, 0).unwrap()),
"2014-05-06T00:00:00+09:00"
);
assert_eq!(
format!("{:?}", Utc.ymd_opt(2014, 5, 6).unwrap().and_hms_opt(23, 59, 59).unwrap()),
format!("{:?}", Utc.with_ymd_and_hms(2014, 5, 6, 23, 59, 59).unwrap()),
"2014-05-06T23:59:59Z"
);
assert_eq!(
format!("{:?}", edt.ymd_opt(2014, 5, 6).unwrap().and_hms_opt(23, 59, 59).unwrap()),
format!("{:?}", edt.with_ymd_and_hms(2014, 5, 6, 23, 59, 59).unwrap()),
"2014-05-06T23:59:59-04:00"
);
assert_eq!(
format!("{:?}", kst.ymd_opt(2014, 5, 6).unwrap().and_hms_opt(23, 59, 59).unwrap()),
format!("{:?}", kst.with_ymd_and_hms(2014, 5, 6, 23, 59, 59).unwrap()),
"2014-05-06T23:59:59+09:00"
);
let dt = Utc.ymd_opt(2014, 5, 6).unwrap().and_hms_opt(7, 8, 9).unwrap();
assert_eq!(dt, edt.ymd_opt(2014, 5, 6).unwrap().and_hms_opt(3, 8, 9).unwrap());
let dt = Utc.with_ymd_and_hms(2014, 5, 6, 7, 8, 9).unwrap();
assert_eq!(dt, edt.with_ymd_and_hms(2014, 5, 6, 3, 8, 9).unwrap());
assert_eq!(
dt + Duration::seconds(3600 + 60 + 1),
Utc.ymd_opt(2014, 5, 6).unwrap().and_hms_opt(8, 9, 10).unwrap()
Utc.with_ymd_and_hms(2014, 5, 6, 8, 9, 10).unwrap()
);
assert_eq!(
dt.signed_duration_since(edt.ymd_opt(2014, 5, 6).unwrap().and_hms_opt(10, 11, 12).unwrap()),
dt.signed_duration_since(edt.with_ymd_and_hms(2014, 5, 6, 10, 11, 12).unwrap()),
Duration::seconds(-7 * 3600 - 3 * 60 - 3)
);
assert_eq!(*Utc.ymd_opt(2014, 5, 6).unwrap().and_hms_opt(7, 8, 9).unwrap().offset(), Utc);
assert_eq!(*edt.ymd_opt(2014, 5, 6).unwrap().and_hms_opt(7, 8, 9).unwrap().offset(), edt);
assert!(*edt.ymd_opt(2014, 5, 6).unwrap().and_hms_opt(7, 8, 9).unwrap().offset() != est);
assert_eq!(*Utc.with_ymd_and_hms(2014, 5, 6, 7, 8, 9).unwrap().offset(), Utc);
assert_eq!(*edt.with_ymd_and_hms(2014, 5, 6, 7, 8, 9).unwrap().offset(), edt);
assert!(*edt.with_ymd_and_hms(2014, 5, 6, 7, 8, 9).unwrap().offset() != est);
}
#[test]
fn test_datetime_date_and_time() {
let tz = FixedOffset::east_opt(5 * 60 * 60).unwrap();
let d = tz.ymd_opt(2014, 5, 6).unwrap().and_hms_opt(7, 8, 9).unwrap();
let d = tz.with_ymd_and_hms(2014, 5, 6, 7, 8, 9).unwrap();
assert_eq!(d.time(), NaiveTime::from_hms_opt(7, 8, 9).unwrap());
assert_eq!(d.date(), tz.ymd_opt(2014, 5, 6).unwrap());
assert_eq!(d.date().naive_local(), NaiveDate::from_ymd_opt(2014, 5, 6).unwrap());
assert_eq!(d.date().and_time(d.time()), Some(d));
assert_eq!(d.date_naive(), NaiveDate::from_ymd_opt(2014, 5, 6).unwrap());
let tz = FixedOffset::east_opt(4 * 60 * 60).unwrap();
let d = tz.ymd_opt(2016, 5, 4).unwrap().and_hms_opt(3, 2, 1).unwrap();
let d = tz.with_ymd_and_hms(2016, 5, 4, 3, 2, 1).unwrap();
assert_eq!(d.time(), NaiveTime::from_hms_opt(3, 2, 1).unwrap());
assert_eq!(d.date(), tz.ymd_opt(2016, 5, 4).unwrap());
assert_eq!(d.date().naive_local(), NaiveDate::from_ymd_opt(2016, 5, 4).unwrap());
assert_eq!(d.date().and_time(d.time()), Some(d));
assert_eq!(d.date_naive(), NaiveDate::from_ymd_opt(2016, 5, 4).unwrap());
let tz = FixedOffset::west_opt(13 * 60 * 60).unwrap();
let d = tz.ymd_opt(2017, 8, 9).unwrap().and_hms_opt(12, 34, 56).unwrap();
let d = tz.with_ymd_and_hms(2017, 8, 9, 12, 34, 56).unwrap();
assert_eq!(d.time(), NaiveTime::from_hms_opt(12, 34, 56).unwrap());
assert_eq!(d.date(), tz.ymd_opt(2017, 8, 9).unwrap());
assert_eq!(d.date().naive_local(), NaiveDate::from_ymd_opt(2017, 8, 9).unwrap());
assert_eq!(d.date().and_time(d.time()), Some(d));
assert_eq!(d.date_naive(), NaiveDate::from_ymd_opt(2017, 8, 9).unwrap());
let utc_d = Utc.ymd_opt(2017, 8, 9).unwrap().and_hms_opt(12, 34, 56).unwrap();
let utc_d = Utc.with_ymd_and_hms(2017, 8, 9, 12, 34, 56).unwrap();
assert!(utc_d < d);
}
@ -122,73 +116,92 @@ fn test_datetime_with_timezone() {
fn test_datetime_rfc2822_and_rfc3339() {
let edt = FixedOffset::east_opt(5 * 60 * 60).unwrap();
assert_eq!(
Utc.ymd_opt(2015, 2, 18).unwrap().and_hms_opt(23, 16, 9).unwrap().to_rfc2822(),
Utc.with_ymd_and_hms(2015, 2, 18, 23, 16, 9).unwrap().to_rfc2822(),
"Wed, 18 Feb 2015 23:16:09 +0000"
);
assert_eq!(
Utc.ymd_opt(2015, 2, 18).unwrap().and_hms_opt(23, 16, 9).unwrap().to_rfc3339(),
Utc.with_ymd_and_hms(2015, 2, 18, 23, 16, 9).unwrap().to_rfc3339(),
"2015-02-18T23:16:09+00:00"
);
assert_eq!(
edt.ymd_opt(2015, 2, 18).unwrap().and_hms_milli_opt(23, 16, 9, 150).unwrap().to_rfc2822(),
edt.from_local_datetime(
&NaiveDate::from_ymd_opt(2015, 2, 18)
.unwrap()
.and_hms_milli_opt(23, 16, 9, 150)
.unwrap()
)
.unwrap()
.to_rfc2822(),
"Wed, 18 Feb 2015 23:16:09 +0500"
);
assert_eq!(
edt.ymd_opt(2015, 2, 18).unwrap().and_hms_milli_opt(23, 16, 9, 150).unwrap().to_rfc3339(),
edt.from_local_datetime(
&NaiveDate::from_ymd_opt(2015, 2, 18)
.unwrap()
.and_hms_milli_opt(23, 16, 9, 150)
.unwrap()
)
.unwrap()
.to_rfc3339(),
"2015-02-18T23:16:09.150+05:00"
);
assert_eq!(
edt.ymd_opt(2015, 2, 18)
.unwrap()
.and_hms_micro_opt(23, 59, 59, 1_234_567)
.unwrap()
.to_rfc2822(),
edt.from_local_datetime(
&NaiveDate::from_ymd_opt(2015, 2, 18)
.unwrap()
.and_hms_micro_opt(23, 59, 59, 1_234_567)
.unwrap()
)
.unwrap()
.to_rfc2822(),
"Wed, 18 Feb 2015 23:59:60 +0500"
);
assert_eq!(
edt.ymd_opt(2015, 2, 18)
.unwrap()
.and_hms_micro_opt(23, 59, 59, 1_234_567)
.unwrap()
.to_rfc3339(),
edt.from_local_datetime(
&NaiveDate::from_ymd_opt(2015, 2, 18)
.unwrap()
.and_hms_micro_opt(23, 59, 59, 1_234_567)
.unwrap()
)
.unwrap()
.to_rfc3339(),
"2015-02-18T23:59:60.234567+05:00"
);
assert_eq!(
DateTime::parse_from_rfc2822("Wed, 18 Feb 2015 23:16:09 +0000"),
Ok(FixedOffset::east_opt(0)
.unwrap()
.ymd_opt(2015, 2, 18)
.unwrap()
.and_hms_opt(23, 16, 9)
.unwrap())
Ok(FixedOffset::east_opt(0).unwrap().with_ymd_and_hms(2015, 2, 18, 23, 16, 9).unwrap())
);
assert_eq!(
DateTime::parse_from_rfc2822("Wed, 18 Feb 2015 23:16:09 -0000"),
Ok(FixedOffset::east_opt(0)
.unwrap()
.ymd_opt(2015, 2, 18)
.unwrap()
.and_hms_opt(23, 16, 9)
.unwrap())
Ok(FixedOffset::east_opt(0).unwrap().with_ymd_and_hms(2015, 2, 18, 23, 16, 9).unwrap())
);
assert_eq!(
DateTime::parse_from_rfc3339("2015-02-18T23:16:09Z"),
Ok(FixedOffset::east_opt(0)
.unwrap()
.ymd_opt(2015, 2, 18)
.unwrap()
.and_hms_opt(23, 16, 9)
.unwrap())
Ok(FixedOffset::east_opt(0).unwrap().with_ymd_and_hms(2015, 2, 18, 23, 16, 9).unwrap())
);
assert_eq!(
DateTime::parse_from_rfc2822("Wed, 18 Feb 2015 23:59:60 +0500"),
Ok(edt.ymd_opt(2015, 2, 18).unwrap().and_hms_milli_opt(23, 59, 59, 1_000).unwrap())
Ok(edt
.from_local_datetime(
&NaiveDate::from_ymd_opt(2015, 2, 18)
.unwrap()
.and_hms_milli_opt(23, 59, 59, 1_000)
.unwrap()
)
.unwrap())
);
assert!(DateTime::parse_from_rfc2822("31 DEC 262143 23:59 -2359").is_err());
assert_eq!(
DateTime::parse_from_rfc3339("2015-02-18T23:59:60.234567+05:00"),
Ok(edt.ymd_opt(2015, 2, 18).unwrap().and_hms_micro_opt(23, 59, 59, 1_234_567).unwrap())
Ok(edt
.from_local_datetime(
&NaiveDate::from_ymd_opt(2015, 2, 18)
.unwrap()
.and_hms_micro_opt(23, 59, 59, 1_234_567)
.unwrap()
)
.unwrap())
);
}
@ -196,7 +209,14 @@ fn test_datetime_rfc2822_and_rfc3339() {
fn test_rfc3339_opts() {
use crate::SecondsFormat::*;
let pst = FixedOffset::east_opt(8 * 60 * 60).unwrap();
let dt = pst.ymd_opt(2018, 1, 11).unwrap().and_hms_nano_opt(10, 5, 13, 84_660_000).unwrap();
let dt = pst
.from_local_datetime(
&NaiveDate::from_ymd_opt(2018, 1, 11)
.unwrap()
.and_hms_nano_opt(10, 5, 13, 84_660_000)
.unwrap(),
)
.unwrap();
assert_eq!(dt.to_rfc3339_opts(Secs, false), "2018-01-11T10:05:13+08:00");
assert_eq!(dt.to_rfc3339_opts(Secs, true), "2018-01-11T10:05:13+08:00");
assert_eq!(dt.to_rfc3339_opts(Millis, false), "2018-01-11T10:05:13.084+08:00");
@ -218,7 +238,7 @@ fn test_rfc3339_opts() {
#[should_panic]
fn test_rfc3339_opts_nonexhaustive() {
use crate::SecondsFormat;
let dt = Utc.ymd_opt(1999, 10, 9).unwrap().and_hms_opt(1, 2, 3).unwrap();
let dt = Utc.with_ymd_and_hms(1999, 10, 9, 1, 2, 3).unwrap();
dt.to_rfc3339_opts(SecondsFormat::__NonExhaustive, true);
}
@ -228,51 +248,95 @@ fn test_datetime_from_str() {
"2015-02-18T23:16:9.15Z".parse::<DateTime<FixedOffset>>(),
Ok(FixedOffset::east_opt(0)
.unwrap()
.ymd_opt(2015, 2, 18)
.unwrap()
.and_hms_milli_opt(23, 16, 9, 150)
.from_local_datetime(
&NaiveDate::from_ymd_opt(2015, 2, 18)
.unwrap()
.and_hms_milli_opt(23, 16, 9, 150)
.unwrap()
)
.unwrap())
);
assert_eq!(
"2015-02-18T23:16:9.15Z".parse::<DateTime<Utc>>(),
Ok(Utc.ymd_opt(2015, 2, 18).unwrap().and_hms_milli_opt(23, 16, 9, 150).unwrap())
Ok(Utc
.from_local_datetime(
&NaiveDate::from_ymd_opt(2015, 2, 18)
.unwrap()
.and_hms_milli_opt(23, 16, 9, 150)
.unwrap()
)
.unwrap())
);
assert_eq!(
"2015-02-18T23:16:9.15 UTC".parse::<DateTime<Utc>>(),
Ok(Utc.ymd_opt(2015, 2, 18).unwrap().and_hms_milli_opt(23, 16, 9, 150).unwrap())
Ok(Utc
.from_local_datetime(
&NaiveDate::from_ymd_opt(2015, 2, 18)
.unwrap()
.and_hms_milli_opt(23, 16, 9, 150)
.unwrap()
)
.unwrap())
);
assert_eq!(
"2015-02-18T23:16:9.15UTC".parse::<DateTime<Utc>>(),
Ok(Utc.ymd_opt(2015, 2, 18).unwrap().and_hms_milli_opt(23, 16, 9, 150).unwrap())
Ok(Utc
.from_local_datetime(
&NaiveDate::from_ymd_opt(2015, 2, 18)
.unwrap()
.and_hms_milli_opt(23, 16, 9, 150)
.unwrap()
)
.unwrap())
);
assert_eq!(
"2015-2-18T23:16:9.15Z".parse::<DateTime<FixedOffset>>(),
Ok(FixedOffset::east_opt(0)
.unwrap()
.ymd_opt(2015, 2, 18)
.unwrap()
.and_hms_milli_opt(23, 16, 9, 150)
.from_local_datetime(
&NaiveDate::from_ymd_opt(2015, 2, 18)
.unwrap()
.and_hms_milli_opt(23, 16, 9, 150)
.unwrap()
)
.unwrap())
);
assert_eq!(
"2015-2-18T13:16:9.15-10:00".parse::<DateTime<FixedOffset>>(),
Ok(FixedOffset::west_opt(10 * 3600)
.unwrap()
.ymd_opt(2015, 2, 18)
.unwrap()
.and_hms_milli_opt(13, 16, 9, 150)
.from_local_datetime(
&NaiveDate::from_ymd_opt(2015, 2, 18)
.unwrap()
.and_hms_milli_opt(13, 16, 9, 150)
.unwrap()
)
.unwrap())
);
assert!("2015-2-18T23:16:9.15".parse::<DateTime<FixedOffset>>().is_err());
assert_eq!(
"2015-2-18T23:16:9.15Z".parse::<DateTime<Utc>>(),
Ok(Utc.ymd_opt(2015, 2, 18).unwrap().and_hms_milli_opt(23, 16, 9, 150).unwrap())
Ok(Utc
.from_local_datetime(
&NaiveDate::from_ymd_opt(2015, 2, 18)
.unwrap()
.and_hms_milli_opt(23, 16, 9, 150)
.unwrap()
)
.unwrap())
);
assert_eq!(
"2015-2-18T13:16:9.15-10:00".parse::<DateTime<Utc>>(),
Ok(Utc.ymd_opt(2015, 2, 18).unwrap().and_hms_milli_opt(23, 16, 9, 150).unwrap())
Ok(Utc
.from_local_datetime(
&NaiveDate::from_ymd_opt(2015, 2, 18)
.unwrap()
.and_hms_milli_opt(23, 16, 9, 150)
.unwrap()
)
.unwrap())
);
assert!("2015-2-18T23:16:9.15".parse::<DateTime<Utc>>().is_err());
@ -282,7 +346,7 @@ fn test_datetime_from_str() {
#[test]
fn test_datetime_parse_from_str() {
let ymdhms = |y, m, d, h, n, s, off| {
FixedOffset::east_opt(off).unwrap().ymd_opt(y, m, d).unwrap().and_hms_opt(h, n, s).unwrap()
FixedOffset::east_opt(off).unwrap().with_ymd_and_hms(y, m, d, h, n, s).unwrap()
};
assert_eq!(
DateTime::parse_from_str("2014-5-7T12:34:56+09:30", "%Y-%m-%dT%H:%M:%S%z"),
@ -293,13 +357,13 @@ fn test_datetime_parse_from_str() {
.is_err());
assert_eq!(
Utc.datetime_from_str("Fri, 09 Aug 2013 23:54:35 GMT", "%a, %d %b %Y %H:%M:%S GMT"),
Ok(Utc.ymd_opt(2013, 8, 9).unwrap().and_hms_opt(23, 54, 35).unwrap())
Ok(Utc.with_ymd_and_hms(2013, 8, 9, 23, 54, 35).unwrap())
);
}
#[test]
fn test_to_string_round_trip() {
let dt = Utc.ymd_opt(2000, 1, 1).unwrap().and_hms_opt(0, 0, 0).unwrap();
let dt = Utc.with_ymd_and_hms(2000, 1, 1, 0, 0, 0).unwrap();
let _dt: DateTime<Utc> = dt.to_string().parse().unwrap();
let ndt_fixed = dt.with_timezone(&FixedOffset::east_opt(3600).unwrap());
@ -349,7 +413,14 @@ fn test_datetime_is_send() {
#[test]
fn test_subsecond_part() {
let datetime = Utc.ymd_opt(2014, 7, 8).unwrap().and_hms_nano_opt(9, 10, 11, 1234567).unwrap();
let datetime = Utc
.from_local_datetime(
&NaiveDate::from_ymd_opt(2014, 7, 8)
.unwrap()
.and_hms_nano_opt(9, 10, 11, 1234567)
.unwrap(),
)
.unwrap();
assert_eq!(1, datetime.timestamp_subsec_millis());
assert_eq!(1234, datetime.timestamp_subsec_micros());
@ -361,31 +432,52 @@ fn test_subsecond_part() {
fn test_from_system_time() {
use std::time::Duration;
let epoch = Utc.ymd_opt(1970, 1, 1).unwrap().and_hms_opt(0, 0, 0).unwrap();
let epoch = Utc.with_ymd_and_hms(1970, 1, 1, 0, 0, 0).unwrap();
let nanos = 999_999_999;
// SystemTime -> DateTime<Utc>
assert_eq!(DateTime::<Utc>::from(UNIX_EPOCH), epoch);
assert_eq!(
DateTime::<Utc>::from(UNIX_EPOCH + Duration::new(999_999_999, nanos)),
Utc.ymd_opt(2001, 9, 9).unwrap().and_hms_nano_opt(1, 46, 39, nanos).unwrap()
Utc.from_local_datetime(
&NaiveDate::from_ymd_opt(2001, 9, 9)
.unwrap()
.and_hms_nano_opt(1, 46, 39, nanos)
.unwrap()
)
.unwrap()
);
assert_eq!(
DateTime::<Utc>::from(UNIX_EPOCH - Duration::new(999_999_999, nanos)),
Utc.ymd_opt(1938, 4, 24).unwrap().and_hms_nano_opt(22, 13, 20, 1).unwrap()
Utc.from_local_datetime(
&NaiveDate::from_ymd_opt(1938, 4, 24).unwrap().and_hms_nano_opt(22, 13, 20, 1).unwrap()
)
.unwrap()
);
// DateTime<Utc> -> SystemTime
assert_eq!(SystemTime::from(epoch), UNIX_EPOCH);
assert_eq!(
SystemTime::from(
Utc.ymd_opt(2001, 9, 9).unwrap().and_hms_nano_opt(1, 46, 39, nanos).unwrap()
Utc.from_local_datetime(
&NaiveDate::from_ymd_opt(2001, 9, 9)
.unwrap()
.and_hms_nano_opt(1, 46, 39, nanos)
.unwrap()
)
.unwrap()
),
UNIX_EPOCH + Duration::new(999_999_999, nanos)
);
assert_eq!(
SystemTime::from(
Utc.ymd_opt(1938, 4, 24).unwrap().and_hms_nano_opt(22, 13, 20, 1).unwrap()
Utc.from_local_datetime(
&NaiveDate::from_ymd_opt(1938, 4, 24)
.unwrap()
.and_hms_nano_opt(22, 13, 20, 1)
.unwrap()
)
.unwrap()
),
UNIX_EPOCH - Duration::new(999_999_999, 999_999_999)
);
@ -412,30 +504,54 @@ fn test_from_system_time() {
let nanos = 999_999_000;
let epoch = Utc.ymd_opt(1970, 1, 1).unwrap().and_hms_opt(0, 0, 0).unwrap();
let epoch = Utc.with_ymd_and_hms(1970, 1, 1, 0, 0, 0).unwrap();
// SystemTime -> DateTime<Utc>
assert_eq!(DateTime::<Utc>::from(UNIX_EPOCH), epoch);
assert_eq!(
DateTime::<Utc>::from(UNIX_EPOCH + Duration::new(999_999_999, nanos)),
Utc.ymd_opt(2001, 9, 9).unwrap().and_hms_nano_opt(1, 46, 39, nanos).unwrap()
Utc.from_local_datetime(
&NaiveDate::from_ymd_opt(2001, 9, 9)
.unwrap()
.and_hms_nano_opt(1, 46, 39, nanos)
.unwrap()
)
.unwrap()
);
assert_eq!(
DateTime::<Utc>::from(UNIX_EPOCH - Duration::new(999_999_999, nanos)),
Utc.ymd_opt(1938, 4, 24).unwrap().and_hms_nano_opt(22, 13, 20, 1_000).unwrap()
Utc.from_local_datetime(
&NaiveDate::from_ymd_opt(1938, 4, 24)
.unwrap()
.and_hms_nano_opt(22, 13, 20, 1_000)
.unwrap()
)
.unwrap()
);
// DateTime<Utc> -> SystemTime
assert_eq!(SystemTime::from(epoch), UNIX_EPOCH);
assert_eq!(
SystemTime::from(
Utc.ymd_opt(2001, 9, 9).unwrap().and_hms_nano_opt(1, 46, 39, nanos).unwrap()
Utc.from_local_datetime(
&NaiveDate::from_ymd_opt(2001, 9, 9)
.unwrap()
.and_hms_nano_opt(1, 46, 39, nanos)
.unwrap()
)
.unwrap()
),
UNIX_EPOCH + Duration::new(999_999_999, nanos)
);
assert_eq!(
SystemTime::from(
Utc.ymd_opt(1938, 4, 24).unwrap().and_hms_nano_opt(22, 13, 20, 1_000).unwrap()
Utc.from_local_datetime(
&NaiveDate::from_ymd_opt(1938, 4, 24)
.unwrap()
.and_hms_nano_opt(22, 13, 20, 1_000)
.unwrap()
)
.unwrap()
),
UNIX_EPOCH - Duration::new(999_999_999, nanos)
);
@ -457,7 +573,7 @@ fn test_from_system_time() {
#[test]
fn test_datetime_format_alignment() {
let datetime = Utc.ymd_opt(2007, 1, 2).unwrap();
let datetime = Utc.with_ymd_and_hms(2007, 1, 2, 0, 0, 0).unwrap();
// Item::Literal
let percent = datetime.format("%%");
@ -514,16 +630,18 @@ fn test_years_elapsed() {
const WEEKS_PER_YEAR: f32 = 52.1775;
// This is always at least one year because 1 year = 52.1775 weeks.
let one_year_ago = Utc::today() - Duration::weeks((WEEKS_PER_YEAR * 1.5).ceil() as i64);
let one_year_ago =
Utc::now().date_naive() - Duration::weeks((WEEKS_PER_YEAR * 1.5).ceil() as i64);
// A bit more than 2 years.
let two_year_ago = Utc::today() - Duration::weeks((WEEKS_PER_YEAR * 2.5).ceil() as i64);
let two_year_ago =
Utc::now().date_naive() - Duration::weeks((WEEKS_PER_YEAR * 2.5).ceil() as i64);
assert_eq!(Utc::today().years_since(one_year_ago), Some(1));
assert_eq!(Utc::today().years_since(two_year_ago), Some(2));
assert_eq!(Utc::now().date_naive().years_since(one_year_ago), Some(1));
assert_eq!(Utc::now().date_naive().years_since(two_year_ago), Some(2));
// If the given DateTime is later than now, the function will always return 0.
let future = Utc::today() + Duration::weeks(12);
assert_eq!(Utc::today().years_since(future), None);
let future = Utc::now().date_naive() + Duration::weeks(12);
assert_eq!(Utc::now().date_naive().years_since(future), None);
}
#[test]

View File

@ -20,7 +20,7 @@
//! # use std::error::Error;
//! use chrono::prelude::*;
//!
//! let date_time = Utc.ymd_opt(2020, 11, 10).unwrap().and_hms_opt(0, 1, 32).unwrap();
//! let date_time = Utc.with_ymd_and_hms(2020, 11, 10, 0, 1, 32).unwrap();
//!
//! let formatted = format!("{}", date_time.format("%Y-%m-%d %H:%M:%S"));
//! assert_eq!(formatted, "2020-11-10 00:01:32");

View File

@ -892,7 +892,7 @@ fn parse_rfc850() {
static RFC850_FMT: &str = "%A, %d-%b-%y %T GMT";
let dt_str = "Sunday, 06-Nov-94 08:49:37 GMT";
let dt = Utc.ymd_opt(1994, 11, 6).unwrap().and_hms_opt(8, 49, 37).unwrap();
let dt = Utc.with_ymd_and_hms(1994, 11, 6, 8, 49, 37).unwrap();
// Check that the format is what we expect
assert_eq!(dt.format(RFC850_FMT).to_string(), dt_str);
@ -903,28 +903,19 @@ fn parse_rfc850() {
// Check that the rest of the weekdays parse correctly (this test originally failed because
// Sunday parsed incorrectly).
let testdates = [
(Utc.with_ymd_and_hms(1994, 11, 7, 8, 49, 37).unwrap(), "Monday, 07-Nov-94 08:49:37 GMT"),
(Utc.with_ymd_and_hms(1994, 11, 8, 8, 49, 37).unwrap(), "Tuesday, 08-Nov-94 08:49:37 GMT"),
(
Utc.ymd_opt(1994, 11, 7).unwrap().and_hms_opt(8, 49, 37).unwrap(),
"Monday, 07-Nov-94 08:49:37 GMT",
),
(
Utc.ymd_opt(1994, 11, 8).unwrap().and_hms_opt(8, 49, 37).unwrap(),
"Tuesday, 08-Nov-94 08:49:37 GMT",
),
(
Utc.ymd_opt(1994, 11, 9).unwrap().and_hms_opt(8, 49, 37).unwrap(),
Utc.with_ymd_and_hms(1994, 11, 9, 8, 49, 37).unwrap(),
"Wednesday, 09-Nov-94 08:49:37 GMT",
),
(
Utc.ymd_opt(1994, 11, 10).unwrap().and_hms_opt(8, 49, 37).unwrap(),
Utc.with_ymd_and_hms(1994, 11, 10, 8, 49, 37).unwrap(),
"Thursday, 10-Nov-94 08:49:37 GMT",
),
(Utc.with_ymd_and_hms(1994, 11, 11, 8, 49, 37).unwrap(), "Friday, 11-Nov-94 08:49:37 GMT"),
(
Utc.ymd_opt(1994, 11, 11).unwrap().and_hms_opt(8, 49, 37).unwrap(),
"Friday, 11-Nov-94 08:49:37 GMT",
),
(
Utc.ymd_opt(1994, 11, 12).unwrap().and_hms_opt(8, 49, 37).unwrap(),
Utc.with_ymd_and_hms(1994, 11, 12, 8, 49, 37).unwrap(),
"Saturday, 12-Nov-94 08:49:37 GMT",
),
];

View File

@ -1181,9 +1181,12 @@ mod tests {
let ymdhmsn = |y, m, d, h, n, s, nano, off| {
Ok(FixedOffset::east_opt(off)
.unwrap()
.ymd_opt(y, m, d)
.unwrap()
.and_hms_nano_opt(h, n, s, nano)
.from_local_datetime(
&NaiveDate::from_ymd_opt(y, m, d)
.unwrap()
.and_hms_nano_opt(h, n, s, nano)
.unwrap(),
)
.unwrap())
};
@ -1228,7 +1231,14 @@ mod tests {
parse!(Utc;
year: 2014, ordinal: 365, hour_div_12: 0, hour_mod_12: 4,
minute: 26, second: 40, nanosecond: 12_345_678, offset: 0),
Ok(Utc.ymd_opt(2014, 12, 31).unwrap().and_hms_nano_opt(4, 26, 40, 12_345_678).unwrap())
Ok(Utc
.from_local_datetime(
&NaiveDate::from_ymd_opt(2014, 12, 31)
.unwrap()
.and_hms_nano_opt(4, 26, 40, 12_345_678)
.unwrap()
)
.unwrap())
);
assert_eq!(
parse!(Utc;
@ -1248,16 +1258,19 @@ mod tests {
minute: 26, second: 40, nanosecond: 12_345_678, offset: 32400),
Ok(FixedOffset::east_opt(32400)
.unwrap()
.ymd_opt(2014, 12, 31)
.unwrap()
.and_hms_nano_opt(13, 26, 40, 12_345_678)
.from_local_datetime(
&NaiveDate::from_ymd_opt(2014, 12, 31)
.unwrap()
.and_hms_nano_opt(13, 26, 40, 12_345_678)
.unwrap()
)
.unwrap())
);
// single result from timestamp
assert_eq!(
parse!(Utc; timestamp: 1_420_000_000, offset: 0),
Ok(Utc.ymd_opt(2014, 12, 31).unwrap().and_hms_opt(4, 26, 40).unwrap())
Ok(Utc.with_ymd_and_hms(2014, 12, 31, 4, 26, 40).unwrap())
);
assert_eq!(parse!(Utc; timestamp: 1_420_000_000, offset: 32400), Err(IMPOSSIBLE));
assert_eq!(
@ -1268,9 +1281,7 @@ mod tests {
parse!(FixedOffset::east_opt(32400).unwrap(); timestamp: 1_420_000_000, offset: 32400),
Ok(FixedOffset::east_opt(32400)
.unwrap()
.ymd_opt(2014, 12, 31)
.unwrap()
.and_hms_opt(13, 26, 40)
.with_ymd_and_hms(2014, 12, 31, 13, 26, 40)
.unwrap())
);

View File

@ -556,13 +556,17 @@ fn test_strftime_items() {
#[cfg(test)]
#[test]
fn test_strftime_docs() {
use crate::NaiveDate;
use crate::{DateTime, FixedOffset, TimeZone, Timelike, Utc};
let dt = FixedOffset::east_opt(34200)
.unwrap()
.ymd_opt(2001, 7, 8)
.unwrap()
.and_hms_nano_opt(0, 34, 59, 1_026_490_708)
.from_local_datetime(
&NaiveDate::from_ymd_opt(2001, 7, 8)
.unwrap()
.and_hms_nano_opt(0, 34, 59, 1_026_490_708)
.unwrap(),
)
.unwrap();
// date specifiers

View File

@ -126,26 +126,26 @@
//! use chrono::prelude::*;
//! use chrono::offset::LocalResult;
//!
//! let dt = Utc.ymd_opt(2014, 7, 8).unwrap().and_hms_opt(9, 10, 11).unwrap(); // `2014-07-08T09:10:11Z`
//! let dt = Utc.with_ymd_and_hms(2014, 7, 8, 9, 10, 11).unwrap(); // `2014-07-08T09:10:11Z`
//! // July 8 is 188th day of the year 2014 (`o` for "ordinal")
//! assert_eq!(dt, Utc.yo(2014, 189).and_hms_opt(9, 10, 11).unwrap());
//! // July 8 is Tuesday in ISO week 28 of the year 2014.
//! assert_eq!(dt, Utc.isoywd(2014, 28, Weekday::Tue).and_hms_opt(9, 10, 11).unwrap());
//!
//! let dt = Utc.ymd_opt(2014, 7, 8).unwrap().and_hms_milli_opt(9, 10, 11, 12).unwrap(); // `2014-07-08T09:10:11.012Z`
//! assert_eq!(dt, Utc.ymd_opt(2014, 7, 8).unwrap().and_hms_micro_opt(9, 10, 11, 12_000).unwrap());
//! assert_eq!(dt, Utc.ymd_opt(2014, 7, 8).unwrap().and_hms_nano_opt(9, 10, 11, 12_000_000).unwrap());
//! let dt = NaiveDate::from_ymd_opt(2014, 7, 8).unwrap().and_hms_milli_opt(9, 10, 11, 12).unwrap().and_local_timezone(Utc).unwrap(); // `2014-07-08T09:10:11.012Z`
//! assert_eq!(dt, NaiveDate::from_ymd_opt(2014, 7, 8).unwrap().and_hms_micro_opt(9, 10, 11, 12_000).unwrap().and_local_timezone(Utc).unwrap());
//! assert_eq!(dt, NaiveDate::from_ymd_opt(2014, 7, 8).unwrap().and_hms_nano_opt(9, 10, 11, 12_000_000).unwrap().and_local_timezone(Utc).unwrap());
//!
//! // dynamic verification
//! assert_eq!(Utc.ymd_opt(2014, 7, 8).and_hms_opt(21, 15, 33),
//! LocalResult::Single(Utc.ymd_opt(2014, 7, 8).unwrap().and_hms_opt(21, 15, 33).unwrap()));
//! LocalResult::Single(Utc.with_ymd_and_hms(2014, 7, 8, 21, 15, 33).unwrap()));
//! assert_eq!(Utc.ymd_opt(2014, 7, 8).and_hms_opt(80, 15, 33), LocalResult::None);
//! assert_eq!(Utc.ymd_opt(2014, 7, 38).and_hms_opt(21, 15, 33), LocalResult::None);
//!
//! // 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.ymd_opt(2014, 7, 8).unwrap().and_hms_milli_opt(9, 10, 11, 12).unwrap();
//! let fixed_dt = FixedOffset::east_opt(9 * 3600).unwrap().ymd_opt(2014, 7, 8).unwrap().and_hms_milli_opt(18, 10, 11, 12).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;
//! ```
@ -161,7 +161,7 @@
//! use chrono::Duration;
//!
//! // assume this returned `2014-11-28T21:45:59.324310806+09:00`:
//! let dt = FixedOffset::east_opt(9*3600).unwrap().ymd_opt(2014, 11, 28).unwrap().and_hms_nano_opt(21, 45, 59, 324310806).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));
@ -175,7 +175,7 @@
//! // 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), Utc.ymd_opt(2014, 11, 28).unwrap().and_hms_nano_opt(12, 45, 59, 324310806).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
@ -183,14 +183,14 @@
//! assert_eq!(dt.with_year(-300).unwrap().num_days_from_ce(), -109606); // November 29, 301 BCE
//!
//! // arithmetic operations
//! let dt1 = Utc.ymd_opt(2014, 11, 14).unwrap().and_hms_opt(8, 9, 10).unwrap();
//! let dt2 = Utc.ymd_opt(2014, 11, 14).unwrap().and_hms_opt(10, 9, 8).unwrap();
//! let dt1 = Utc.with_ymd_and_hms(2014, 11, 14, 8, 9, 10).unwrap();
//! let dt2 = Utc.with_ymd_and_hms(2014, 11, 14, 10, 9, 8).unwrap();
//! assert_eq!(dt1.signed_duration_since(dt2), Duration::seconds(-2 * 3600 + 2));
//! assert_eq!(dt2.signed_duration_since(dt1), Duration::seconds(2 * 3600 - 2));
//! assert_eq!(Utc.ymd_opt(1970, 1, 1).unwrap().and_hms_opt(0, 0, 0).unwrap() + Duration::seconds(1_000_000_000),
//! Utc.ymd_opt(2001, 9, 9).unwrap().and_hms_opt(1, 46, 40).unwrap());
//! assert_eq!(Utc.ymd_opt(1970, 1, 1).unwrap().and_hms_opt(0, 0, 0).unwrap() - Duration::seconds(1_000_000_000),
//! Utc.ymd_opt(1938, 4, 24).unwrap().and_hms_opt(22, 13, 20).unwrap());
//! assert_eq!(Utc.with_ymd_and_hms(1970, 1, 1, 0, 0, 0).unwrap() + Duration::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() - Duration::seconds(1_000_000_000),
//! Utc.with_ymd_and_hms(1938, 4, 24, 22, 13, 20).unwrap());
//! ```
//!
//! ### Formatting and Parsing
@ -221,7 +221,7 @@
//!
//! # #[cfg(feature = "unstable-locales")]
//! # fn test() {
//! let dt = Utc.ymd_opt(2014, 11, 28).unwrap().and_hms_opt(12, 0, 9).unwrap();
//! 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");
@ -233,7 +233,7 @@
//! assert_eq!(format!("{:?}", dt), "2014-11-28T12:00:09Z");
//!
//! // Note that milli/nanoseconds are only printed if they are non-zero
//! let dt_nano = Utc.ymd_opt(2014, 11, 28).unwrap().and_hms_nano_opt(12, 0, 9, 1).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(feature = "unstable-locales"))]
@ -273,7 +273,7 @@
//! ```rust
//! use chrono::prelude::*;
//!
//! let dt = Utc.ymd_opt(2014, 11, 28).unwrap().and_hms_opt(12, 0, 9).unwrap();
//! 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());
//!
//! // method 1
@ -343,7 +343,7 @@
//!
//! assert_eq!(Utc.ymd_opt(2014, 11, 28).unwrap().weekday(), Weekday::Fri);
//! assert_eq!(Utc.ymd_opt(2014, 11, 31), LocalResult::None);
//! assert_eq!(Utc.ymd_opt(2014, 11, 28).unwrap().and_hms_milli_opt(7, 8, 9, 10).unwrap().format("%H%M%S").to_string(),
//! assert_eq!(NaiveDate::from_ymd_opt(2014, 11, 28).unwrap().and_hms_milli_opt(7, 8, 9, 10).unwrap().and_local_timezone(Utc).unwrap().format("%H%M%S").to_string(),
//! "070809");
//! ```
//!
@ -429,6 +429,7 @@ doctest!("../README.md");
/// A convenience module appropriate for glob imports (`use chrono::prelude::*;`).
pub mod prelude {
#[doc(no_inline)]
#[allow(deprecated)]
pub use crate::Date;
#[cfg(feature = "clock")]
#[cfg_attr(docsrs, doc(cfg(feature = "clock")))]

View File

@ -12,7 +12,7 @@ use rkyv::{Archive, Deserialize, Serialize};
/// ```
/// use num_traits::FromPrimitive;
/// use chrono::prelude::*;
/// let date = Utc.ymd_opt(2019, 10, 28).unwrap().and_hms_opt(9, 10, 11).unwrap();
/// let date = Utc.with_ymd_and_hms(2019, 10, 28, 9, 10, 11).unwrap();
/// // `2019-10-28T09:10:11Z`
/// let month = Month::from_u32(date.month());
/// assert_eq!(month, Some(Month::October))
@ -21,7 +21,7 @@ use rkyv::{Archive, Deserialize, Serialize};
/// ```
/// # use chrono::prelude::*;
/// let month = Month::January;
/// let dt = Utc.ymd_opt(2019, month.number_from_month(), 28).unwrap().and_hms_opt(9, 10, 11).unwrap();
/// let dt = Utc.with_ymd_and_hms(2019, month.number_from_month(), 28, 9, 10, 11).unwrap();
/// assert_eq!((dt.year(), dt.month(), dt.day()), (2019, 1, 28));
/// ```
/// Allows mapping from and to month, from 1-January to 12-December.
@ -337,15 +337,11 @@ mod tests {
assert_eq!(dec_opt, Some(Month::December));
assert_eq!(no_month, None);
let date = Utc.ymd_opt(2019, 10, 28).unwrap().and_hms_opt(9, 10, 11).unwrap();
let date = Utc.with_ymd_and_hms(2019, 10, 28, 9, 10, 11).unwrap();
assert_eq!(Month::from_u32(date.month()), Some(Month::October));
let month = Month::January;
let dt = Utc
.ymd_opt(2019, month.number_from_month(), 28)
.unwrap()
.and_hms_opt(9, 10, 11)
.unwrap();
let dt = Utc.with_ymd_and_hms(2019, month.number_from_month(), 28, 9, 10, 11).unwrap();
assert_eq!((dt.year(), dt.month(), dt.day()), (2019, 1, 28));
}

View File

@ -1053,7 +1053,7 @@ fn test_serde_deserialize() {
// it is not self-describing.
#[test]
fn test_serde_bincode() {
use crate::naive::NaiveDate;
use crate::NaiveDate;
use bincode::{deserialize, serialize};
let dt = NaiveDate::from_ymd_opt(2016, 7, 8).unwrap().and_hms_milli_opt(9, 10, 48, 90).unwrap();
@ -1076,10 +1076,8 @@ fn test_serde_bincode_optional() {
two: Option<DateTime<Utc>>,
}
let expected = Test {
one: Some(1),
two: Some(Utc.ymd_opt(1970, 1, 1).unwrap().and_hms_opt(0, 1, 1).unwrap()),
};
let expected =
Test { one: Some(1), two: Some(Utc.with_ymd_and_hms(1970, 1, 1, 0, 1, 1).unwrap()) };
let bytes: Vec<u8> = serialize(&expected).unwrap();
let actual = deserialize::<Test>(&(bytes)).unwrap();

View File

@ -1,6 +1,6 @@
use super::NaiveDateTime;
use crate::naive::NaiveDate;
use crate::oldtime::Duration;
use crate::NaiveDate;
use crate::{Datelike, FixedOffset, Utc};
use std::i64;

View File

@ -80,7 +80,7 @@ mod tests;
///
/// let dt1 = NaiveDate::from_ymd_opt(2015, 7, 1).unwrap().and_hms_micro_opt(8, 59, 59, 1_000_000).unwrap();
///
/// let dt2 = Utc.ymd_opt(2015, 6, 30).unwrap().and_hms_nano_opt(23, 59, 59, 1_000_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 _ = (t, dt1, dt2);
/// ```
///
@ -161,9 +161,9 @@ mod tests;
/// will be represented as the second part being 60, as required by ISO 8601.
///
/// ```
/// use chrono::{Utc, TimeZone};
/// use chrono::{Utc, TimeZone, NaiveDate};
///
/// let dt = Utc.ymd_opt(2015, 6, 30).unwrap().and_hms_milli_opt(23, 59, 59, 1_000).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");
/// ```
///
@ -175,12 +175,12 @@ mod tests;
/// and would be read back to the next non-leap second.
///
/// ```
/// use chrono::{DateTime, Utc, TimeZone};
/// use chrono::{DateTime, Utc, TimeZone, NaiveDate};
///
/// let dt = Utc.ymd_opt(2015, 6, 30).unwrap().and_hms_milli_opt(23, 56, 4, 1_000).unwrap();
/// let dt = NaiveDate::from_ymd_opt(2015, 6, 30).unwrap().and_hms_milli_opt(23, 56, 4, 1_000).unwrap().and_local_timezone(Utc).unwrap();
/// assert_eq!(format!("{:?}", dt), "2015-06-30T23:56:05Z");
///
/// let dt = Utc.ymd_opt(2015, 6, 30).unwrap().and_hms_opt(23, 56, 5).unwrap();
/// let dt = Utc.with_ymd_and_hms(2015, 6, 30, 23, 56, 5).unwrap();
/// assert_eq!(format!("{:?}", dt), "2015-06-30T23:56:05Z");
/// assert_eq!(DateTime::parse_from_rfc3339("2015-06-30T23:56:05Z").unwrap(), dt);
/// ```

View File

@ -241,33 +241,41 @@ mod tests {
// starting from 0.3 we don't have an offset exceeding one day.
// this makes everything easier!
assert_eq!(
format!("{:?}", FixedOffset::east_opt(86399).unwrap().ymd_opt(2012, 2, 29).unwrap()),
"2012-02-29+23:59:59".to_string()
format!(
"{:?}",
FixedOffset::east_opt(86399)
.unwrap()
.with_ymd_and_hms(2012, 2, 29, 5, 6, 7)
.unwrap()
),
"2012-02-29T05:06:07+23:59:59".to_string()
);
assert_eq!(
format!(
"{:?}",
FixedOffset::east_opt(86399)
.unwrap()
.ymd_opt(2012, 2, 29)
.unwrap()
.and_hms_opt(5, 6, 7)
.with_ymd_and_hms(2012, 2, 29, 5, 6, 7)
.unwrap()
),
"2012-02-29T05:06:07+23:59:59".to_string()
);
assert_eq!(
format!("{:?}", FixedOffset::west_opt(86399).unwrap().ymd_opt(2012, 3, 4).unwrap()),
"2012-03-04-23:59:59".to_string()
);
assert_eq!(
format!(
"{:?}",
FixedOffset::west_opt(86399)
.unwrap()
.ymd_opt(2012, 3, 4)
.with_ymd_and_hms(2012, 3, 4, 5, 6, 7)
.unwrap()
.and_hms_opt(5, 6, 7)
),
"2012-03-04T05:06:07-23:59:59".to_string()
);
assert_eq!(
format!(
"{:?}",
FixedOffset::west_opt(86399)
.unwrap()
.with_ymd_and_hms(2012, 3, 4, 5, 6, 7)
.unwrap()
),
"2012-03-04T05:06:07-23:59:59".to_string()

View File

@ -9,6 +9,7 @@ use rkyv::{Archive, Deserialize, Serialize};
use super::fixed::FixedOffset;
use super::{LocalResult, TimeZone};
use crate::naive::{NaiveDate, NaiveDateTime};
#[allow(deprecated)]
use crate::{Date, DateTime};
// we don't want `stub.rs` when the target_os is not wasi or emscripten
@ -108,6 +109,7 @@ impl TimeZone for Local {
self.from_local_datetime(local).map(|datetime| *datetime.offset())
}
#[allow(deprecated)]
fn offset_from_utc_date(&self, utc: &NaiveDate) -> FixedOffset {
*self.from_utc_date(utc).offset()
}
@ -117,6 +119,7 @@ impl TimeZone for Local {
}
// override them for avoiding redundant works
#[allow(deprecated)]
fn from_local_date(&self, local: &NaiveDate) -> LocalResult<Date<Local>> {
// this sounds very strange, but required for keeping `TimeZone::ymd` sane.
// in the other words, we use the offset at the local midnight
@ -149,6 +152,7 @@ impl TimeZone for Local {
inner::naive_to_local(local, true)
}
#[allow(deprecated)]
fn from_utc_date(&self, utc: &NaiveDate) -> Date<Local> {
let midnight = self.from_utc_datetime(&utc.and_hms_opt(0, 0, 0).unwrap());
Date::from_utc(*utc, *midnight.offset())
@ -183,7 +187,7 @@ impl TimeZone for Local {
mod tests {
use super::Local;
use crate::offset::TimeZone;
use crate::{Datelike, Duration};
use crate::{Datelike, Duration, Utc};
#[test]
fn verify_correct_offsets() {
@ -231,13 +235,13 @@ mod tests {
#[test]
fn test_local_date_sanity_check() {
// issue #27
assert_eq!(Local.ymd_opt(2999, 12, 28).unwrap().day(), 28);
assert_eq!(Local.with_ymd_and_hms(2999, 12, 28, 0, 0, 0).unwrap().day(), 28);
}
#[test]
fn test_leap_second() {
// issue #123
let today = Local::today();
let today = Utc::now().date_naive();
let dt = today.and_hms_milli_opt(1, 2, 59, 1000).unwrap();
let timestr = dt.time().to_string();

View File

@ -23,6 +23,7 @@ use core::fmt;
use crate::format::{parse, ParseResult, Parsed, StrftimeItems};
use crate::naive::{NaiveDate, NaiveDateTime, NaiveTime};
use crate::Weekday;
#[allow(deprecated)]
use crate::{Date, DateTime};
mod fixed;
@ -84,6 +85,7 @@ impl<T> LocalResult<T> {
}
}
#[allow(deprecated)]
impl<Tz: TimeZone> LocalResult<Date<Tz>> {
/// Makes a new `DateTime` from the current date and given `NaiveTime`.
/// The offset in the current date is preserved.

View File

@ -20,6 +20,7 @@ use rkyv::{Archive, Deserialize, Serialize};
use super::{FixedOffset, LocalResult, Offset, TimeZone};
use crate::naive::{NaiveDate, NaiveDateTime};
#[cfg(feature = "clock")]
#[allow(deprecated)]
use crate::{Date, DateTime};
/// The UTC time zone. This is the most efficient time zone when you don't need the local time.
@ -37,7 +38,7 @@ use crate::{Date, DateTime};
/// let dt = DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(61, 0), Utc);
///
/// assert_eq!(Utc.timestamp(61, 0), dt);
/// assert_eq!(Utc.ymd_opt(1970, 1, 1).unwrap().and_hms_opt(0, 1, 1).unwrap(), dt);
/// assert_eq!(Utc.with_ymd_and_hms(1970, 1, 1, 0, 1, 1).unwrap(), dt);
/// ```
#[derive(Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "rkyv", derive(Archive, Deserialize, Serialize))]

View File

@ -2,8 +2,8 @@
// See README.md and LICENSE.txt for details.
use crate::datetime::DateTime;
use crate::naive::NaiveDateTime;
use crate::oldtime::Duration;
use crate::NaiveDateTime;
use crate::TimeZone;
use crate::Timelike;
use core::cmp::Ordering;
@ -24,8 +24,8 @@ pub trait SubsecRound {
///
/// # Example
/// ``` rust
/// # use chrono::{DateTime, SubsecRound, Timelike, TimeZone, Utc};
/// let dt = Utc.ymd_opt(2018, 1, 11).unwrap().and_hms_milli_opt(12, 0, 0, 154).unwrap();
/// # use chrono::{DateTime, SubsecRound, Timelike, TimeZone, 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();
/// assert_eq!(dt.round_subsecs(2).nanosecond(), 150_000_000);
/// assert_eq!(dt.round_subsecs(1).nanosecond(), 200_000_000);
/// ```
@ -36,8 +36,8 @@ pub trait SubsecRound {
///
/// # Example
/// ``` rust
/// # use chrono::{DateTime, SubsecRound, Timelike, TimeZone, Utc};
/// let dt = Utc.ymd_opt(2018, 1, 11).unwrap().and_hms_milli_opt(12, 0, 0, 154).unwrap();
/// # use chrono::{DateTime, SubsecRound, Timelike, TimeZone, 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();
/// assert_eq!(dt.trunc_subsecs(2).nanosecond(), 150_000_000);
/// assert_eq!(dt.trunc_subsecs(1).nanosecond(), 100_000_000);
/// ```
@ -111,8 +111,8 @@ pub trait DurationRound: Sized {
///
/// # Example
/// ``` rust
/// # use chrono::{DateTime, DurationRound, Duration, TimeZone, Utc};
/// let dt = Utc.ymd_opt(2018, 1, 11).unwrap().and_hms_milli_opt(12, 0, 0, 154).unwrap();
/// # use chrono::{DateTime, DurationRound, Duration, TimeZone, 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();
/// assert_eq!(
/// dt.duration_round(Duration::milliseconds(10)).unwrap().to_string(),
/// "2018-01-11 12:00:00.150 UTC"
@ -128,8 +128,8 @@ pub trait DurationRound: Sized {
///
/// # Example
/// ``` rust
/// # use chrono::{DateTime, DurationRound, Duration, TimeZone, Utc};
/// let dt = Utc.ymd_opt(2018, 1, 11).unwrap().and_hms_milli_opt(12, 0, 0, 154).unwrap();
/// # use chrono::{DateTime, DurationRound, Duration, TimeZone, 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();
/// assert_eq!(
/// dt.duration_trunc(Duration::milliseconds(10)).unwrap().to_string(),
/// "2018-01-11 12:00:00.150 UTC"
@ -244,7 +244,7 @@ pub enum RoundingError {
///
/// ``` rust
/// # use chrono::{DateTime, DurationRound, Duration, RoundingError, TimeZone, Utc};
/// let dt = Utc.ymd_opt(1970, 12, 12).unwrap().and_hms_opt(0, 0, 0).unwrap();
/// let dt = Utc.with_ymd_and_hms(1970, 12, 12, 0, 0, 0).unwrap();
///
/// assert_eq!(
/// dt.duration_round(Duration::days(365)),
@ -256,8 +256,8 @@ pub enum RoundingError {
/// Error when `Duration.num_nanoseconds` exceeds the limit.
///
/// ``` rust
/// # use chrono::{DateTime, DurationRound, Duration, RoundingError, TimeZone, Utc};
/// let dt = Utc.ymd_opt(2260, 12, 31).unwrap().and_hms_nano_opt(23, 59, 59, 1_75_500_000).unwrap();
/// # use chrono::{DateTime, DurationRound, Duration, RoundingError, TimeZone, 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();
///
/// assert_eq!(
/// dt.duration_round(Duration::days(300 * 365)),
@ -270,7 +270,7 @@ pub enum RoundingError {
///
/// ``` rust
/// # use chrono::{DateTime, DurationRound, Duration, RoundingError, TimeZone, Utc};
/// let dt = Utc.ymd_opt(2300, 12, 12).unwrap().and_hms_opt(0, 0, 0).unwrap();
/// let dt = Utc.with_ymd_and_hms(2300, 12, 12, 0, 0, 0).unwrap();
///
/// assert_eq!(dt.duration_round(Duration::days(1)), Err(RoundingError::TimestampExceedsLimit),);
/// ```
@ -306,12 +306,20 @@ impl std::error::Error for RoundingError {
mod tests {
use super::{Duration, DurationRound, SubsecRound};
use crate::offset::{FixedOffset, TimeZone, Utc};
use crate::NaiveDate;
use crate::Timelike;
#[test]
fn test_round_subsecs() {
let pst = FixedOffset::east_opt(8 * 60 * 60).unwrap();
let dt = pst.ymd_opt(2018, 1, 11).unwrap().and_hms_nano_opt(10, 5, 13, 84_660_684).unwrap();
let dt = pst
.from_local_datetime(
&NaiveDate::from_ymd_opt(2018, 1, 11)
.unwrap()
.and_hms_nano_opt(10, 5, 13, 84_660_684)
.unwrap(),
)
.unwrap();
assert_eq!(dt.round_subsecs(10), dt);
assert_eq!(dt.round_subsecs(9), dt);
@ -327,8 +335,14 @@ mod tests {
assert_eq!(dt.round_subsecs(0).nanosecond(), 0);
assert_eq!(dt.round_subsecs(0).second(), 13);
let dt =
Utc.ymd_opt(2018, 1, 11).unwrap().and_hms_nano_opt(10, 5, 27, 750_500_000).unwrap();
let dt = Utc
.from_local_datetime(
&NaiveDate::from_ymd_opt(2018, 1, 11)
.unwrap()
.and_hms_nano_opt(10, 5, 27, 750_500_000)
.unwrap(),
)
.unwrap();
assert_eq!(dt.round_subsecs(9), dt);
assert_eq!(dt.round_subsecs(4), dt);
assert_eq!(dt.round_subsecs(3).nanosecond(), 751_000_000);
@ -341,8 +355,14 @@ mod tests {
#[test]
fn test_round_leap_nanos() {
let dt =
Utc.ymd_opt(2016, 12, 31).unwrap().and_hms_nano_opt(23, 59, 59, 1_750_500_000).unwrap();
let dt = Utc
.from_local_datetime(
&NaiveDate::from_ymd_opt(2016, 12, 31)
.unwrap()
.and_hms_nano_opt(23, 59, 59, 1_750_500_000)
.unwrap(),
)
.unwrap();
assert_eq!(dt.round_subsecs(9), dt);
assert_eq!(dt.round_subsecs(4), dt);
assert_eq!(dt.round_subsecs(2).nanosecond(), 1_750_000_000);
@ -356,7 +376,14 @@ mod tests {
#[test]
fn test_trunc_subsecs() {
let pst = FixedOffset::east_opt(8 * 60 * 60).unwrap();
let dt = pst.ymd_opt(2018, 1, 11).unwrap().and_hms_nano_opt(10, 5, 13, 84_660_684).unwrap();
let dt = pst
.from_local_datetime(
&NaiveDate::from_ymd_opt(2018, 1, 11)
.unwrap()
.and_hms_nano_opt(10, 5, 13, 84_660_684)
.unwrap(),
)
.unwrap();
assert_eq!(dt.trunc_subsecs(10), dt);
assert_eq!(dt.trunc_subsecs(9), dt);
@ -372,8 +399,14 @@ mod tests {
assert_eq!(dt.trunc_subsecs(0).nanosecond(), 0);
assert_eq!(dt.trunc_subsecs(0).second(), 13);
let dt =
pst.ymd_opt(2018, 1, 11).unwrap().and_hms_nano_opt(10, 5, 27, 750_500_000).unwrap();
let dt = pst
.from_local_datetime(
&NaiveDate::from_ymd_opt(2018, 1, 11)
.unwrap()
.and_hms_nano_opt(10, 5, 27, 750_500_000)
.unwrap(),
)
.unwrap();
assert_eq!(dt.trunc_subsecs(9), dt);
assert_eq!(dt.trunc_subsecs(4), dt);
assert_eq!(dt.trunc_subsecs(3).nanosecond(), 750_000_000);
@ -386,8 +419,14 @@ mod tests {
#[test]
fn test_trunc_leap_nanos() {
let dt =
Utc.ymd_opt(2016, 12, 31).unwrap().and_hms_nano_opt(23, 59, 59, 1_750_500_000).unwrap();
let dt = Utc
.from_local_datetime(
&NaiveDate::from_ymd_opt(2016, 12, 31)
.unwrap()
.and_hms_nano_opt(23, 59, 59, 1_750_500_000)
.unwrap(),
)
.unwrap();
assert_eq!(dt.trunc_subsecs(9), dt);
assert_eq!(dt.trunc_subsecs(4), dt);
assert_eq!(dt.trunc_subsecs(2).nanosecond(), 1_750_000_000);
@ -400,8 +439,14 @@ mod tests {
#[test]
fn test_duration_round() {
let dt =
Utc.ymd_opt(2016, 12, 31).unwrap().and_hms_nano_opt(23, 59, 59, 175_500_000).unwrap();
let dt = Utc
.from_local_datetime(
&NaiveDate::from_ymd_opt(2016, 12, 31)
.unwrap()
.and_hms_nano_opt(23, 59, 59, 175_500_000)
.unwrap(),
)
.unwrap();
assert_eq!(
dt.duration_round(Duration::zero()).unwrap().to_string(),
@ -414,13 +459,27 @@ mod tests {
);
// round up
let dt = Utc.ymd_opt(2012, 12, 12).unwrap().and_hms_milli_opt(18, 22, 30, 0).unwrap();
let dt = Utc
.from_local_datetime(
&NaiveDate::from_ymd_opt(2012, 12, 12)
.unwrap()
.and_hms_milli_opt(18, 22, 30, 0)
.unwrap(),
)
.unwrap();
assert_eq!(
dt.duration_round(Duration::minutes(5)).unwrap().to_string(),
"2012-12-12 18:25:00 UTC"
);
// round down
let dt = Utc.ymd_opt(2012, 12, 12).unwrap().and_hms_milli_opt(18, 22, 29, 999).unwrap();
let dt = Utc
.from_local_datetime(
&NaiveDate::from_ymd_opt(2012, 12, 12)
.unwrap()
.and_hms_milli_opt(18, 22, 29, 999)
.unwrap(),
)
.unwrap();
assert_eq!(
dt.duration_round(Duration::minutes(5)).unwrap().to_string(),
"2012-12-12 18:20:00 UTC"
@ -444,12 +503,8 @@ mod tests {
);
// timezone east
let dt = FixedOffset::east_opt(3600)
.unwrap()
.ymd_opt(2020, 10, 27)
.unwrap()
.and_hms_opt(15, 0, 0)
.unwrap();
let dt =
FixedOffset::east_opt(3600).unwrap().with_ymd_and_hms(2020, 10, 27, 15, 0, 0).unwrap();
assert_eq!(
dt.duration_round(Duration::days(1)).unwrap().to_string(),
"2020-10-28 00:00:00 +01:00"
@ -460,12 +515,8 @@ mod tests {
);
// timezone west
let dt = FixedOffset::west_opt(3600)
.unwrap()
.ymd_opt(2020, 10, 27)
.unwrap()
.and_hms_opt(15, 0, 0)
.unwrap();
let dt =
FixedOffset::west_opt(3600).unwrap().with_ymd_and_hms(2020, 10, 27, 15, 0, 0).unwrap();
assert_eq!(
dt.duration_round(Duration::days(1)).unwrap().to_string(),
"2020-10-28 00:00:00 -01:00"
@ -479,9 +530,12 @@ mod tests {
#[test]
fn test_duration_round_naive() {
let dt = Utc
.ymd_opt(2016, 12, 31)
.unwrap()
.and_hms_nano_opt(23, 59, 59, 175_500_000)
.from_local_datetime(
&NaiveDate::from_ymd_opt(2016, 12, 31)
.unwrap()
.and_hms_nano_opt(23, 59, 59, 175_500_000)
.unwrap(),
)
.unwrap()
.naive_utc();
@ -497,9 +551,12 @@ mod tests {
// round up
let dt = Utc
.ymd_opt(2012, 12, 12)
.unwrap()
.and_hms_milli_opt(18, 22, 30, 0)
.from_local_datetime(
&NaiveDate::from_ymd_opt(2012, 12, 12)
.unwrap()
.and_hms_milli_opt(18, 22, 30, 0)
.unwrap(),
)
.unwrap()
.naive_utc();
assert_eq!(
@ -508,9 +565,12 @@ mod tests {
);
// round down
let dt = Utc
.ymd_opt(2012, 12, 12)
.unwrap()
.and_hms_milli_opt(18, 22, 29, 999)
.from_local_datetime(
&NaiveDate::from_ymd_opt(2012, 12, 12)
.unwrap()
.and_hms_milli_opt(18, 22, 29, 999)
.unwrap(),
)
.unwrap()
.naive_utc();
assert_eq!(
@ -538,7 +598,7 @@ mod tests {
#[test]
fn test_duration_round_pre_epoch() {
let dt = Utc.ymd_opt(1969, 12, 12).unwrap().and_hms_opt(12, 12, 12).unwrap();
let dt = Utc.with_ymd_and_hms(1969, 12, 12, 12, 12, 12).unwrap();
assert_eq!(
dt.duration_round(Duration::minutes(10)).unwrap().to_string(),
"1969-12-12 12:10:00 UTC"
@ -547,8 +607,14 @@ mod tests {
#[test]
fn test_duration_trunc() {
let dt =
Utc.ymd_opt(2016, 12, 31).unwrap().and_hms_nano_opt(23, 59, 59, 175_500_000).unwrap();
let dt = Utc
.from_local_datetime(
&NaiveDate::from_ymd_opt(2016, 12, 31)
.unwrap()
.and_hms_nano_opt(23, 59, 59, 175_500_000)
.unwrap(),
)
.unwrap();
assert_eq!(
dt.duration_trunc(Duration::milliseconds(10)).unwrap().to_string(),
@ -556,13 +622,27 @@ mod tests {
);
// would round up
let dt = Utc.ymd_opt(2012, 12, 12).unwrap().and_hms_milli_opt(18, 22, 30, 0).unwrap();
let dt = Utc
.from_local_datetime(
&NaiveDate::from_ymd_opt(2012, 12, 12)
.unwrap()
.and_hms_milli_opt(18, 22, 30, 0)
.unwrap(),
)
.unwrap();
assert_eq!(
dt.duration_trunc(Duration::minutes(5)).unwrap().to_string(),
"2012-12-12 18:20:00 UTC"
);
// would round down
let dt = Utc.ymd_opt(2012, 12, 12).unwrap().and_hms_milli_opt(18, 22, 29, 999).unwrap();
let dt = Utc
.from_local_datetime(
&NaiveDate::from_ymd_opt(2012, 12, 12)
.unwrap()
.and_hms_milli_opt(18, 22, 29, 999)
.unwrap(),
)
.unwrap();
assert_eq!(
dt.duration_trunc(Duration::minutes(5)).unwrap().to_string(),
"2012-12-12 18:20:00 UTC"
@ -585,12 +665,8 @@ mod tests {
);
// timezone east
let dt = FixedOffset::east_opt(3600)
.unwrap()
.ymd_opt(2020, 10, 27)
.unwrap()
.and_hms_opt(15, 0, 0)
.unwrap();
let dt =
FixedOffset::east_opt(3600).unwrap().with_ymd_and_hms(2020, 10, 27, 15, 0, 0).unwrap();
assert_eq!(
dt.duration_trunc(Duration::days(1)).unwrap().to_string(),
"2020-10-27 00:00:00 +01:00"
@ -601,12 +677,8 @@ mod tests {
);
// timezone west
let dt = FixedOffset::west_opt(3600)
.unwrap()
.ymd_opt(2020, 10, 27)
.unwrap()
.and_hms_opt(15, 0, 0)
.unwrap();
let dt =
FixedOffset::west_opt(3600).unwrap().with_ymd_and_hms(2020, 10, 27, 15, 0, 0).unwrap();
assert_eq!(
dt.duration_trunc(Duration::days(1)).unwrap().to_string(),
"2020-10-27 00:00:00 -01:00"
@ -620,9 +692,12 @@ mod tests {
#[test]
fn test_duration_trunc_naive() {
let dt = Utc
.ymd_opt(2016, 12, 31)
.unwrap()
.and_hms_nano_opt(23, 59, 59, 175_500_000)
.from_local_datetime(
&NaiveDate::from_ymd_opt(2016, 12, 31)
.unwrap()
.and_hms_nano_opt(23, 59, 59, 175_500_000)
.unwrap(),
)
.unwrap()
.naive_utc();
@ -633,9 +708,12 @@ mod tests {
// would round up
let dt = Utc
.ymd_opt(2012, 12, 12)
.unwrap()
.and_hms_milli_opt(18, 22, 30, 0)
.from_local_datetime(
&NaiveDate::from_ymd_opt(2012, 12, 12)
.unwrap()
.and_hms_milli_opt(18, 22, 30, 0)
.unwrap(),
)
.unwrap()
.naive_utc();
assert_eq!(
@ -644,9 +722,12 @@ mod tests {
);
// would round down
let dt = Utc
.ymd_opt(2012, 12, 12)
.unwrap()
.and_hms_milli_opt(18, 22, 29, 999)
.from_local_datetime(
&NaiveDate::from_ymd_opt(2012, 12, 12)
.unwrap()
.and_hms_milli_opt(18, 22, 29, 999)
.unwrap(),
)
.unwrap()
.naive_utc();
assert_eq!(
@ -673,7 +754,7 @@ mod tests {
#[test]
fn test_duration_trunc_pre_epoch() {
let dt = Utc.ymd_opt(1969, 12, 12).unwrap().and_hms_opt(12, 12, 12).unwrap();
let dt = Utc.with_ymd_and_hms(1969, 12, 12, 12, 12, 12).unwrap();
assert_eq!(
dt.duration_trunc(Duration::minutes(10)).unwrap().to_string(),
"1969-12-12 12:10:00 UTC"

View File

@ -19,7 +19,7 @@ fn verify_against_date_command_local(path: &'static str, dt: NaiveDateTime) {
// seems to be consistent with the output of the `date` command, so we simply
// compare both.
// let local = Local
// .from_local_datetime(&NaiveDate::from_ymd_opt(year, month, day).unwrap().and_hms_opt(hour, 5, 1).unwrap())
// .with_ymd_and_hms(year, month, day, hour, 5, 1)
// // looks like the "date" command always returns a given time when it is ambiguous
// .earliest();