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

Updated Chrono to 0.4.0 #962

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 1 addition & 1 deletion diesel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ categories = ["database"]

[dependencies]
byteorder = "1.0"
chrono = { version = "0.3", optional = true }
chrono = { version = "0.4", optional = true }
clippy = { optional = true, version = "=0.0.126" }
libc = { version = "0.2.0", optional = true }
libsqlite3-sys = { version = ">=0.8.0, <0.9.0", optional = true, features = ["min_sqlite_version_3_7_16"] }
Expand Down
7 changes: 3 additions & 4 deletions diesel/src/mysql/types/date_and_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,7 @@ mod tests {
extern crate dotenv;
extern crate chrono;

use self::chrono::{Duration, NaiveDate, NaiveTime, UTC, TimeZone, FixedOffset};
use self::chrono::naive::date;
use self::chrono::{Duration, NaiveDate, NaiveTime, Utc, TimeZone, FixedOffset};
use self::dotenv::dotenv;

use expression::dsl::{sql, now};
Expand Down Expand Up @@ -169,11 +168,11 @@ mod tests {
#[test]
fn times_relative_to_now_encode_correctly() {
let connection = connection();
let time = UTC::now().naive_utc() + Duration::days(1);
let time = Utc::now().naive_utc() + Duration::days(1);
let query = select(now.lt(time));
assert!(query.get_result::<bool>(&connection).unwrap());

let time = UTC::now().naive_utc() - Duration::days(1);
let time = Utc::now().naive_utc() - Duration::days(1);
let query = select(now.gt(time));
assert!(query.get_result::<bool>(&connection).unwrap());
}
Expand Down
28 changes: 13 additions & 15 deletions diesel/src/pg/types/date_and_time/chrono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,22 @@ extern crate chrono;

use std::error::Error;
use std::io::Write;
use self::chrono::{Duration, NaiveDateTime, NaiveDate, NaiveTime, DateTime, TimeZone, UTC, FixedOffset, Local};
use self::chrono::naive::date;
use self::chrono::{Duration, naive, NaiveDateTime, NaiveDate, NaiveTime, DateTime, TimeZone, Utc, FixedOffset, Local};

use pg::Pg;
use super::{PgDate, PgTime, PgTimestamp};
use types::{Date, FromSql, IsNull, Time, Timestamp, Timestamptz, ToSql};

expression_impls! {
Timestamptz -> NaiveDateTime,
Timestamptz -> DateTime<UTC>,
Timestamptz -> DateTime<Utc>,
Timestamptz -> DateTime<FixedOffset>,
Timestamptz -> DateTime<Local>,
}

queryable_impls! {
Timestamptz -> NaiveDateTime,
Timestamptz -> DateTime<UTC>,
Timestamptz -> DateTime<Utc>,
}

// Postgres timestamps start from January 1st 2000.
Expand Down Expand Up @@ -66,10 +65,10 @@ impl ToSql<Timestamptz, Pg> for NaiveDateTime {
}
}

impl FromSql<Timestamptz, Pg> for DateTime<UTC> {
impl FromSql<Timestamptz, Pg> for DateTime<Utc> {
fn from_sql(bytes: Option<&[u8]>) -> Result<Self, Box<Error+Send+Sync>> {
let naive_date_time = try!(<NaiveDateTime as FromSql<Timestamptz, Pg>>::from_sql(bytes));
Ok(DateTime::from_utc(naive_date_time, UTC))
Ok(DateTime::from_utc(naive_date_time, Utc))
}
}

Expand Down Expand Up @@ -119,7 +118,7 @@ impl FromSql<Date, Pg> for NaiveDate {
Some(date) => Ok(date),
None => {
let error_message = format!("Chrono can only represent dates up to {:?}",
date::MAX);
naive::MAX_DATE);
Err(Box::<Error + Send + Sync>::from(error_message))
}
}
Expand All @@ -131,8 +130,7 @@ mod tests {
extern crate dotenv;
extern crate chrono;

use self::chrono::{Duration, NaiveDate, NaiveTime, UTC, TimeZone, FixedOffset};
use self::chrono::naive::date;
use self::chrono::{Duration, naive, NaiveDate, NaiveTime, Utc, TimeZone, FixedOffset};
use self::dotenv::dotenv;

use ::select;
Expand Down Expand Up @@ -161,7 +159,7 @@ mod tests {
#[test]
fn unix_epoch_encodes_correctly_with_utc_timezone() {
let connection = connection();
let time = UTC.ymd(1970, 1, 1).and_hms(0, 0, 0);
let time = Utc.ymd(1970, 1, 1).and_hms(0, 0, 0);
let query = select(sql::<Timestamptz>("'1970-01-01Z'::timestamptz").eq(time));
assert!(query.get_result::<bool>(&connection).unwrap());
}
Expand All @@ -186,7 +184,7 @@ mod tests {
#[test]
fn unix_epoch_decodes_correctly_with_timezone() {
let connection = connection();
let time = UTC.ymd(1970, 1, 1).and_hms(0, 0, 0);
let time = Utc.ymd(1970, 1, 1).and_hms(0, 0, 0);
let epoch_from_sql = select(sql::<Timestamptz>("'1970-01-01Z'::timestamptz"))
.get_result(&connection);
assert_eq!(Ok(time), epoch_from_sql);
Expand All @@ -195,11 +193,11 @@ mod tests {
#[test]
fn times_relative_to_now_encode_correctly() {
let connection = connection();
let time = UTC::now().naive_utc() + Duration::seconds(60);
let time = Utc::now().naive_utc() + Duration::seconds(60);
let query = select(now.at_time_zone("utc").lt(time));
assert!(query.get_result::<bool>(&connection).unwrap());

let time = UTC::now().naive_utc() - Duration::seconds(60);
let time = Utc::now().naive_utc() - Duration::seconds(60);
let query = select(now.at_time_zone("utc").gt(time));
assert!(query.get_result::<bool>(&connection).unwrap());
}
Expand Down Expand Up @@ -262,7 +260,7 @@ mod tests {
let query = select(sql::<Date>("'J0'::date").eq(julian_epoch));
assert!(query.get_result::<bool>(&connection).unwrap());

let max_date = date::MAX;
let max_date = naive::MAX_DATE;
let query = select(sql::<Date>("'262143-12-31'::date").eq(max_date));
assert!(query.get_result::<bool>(&connection).unwrap());

Expand Down Expand Up @@ -290,7 +288,7 @@ mod tests {
let query = select(sql::<Date>("'J0'::date"));
assert_eq!(Ok(julian_epoch), query.get_result::<NaiveDate>(&connection));

let max_date = date::MAX;
let max_date = naive::MAX_DATE;
let query = select(sql::<Date>("'262143-12-31'::date"));
assert_eq!(Ok(max_date), query.get_result::<NaiveDate>(&connection));

Expand Down
11 changes: 5 additions & 6 deletions diesel/src/sqlite/types/date_and_time/chrono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ mod tests {
extern crate dotenv;
extern crate chrono;

use self::chrono::{Duration, NaiveDate, NaiveTime, NaiveDateTime, UTC, Timelike};
use self::chrono::naive::date;
use self::chrono::{Duration, naive, NaiveDate, NaiveTime, NaiveDateTime, Utc, Timelike};
use self::dotenv::dotenv;

use ::select;
Expand Down Expand Up @@ -131,11 +130,11 @@ mod tests {
#[test]
fn times_relative_to_now_encode_correctly() {
let connection = connection();
let time = UTC::now().naive_utc() + Duration::seconds(60);
let time = Utc::now().naive_utc() + Duration::seconds(60);
let query = select(now.lt(time));
assert!(query.get_result::<bool>(&connection).unwrap());

let time = UTC::now().naive_utc() - Duration::seconds(600);
let time = Utc::now().naive_utc() - Duration::seconds(600);
let query = select(now.gt(time));
assert!(query.get_result::<bool>(&connection).unwrap());
}
Expand Down Expand Up @@ -184,7 +183,7 @@ mod tests {
let query = select(sql::<Date>("'-398-04-11'").eq(distant_past));
assert!(query.get_result::<bool>(&connection).unwrap());

let max_date = date::MAX;
let max_date = naive::MAX_DATE;
let query = select(sql::<Date>("'262143-12-31'").eq(max_date));
assert!(query.get_result::<bool>(&connection).unwrap());

Expand All @@ -208,7 +207,7 @@ mod tests {
let query = select(sql::<Date>("'-399-04-11'"));
assert_eq!(Ok(distant_past), query.get_result::<NaiveDate>(&connection));

let max_date = date::MAX;
let max_date = naive::MAX_DATE;
let query = select(sql::<Date>("'262143-12-31'"));
assert_eq!(Ok(max_date), query.get_result::<NaiveDate>(&connection));

Expand Down
2 changes: 1 addition & 1 deletion diesel_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ keywords = ["orm", "database", "postgres", "postgresql", "sql"]
name = "diesel"

[dependencies]
chrono = "0.3"
chrono = "0.4"
clap = "2.20.3"
diesel = { version = "0.13.0", default-features = false }
dotenv = ">=0.8, <0.11"
Expand Down
6 changes: 3 additions & 3 deletions diesel_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ fn run_migration_command(matches: &ArgMatches) {
// sort migrations by version timestamp, push non-conforming timestamped versions to the bottom
sorted.sort_by(|a, b| {
let version_a = a.0.split('_').nth(0).expect(&format!("Unexpected version format {:?}", a.0));
let ver_date_a = UTC.datetime_from_str(version_a, TIMESTAMP_FORMAT);
let ver_date_a = Utc.datetime_from_str(version_a, TIMESTAMP_FORMAT);
let version_b = b.0.split('_').nth(0).expect(&format!("Unexpected version format {:?}", b.0));
let ver_date_b = UTC.datetime_from_str(version_b, TIMESTAMP_FORMAT);
let ver_date_b = Utc.datetime_from_str(version_b, TIMESTAMP_FORMAT);
match (ver_date_a.is_ok(), ver_date_b.is_ok()) {
(false, false) => version_a.to_lowercase().cmp(&version_b.to_lowercase()),
(true, false) => Ordering::Less,
Expand Down Expand Up @@ -159,7 +159,7 @@ fn run_migration_command(matches: &ArgMatches) {
use std::fmt::Display;
fn migration_version<'a>(matches: &'a ArgMatches) -> Box<Display + 'a> {
matches.value_of("MIGRATION_VERSION").map(|s| Box::new(s) as Box<Display>)
.unwrap_or_else(|| Box::new(UTC::now().format(TIMESTAMP_FORMAT)))
.unwrap_or_else(|| Box::new(Utc::now().format(TIMESTAMP_FORMAT)))
}

fn migrations_dir(matches: &ArgMatches) -> PathBuf {
Expand Down
2 changes: 1 addition & 1 deletion diesel_cli/tests/migration_generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Creating migrations.\\d*_hello.down.sql\
let captured_timestamps = Regex::new(r"(?P<stamp>\d*)_hello").unwrap();
let mut stamps_found = 0;
for caps in captured_timestamps.captures_iter(result.stdout()) {
let timestamp = UTC.datetime_from_str(&caps["stamp"], TIMESTAMP_FORMAT);
let timestamp = Utc.datetime_from_str(&caps["stamp"], TIMESTAMP_FORMAT);
assert!(timestamp.is_ok(), "Found invalid timestamp format: {:?}", &caps["stamp"]);
stamps_found += 1;
}
Expand Down
14 changes: 7 additions & 7 deletions diesel_cli/tests/migration_list.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

use chrono::UTC;
use chrono::Utc;
use std::thread::sleep;
use std::time::Duration;

Expand Down Expand Up @@ -60,7 +60,7 @@ fn migration_list_lists_migrations_ordered_by_timestamp() {

p.command("setup").run();

let tag1 = format!("{}_initial", UTC::now().format(TIMESTAMP_FORMAT));
let tag1 = format!("{}_initial", Utc::now().format(TIMESTAMP_FORMAT));
p.create_migration(&tag1, "", "");

let result = p.command("migration")
Expand All @@ -71,7 +71,7 @@ fn migration_list_lists_migrations_ordered_by_timestamp() {

sleep(Duration::from_millis(1100));

let tag2 = format!("{}_alter", UTC::now().format(TIMESTAMP_FORMAT));
let tag2 = format!("{}_alter", Utc::now().format(TIMESTAMP_FORMAT));
p.create_migration(&tag2, "", "");

let result = p.command("migration")
Expand All @@ -90,7 +90,7 @@ fn migration_list_orders_unknown_timestamps_last() {

p.command("setup").run();

let tag1 = format!("{}_migration1", UTC::now().format(TIMESTAMP_FORMAT));
let tag1 = format!("{}_migration1", Utc::now().format(TIMESTAMP_FORMAT));
p.create_migration(&tag1, "", "");

let tag4 = "abc_migration4";
Expand All @@ -101,12 +101,12 @@ fn migration_list_orders_unknown_timestamps_last() {

sleep(Duration::from_millis(1100));

let tag2 = format!("{}_migration2", UTC::now().format(TIMESTAMP_FORMAT));
let tag2 = format!("{}_migration2", Utc::now().format(TIMESTAMP_FORMAT));
p.create_migration(&tag2, "", "");

sleep(Duration::from_millis(1100));

let tag3 = format!("{}_migration3", UTC::now().format(TIMESTAMP_FORMAT));
let tag3 = format!("{}_migration3", Utc::now().format(TIMESTAMP_FORMAT));
p.create_migration(&tag3, "", "");

let result = p.command("migration")
Expand Down Expand Up @@ -140,7 +140,7 @@ fn migration_list_orders_nontimestamp_versions_lexicographically() {
let tag3 = "7letters";
p.create_migration(&tag3, "", "");

let tag1 = format!("{}_stamped_migration", UTC::now().format(TIMESTAMP_FORMAT));
let tag1 = format!("{}_stamped_migration", Utc::now().format(TIMESTAMP_FORMAT));
p.create_migration(&tag1, "", "");

let result = p.command("migration")
Expand Down
2 changes: 1 addition & 1 deletion diesel_tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ dotenv = ">=0.8, <0.11"

[dependencies]
assert_matches = "1.0.1"
chrono = { version = "0.3" }
chrono = { version = "0.4" }
diesel = { path = "../diesel", default-features = false, features = ["quickcheck", "chrono", "uuid", "serde_json", "network-address"] }
diesel_codegen = { path = "../diesel_codegen" }
dotenv = ">=0.8, <0.11"
Expand Down
14 changes: 7 additions & 7 deletions diesel_tests/tests/types_roundtrip.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
extern crate chrono;

pub use quickcheck::quickcheck;
use self::chrono::{Duration, NaiveDate, NaiveDateTime, NaiveTime, DateTime, UTC};
use self::chrono::naive::date;
use self::chrono::{Duration, NaiveDate, NaiveDateTime, NaiveTime, DateTime, Utc};
use self::chrono::naive;

pub use schema::{connection, TestConnection};
pub use diesel::*;
Expand Down Expand Up @@ -154,14 +154,14 @@ pub fn mk_naive_time(data: (u32, u32)) -> NaiveTime {
NaiveTime::from_num_seconds_from_midnight(data.0, data.1 / 1000)
}

pub fn mk_datetime(data: (i64, u32)) -> DateTime<UTC> {
DateTime::from_utc(mk_naive_datetime(data), UTC)
pub fn mk_datetime(data: (i64, u32)) -> DateTime<Utc> {
DateTime::from_utc(mk_naive_datetime(data), Utc)
}

#[cfg(feature = "postgres")]
pub fn mk_naive_date(days: u32) -> NaiveDate {
let earliest_pg_date = NaiveDate::from_ymd(-4713, 11, 24);
let latest_chrono_date = date::MAX;
let latest_chrono_date = naive::MAX_DATE;
let num_days_representable = latest_chrono_date.signed_duration_since(earliest_pg_date).num_days();
earliest_pg_date + Duration::days(days as i64 % num_days_representable)
}
Expand All @@ -176,8 +176,8 @@ pub fn mk_naive_date(days: u32) -> NaiveDate {

#[cfg(feature = "sqlite")]
pub fn mk_naive_date(days: u32) -> NaiveDate {
let earliest_sqlite_date = date::MIN;
let latest_sqlite_date = date::MAX;
let earliest_sqlite_date = naive::MIN_DATE;
let latest_sqlite_date = naive::MAX_DATE;
let num_days_representable = latest_sqlite_date.signed_duration_since(earliest_sqlite_date).num_days();
earliest_sqlite_date + Duration::days(days as i64 % num_days_representable)
}
Expand Down