Skip to content

Commit

Permalink
Add DateTime::to_utc
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Sep 27, 2023
1 parent 0032431 commit 068b4bd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,14 @@ impl<Tz: TimeZone> DateTime<Tz> {
self.with_timezone(&self.offset().fix())
}

/// Turn this `DateTime` into a `DateTime<Utc>`, dropping the offset and associated timezone
/// information.
#[inline]
#[must_use]
pub fn to_utc(&self) -> DateTime<Utc> {
DateTime { datetime: self.datetime, offset: Utc }
}

/// Adds given `Duration` to the current date and time.
///
/// # Errors
Expand Down
8 changes: 8 additions & 0 deletions src/datetime/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1428,6 +1428,14 @@ fn test_datetime_fixed_offset() {
assert_eq!(datetime_fixed.fixed_offset(), datetime_fixed);
}

#[test]
fn test_datetime_to_utc() {
let dt =
FixedOffset::east_opt(3600).unwrap().with_ymd_and_hms(2020, 2, 22, 23, 24, 25).unwrap();
let dt_utc: DateTime<Utc> = dt.to_utc();
assert_eq!(dt, dt_utc);
}

#[test]
fn test_add_sub_months() {
let utc_dt = Utc.with_ymd_and_hms(2018, 9, 5, 23, 58, 0).unwrap();
Expand Down

0 comments on commit 068b4bd

Please sign in to comment.