Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DateTime::to_utc #1325

Merged
merged 1 commit into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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