Skip to content

Commit

Permalink
Avoid use of deprecated API
Browse files Browse the repository at this point in the history
  • Loading branch information
djc committed Nov 9, 2022
1 parent 77317d5 commit 22b4d32
Show file tree
Hide file tree
Showing 21 changed files with 574 additions and 393 deletions.
18 changes: 16 additions & 2 deletions benches/chrono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}

Expand Down
2 changes: 1 addition & 1 deletion ci/core-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
1 change: 1 addition & 0 deletions src/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
147 changes: 49 additions & 98 deletions src/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand All @@ -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]
Expand All @@ -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]
Expand Down Expand Up @@ -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>> {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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),
Expand All @@ -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");
/// ```
Expand Down Expand Up @@ -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");
/// ```
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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);
Expand All @@ -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())
Expand All @@ -1268,32 +1236,25 @@ 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!(
norm(&fixed_from_str(r#""2014-07-24T13:57:06+01:23""#).ok()),
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()
))
);
Expand All @@ -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());
Expand All @@ -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()
);
}
Loading

0 comments on commit 22b4d32

Please sign in to comment.