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::from_local() #572

Merged
merged 4 commits into from Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -17,6 +17,7 @@ Versions with only mechanical changes will be omitted from the following list.
* Add support for microseconds timestamps serde serialization/deserialization (#304)
* Fix `DurationRound` is not TZ aware (#495)
* Implement `DurationRound` for `NaiveDateTime`
* Add `DateTime::from_local()` to construct from given local date and time
Milo123459 marked this conversation as resolved.
Show resolved Hide resolved

## 0.4.19

Expand Down
47 changes: 47 additions & 0 deletions src/datetime.rs
Expand Up @@ -96,6 +96,33 @@ impl<Tz: TimeZone> DateTime<Tz> {
DateTime { datetime: datetime, offset: offset }
}

/// Makes a new `DateTime` with given **local** datetime and offset that
/// presents local timezone.
///
/// # Example
///
/// ```
/// let naivedatetime_utc = NaiveDate::from_ymd(2000, 1, 12).and_hms(2, 0, 0);
/// let datetime_utc = DateTime::<Utc>::from_utc(naivedatetime_utc, Utc);
///
/// let timezone_east = FixedOffset::east(8 * 60 * 60);
/// let naivedatetime_east = NaiveDate::from_ymd(2000, 1, 12).and_hms(10, 0, 0);
/// let datetime_east = DateTime::<FixedOffset>::from_local(naivedatetime_east, timezone_east);
///
/// let timezone_west = FixedOffset::west(7 * 60 * 60);
/// let naivedatetime_west = NaiveDate::from_ymd(2000, 1, 11).and_hms(19, 0, 0);
/// 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]
pub fn from_local(datetime: NaiveDateTime, offset: Tz::Offset) ->DateTime<Tz> {
let datetime_utc = datetime - offset.fix();

DateTime { datetime: datetime_utc, offset: offset }
}

/// Retrieves a date component
///
/// Unless you are immediately planning on turning this into a `DateTime`
Expand Down Expand Up @@ -2842,4 +2869,24 @@ mod tests {
assert_eq!(format!("{} ", ymd_formatted), format!("{:<17}", ymd));
assert_eq!(format!(" {} ", ymd_formatted), format!("{:^17}", ymd));
}

#[test]
fn test_datetime_from_local() {
// 2000-01-12T02:00:00Z
let naivedatetime_utc = NaiveDate::from_ymd(2000, 1, 12).and_hms(2, 0, 0);
let datetime_utc = DateTime::<Utc>::from_utc(naivedatetime_utc, Utc);

// 2000-01-12T10:00:00+8:00:00
let timezone_east = FixedOffset::east(8 * 60 * 60);
let naivedatetime_east = NaiveDate::from_ymd(2000, 1, 12).and_hms(10, 0, 0);
let datetime_east = DateTime::<FixedOffset>::from_local(naivedatetime_east, timezone_east);

// 2000-01-11T19:00:00-7:00:00
let timezone_west = FixedOffset::west(7 * 60 * 60);
let naivedatetime_west = NaiveDate::from_ymd(2000, 1, 11).and_hms(19, 0, 0);
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));
}
}