Skip to content

Commit

Permalink
Merge pull request #572 from retrhelo/localdate
Browse files Browse the repository at this point in the history
  • Loading branch information
Milo123459 committed Oct 26, 2021
2 parents 7a357cd + 928a32f commit 1e49400
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
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 (#572)

## 0.4.19

Expand Down
51 changes: 51 additions & 0 deletions src/datetime.rs
Expand Up @@ -96,6 +96,37 @@ 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
///
/// ```
/// use chrono::DateTime;
/// use chrono::naive::NaiveDate;
/// use chrono::offset::{Utc, FixedOffset};
///
/// 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 @@ -2999,4 +3030,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));
}
}

0 comments on commit 1e49400

Please sign in to comment.