Skip to content

Commit

Permalink
Update chrono to 0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
Eijebong committed Jun 21, 2017
1 parent 983cdfc commit c100afc
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 35 deletions.
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
6 changes: 3 additions & 3 deletions diesel/src/mysql/types/date_and_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ mod tests {
extern crate dotenv;
extern crate chrono;

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

Expand Down Expand Up @@ -169,11 +169,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
23 changes: 11 additions & 12 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, NaiveDateTime, NaiveDate, NaiveTime, DateTime, TimeZone, Utc, FixedOffset, Local, MAX_DATE};

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);
MAX_DATE);
Err(Box::<Error + Send + Sync>::from(error_message))
}
}
Expand All @@ -131,7 +130,7 @@ mod tests {
extern crate dotenv;
extern crate chrono;

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

Expand Down Expand Up @@ -161,7 +160,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 +185,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 +194,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
6 changes: 3 additions & 3 deletions diesel/src/sqlite/types/date_and_time/chrono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ mod tests {
extern crate dotenv;
extern crate chrono;

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

Expand Down Expand Up @@ -131,11 +131,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
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
6 changes: 3 additions & 3 deletions diesel_tests/tests/types_roundtrip.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
extern crate chrono;

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

pub use schema::{connection, TestConnection};
Expand Down Expand Up @@ -154,8 +154,8 @@ 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")]
Expand Down

0 comments on commit c100afc

Please sign in to comment.