Skip to content

Commit

Permalink
Deprecate DateTime::{from_local, from_utc}
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Jul 7, 2023
1 parent 9224444 commit 1ca1fa5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 30 deletions.
36 changes: 12 additions & 24 deletions src/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,43 +126,31 @@ impl<Tz: TimeZone> DateTime<Tz> {
DateTime { datetime, offset }
}

/// Makes a new `DateTime` from its components: a `NaiveDateTime` in UTC and an `Offset`.
#[inline]
#[must_use]
#[deprecated(
since = "0.4.27",
note = "Use TimeZone::from_utc_datetime() or DateTime::from_naive_utc_and_offset instead"
)]
pub fn from_utc(datetime: NaiveDateTime, offset: Tz::Offset) -> DateTime<Tz> {
DateTime { datetime, offset }
}

/// Makes a new `DateTime` with given **local** datetime and offset that
/// presents local timezone.
/// Makes a new `DateTime` from a `NaiveDateTime` in *local* time and an `Offset`.
///
/// # Panics
///
/// Panics if the local datetime can't be converted to UTC because it would be out of range.
///
/// This can happen if `datetime` is near the end of the representable range of `NaiveDateTime`,
/// and the offset from UTC pushes it beyond that.
///
/// # Example
///
/// ```
/// use chrono::DateTime;
/// use chrono::naive::NaiveDate;
/// use chrono::offset::{Utc, FixedOffset};
///
/// let naivedatetime_utc = NaiveDate::from_ymd_opt(2000, 1, 12).unwrap().and_hms_opt(2, 0, 0).unwrap();
/// let datetime_utc = DateTime::<Utc>::from_utc(naivedatetime_utc, Utc);
///
/// let timezone_east = FixedOffset::east_opt(8 * 60 * 60).unwrap();
/// let naivedatetime_east = NaiveDate::from_ymd_opt(2000, 1, 12).unwrap().and_hms_opt(10, 0, 0).unwrap();
/// let datetime_east = DateTime::<FixedOffset>::from_local(naivedatetime_east, timezone_east);
///
/// let timezone_west = FixedOffset::west_opt(7 * 60 * 60).unwrap();
/// let naivedatetime_west = NaiveDate::from_ymd_opt(2000, 1, 11).unwrap().and_hms_opt(19, 0, 0).unwrap();
/// let datetime_west = DateTime::<FixedOffset>::from_local(naivedatetime_west, timezone_west);

/// assert_eq!(datetime_east, datetime_utc.with_timezone(&timezone_east));
/// assert_eq!(datetime_west, datetime_utc.with_timezone(&timezone_west));
/// ```
#[inline]
#[must_use]
#[deprecated(
since = "0.4.27",
note = "Use TimeZone::from_local_datetime() or NaiveDateTime::and_local_timezone instead"
)]
pub fn from_local(datetime: NaiveDateTime, offset: Tz::Offset) -> DateTime<Tz> {
let datetime_utc = datetime - offset.fix();

Expand Down
7 changes: 4 additions & 3 deletions src/datetime/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ fn test_rfc3339_opts() {
assert_eq!(dt.to_rfc3339_opts(Nanos, false), "2018-01-11T10:05:13.084660000+08:00");
assert_eq!(dt.to_rfc3339_opts(AutoSi, false), "2018-01-11T10:05:13.084660+08:00");

let ut = DateTime::<Utc>::from_utc(dt.naive_utc(), Utc);
let ut = dt.naive_utc().and_utc();
assert_eq!(ut.to_rfc3339_opts(Secs, false), "2018-01-11T02:05:13+00:00");
assert_eq!(ut.to_rfc3339_opts(Secs, true), "2018-01-11T02:05:13Z");
assert_eq!(ut.to_rfc3339_opts(Millis, false), "2018-01-11T02:05:13.084+00:00");
Expand Down Expand Up @@ -783,6 +783,7 @@ fn test_datetime_format_alignment() {
}

#[test]
#[allow(deprecated)]
fn test_datetime_from_local() {
// 2000-01-12T02:00:00Z
let naivedatetime_utc =
Expand Down Expand Up @@ -828,7 +829,7 @@ fn test_years_elapsed() {
#[test]
fn test_datetime_add_assign() {
let naivedatetime = NaiveDate::from_ymd_opt(2000, 1, 1).unwrap().and_hms_opt(0, 0, 0).unwrap();
let datetime = DateTime::<Utc>::from_utc(naivedatetime, Utc);
let datetime = naivedatetime.and_utc();
let mut datetime_add = datetime;

datetime_add += Duration::seconds(60);
Expand Down Expand Up @@ -865,7 +866,7 @@ fn test_datetime_add_assign_local() {
#[test]
fn test_datetime_sub_assign() {
let naivedatetime = NaiveDate::from_ymd_opt(2000, 1, 1).unwrap().and_hms_opt(12, 0, 0).unwrap();
let datetime = DateTime::<Utc>::from_utc(naivedatetime, Utc);
let datetime = naivedatetime.and_utc();
let mut datetime_sub = datetime;

datetime_sub -= Duration::minutes(90);
Expand Down
6 changes: 3 additions & 3 deletions src/offset/utc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ use crate::{Date, DateTime};
/// # Example
///
/// ```
/// use chrono::{DateTime, TimeZone, NaiveDateTime, Utc};
/// use chrono::{TimeZone, NaiveDateTime, Utc};
///
/// let dt = DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp_opt(61, 0).unwrap(), Utc);
/// let dt = Utc.from_utc_datetime(&NaiveDateTime::from_timestamp_opt(61, 0).unwrap());
///
/// assert_eq!(Utc.timestamp_opt(61, 0).unwrap(), dt);
/// assert_eq!(Utc.with_ymd_and_hms(1970, 1, 1, 0, 1, 1).unwrap(), dt);
Expand Down Expand Up @@ -71,7 +71,7 @@ impl Utc {
SystemTime::now().duration_since(UNIX_EPOCH).expect("system time before Unix epoch");
let naive =
NaiveDateTime::from_timestamp_opt(now.as_secs() as i64, now.subsec_nanos()).unwrap();
DateTime::from_utc(naive, Utc)
Utc.from_utc_datetime(&naive)
}

/// Returns a `DateTime` which corresponds to the current date and time.
Expand Down

0 comments on commit 1ca1fa5

Please sign in to comment.