Skip to content

Commit

Permalink
0.2.3: Fixed a couple of outstanding bugs.
Browse files Browse the repository at this point in the history
- `DateTime<Tz>` and `Date<Tz>` is now `Copy`/`Send` when
  `Tz::Offset` is `Copy`/`Send`. The implementations for them were
  mistakenly omitted. Fixes #25.

- `Local::from_utc_datetime` didn't set a correct offset.
  The tests for `Local` were lacking. Fixes #26.
  • Loading branch information
lifthrasiir committed Feb 27, 2015
1 parent ffa8660 commit 2dbc11d
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 9 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ Chrono obeys the principle of [Semantic Versioning](http://semver.org/).
There were/are numerous minor versions before 1.0 due to the language changes.
Versions with only mechnical changes will be omitted from the following list.

## 0.2.3 (2015-02-27)

### Added

- `DateTime<Tz>` and `Date<Tz>` is now `Copy`/`Send` when `Tz::Offset` is `Copy`/`Send`.
The implementations for them were mistakenly omitted. (#25)

### Fixed

- `Local::from_utc_datetime` didn't set a correct offset. (#26)

## 0.2.1 (2015-02-21)

### Changed
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "chrono"
version = "0.2.2"
version = "0.2.3"
authors = ["Kang Seonghoon <[email protected]>"]

description = "Date and time library for Rust"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[Chrono][doc] 0.2.2
[Chrono][doc] 0.2.3
===================

[![Chrono on Travis CI][travis-image]][travis]
Expand Down
4 changes: 4 additions & 0 deletions src/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,10 @@ impl<Tz: TimeZone> Datelike for Date<Tz> {
}
}

// we need them as automatic impls cannot handle associated types
impl<Tz: TimeZone> Copy for Date<Tz> where <Tz as TimeZone>::Offset: Copy {}
unsafe impl<Tz: TimeZone> Send for Date<Tz> where <Tz as TimeZone>::Offset: Send {}

impl<Tz: TimeZone, Tz2: TimeZone> PartialEq<Date<Tz2>> for Date<Tz> {
fn eq(&self, other: &Date<Tz2>) -> bool { self.date == other.date }
}
Expand Down
32 changes: 32 additions & 0 deletions src/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,10 @@ impl<Tz: TimeZone> Timelike for DateTime<Tz> {
}
}

// we need them as automatic impls cannot handle associated types
impl<Tz: TimeZone> Copy for DateTime<Tz> where <Tz as TimeZone>::Offset: Copy {}
unsafe impl<Tz: TimeZone> Send for DateTime<Tz> where <Tz as TimeZone>::Offset: Send {}

impl<Tz: TimeZone, Tz2: TimeZone> PartialEq<DateTime<Tz2>> for DateTime<Tz> {
fn eq(&self, other: &DateTime<Tz2>) -> bool { self.datetime == other.datetime }
}
Expand Down Expand Up @@ -403,6 +407,14 @@ mod tests {
NaiveTime::from_hms(7, 8, 9));
}

#[test]
fn test_datetime_with_timezone() {
let local_now = Local::now();
let utc_now = local_now.with_timezone(&UTC);
let local_now2 = utc_now.with_timezone(&Local);
assert_eq!(local_now, local_now2);
}

#[test]
#[allow(non_snake_case)]
fn test_datetime_rfc2822_and_rfc3339() {
Expand Down Expand Up @@ -466,5 +478,25 @@ mod tests {
let dt = Local::now().with_month(5).unwrap();
assert_eq!(dt.format("%Y").to_string(), dt.with_timezone(&UTC).format("%Y").to_string());
}

#[test]
#[ignore] // XXX Rust issue #22818
fn test_datetime_is_copy() {
// UTC is known to be `Copy`.
let a = UTC::now();
let b = a;
assert_eq!(a, b);
}

#[test]
fn test_datetime_is_send() {
use std::thread;

// UTC is known to be `Send`.
let a = UTC::now();
thread::scoped(move || {
let _ = a;
}).join();
}
}

2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/*!
# Chrono 0.2.2
# Chrono 0.2.3
Date and time handling for Rust. (also known as `rust-chrono`)
It aims to be a feature-complete superset of the [time](https://github.com/rust-lang/time) library.
Expand Down
16 changes: 10 additions & 6 deletions src/offset/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ fn tm_to_datetime(mut tm: stdtime::Tm) -> DateTime<Local> {
}

/// Converts a local `NaiveDateTime` to the `time::Timespec`.
fn datetime_to_timespec(d: &NaiveDateTime) -> stdtime::Timespec {
fn datetime_to_timespec(d: &NaiveDateTime, local: bool) -> stdtime::Timespec {
// well, this exploits an undocumented `Tm::to_timespec` behavior
// to get the exact function we want (either `timegm` or `mktime`).
// the number 1 is arbitrary but should be non-zero to trigger `mktime`.
let tm_utcoff = if local {1} else {0};

let tm = stdtime::Tm {
tm_sec: d.second() as i32,
tm_min: d.minute() as i32,
Expand All @@ -46,8 +51,7 @@ fn datetime_to_timespec(d: &NaiveDateTime) -> stdtime::Timespec {
tm_wday: 0, // to_local ignores this
tm_yday: 0, // and this
tm_isdst: -1,
tm_utcoff: 1, // this is arbitrary but should be nonzero
// in order to make `to_timespec` use `rust_mktime` internally.
tm_utcoff: tm_utcoff,
tm_nsec: d.nanosecond() as i32,
};
tm.to_timespec()
Expand Down Expand Up @@ -94,16 +98,16 @@ impl TimeZone for Local {
self.from_local_datetime(&local.and_hms(0, 0, 0)).map(|datetime| datetime.date())
}
fn from_local_datetime(&self, local: &NaiveDateTime) -> LocalResult<DateTime<Local>> {
let timespec = datetime_to_timespec(local);
let timespec = datetime_to_timespec(local, true);
LocalResult::Single(tm_to_datetime(stdtime::at(timespec)))
}

fn from_utc_date(&self, utc: &NaiveDate) -> Date<Local> {
self.from_utc_datetime(&utc.and_hms(0, 0, 0)).date()
}
fn from_utc_datetime(&self, utc: &NaiveDateTime) -> DateTime<Local> {
let timespec = datetime_to_timespec(utc);
tm_to_datetime(stdtime::at_utc(timespec))
let timespec = datetime_to_timespec(utc, false);
tm_to_datetime(stdtime::at(timespec))
}
}

0 comments on commit 2dbc11d

Please sign in to comment.