Skip to content

Commit

Permalink
Change chrono from_* methods to from_*_opt (gluesql#1000)
Browse files Browse the repository at this point in the history
Change chrono `from_*` methods to `from_*_opt`
Set `chrono` depdnency `=0.4.23` version
  • Loading branch information
ever0de committed Nov 12, 2022
1 parent 28a9181 commit 15e8a08
Show file tree
Hide file tree
Showing 16 changed files with 277 additions and 212 deletions.
2 changes: 1 addition & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async-recursion = "1"
cfg-if = "1"
futures-enum = "0.1.17"
futures = "0.3"
chrono = { version = "0.4.22", features = ["serde", "wasmbind"] }
chrono = { version = "=0.4.23", features = ["serde", "wasmbind"] }
rust_decimal = { version = "1", features = ["serde-str"] }
im-rc = "15"
iter-enum = "1"
Expand Down
68 changes: 43 additions & 25 deletions core/src/data/interval/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,19 @@ impl Interval {
}

pub fn add_date(&self, date: &NaiveDate) -> Result<NaiveDateTime> {
self.add_timestamp(&date.and_hms(0, 0, 0))
self.add_timestamp(
&date
.and_hms_opt(0, 0, 0)
.ok_or_else(|| IntervalError::FailedToParseTime(date.to_string()))?,
)
}

pub fn subtract_from_date(&self, date: &NaiveDate) -> Result<NaiveDateTime> {
self.subtract_from_timestamp(&date.and_hms(0, 0, 0))
self.subtract_from_timestamp(
&date
.and_hms_opt(0, 0, 0)
.ok_or_else(|| IntervalError::FailedToParseTime(date.to_string()))?,
)
}

pub fn add_timestamp(&self, timestamp: &NaiveDateTime) -> Result<NaiveDateTime> {
Expand Down Expand Up @@ -294,12 +302,22 @@ impl Interval {

#[cfg(test)]
mod tests {
use super::{Interval, IntervalError};
use crate::ast::DateTimeField;
use {
super::{Interval, IntervalError},
crate::ast::DateTimeField,
chrono::{NaiveDate, NaiveTime},
};

fn date(year: i32, month: u32, day: u32) -> NaiveDate {
NaiveDate::from_ymd_opt(year, month, day).unwrap()
}

fn time(hour: u32, min: u32, sec: u32) -> NaiveTime {
NaiveTime::from_hms_opt(hour, min, sec).unwrap()
}

#[test]
fn arithmetic() {
use chrono::{NaiveDate, NaiveTime};
use Interval::*;

macro_rules! test {
Expand All @@ -308,20 +326,17 @@ mod tests {
};
}

let date = NaiveDate::from_ymd;
let time = NaiveTime::from_hms;

assert_eq!(Month(1).unary_minus(), Month(-1));
assert_eq!(Microsecond(1).unary_minus(), Microsecond(-1));

// date
assert_eq!(
Month(2).add_date(&date(2021, 11, 11)),
Ok(date(2022, 1, 11).and_hms(0, 0, 0))
Ok(date(2022, 1, 11).and_hms_opt(0, 0, 0).unwrap())
);
assert_eq!(
Interval::hours(30).add_date(&date(2021, 11, 11)),
Ok(date(2021, 11, 12).and_hms(6, 0, 0))
Ok(date(2021, 11, 12).and_hms_opt(6, 0, 0).unwrap())
);
assert_eq!(
Interval::years(999_999).add_date(&date(2021, 11, 11)),
Expand All @@ -333,15 +348,15 @@ mod tests {
);
assert_eq!(
Month(2).subtract_from_date(&date(2021, 11, 11)),
Ok(date(2021, 9, 11).and_hms(0, 0, 0))
Ok(date(2021, 9, 11).and_hms_opt(0, 0, 0).unwrap())
);
assert_eq!(
Month(14).subtract_from_date(&date(2021, 11, 11)),
Ok(date(2020, 9, 11).and_hms(0, 0, 0))
Ok(date(2020, 9, 11).and_hms_opt(0, 0, 0).unwrap())
);
assert_eq!(
Interval::hours(30).subtract_from_date(&date(2021, 11, 11)),
Ok(date(2021, 11, 9).and_hms(18, 0, 0))
Ok(date(2021, 11, 9).and_hms_opt(18, 0, 0).unwrap())
);
assert_eq!(
Interval::years(999_999).subtract_from_date(&date(2021, 11, 11)),
Expand All @@ -354,35 +369,38 @@ mod tests {

// timestamp
assert_eq!(
Interval::minutes(2).add_timestamp(&date(2021, 11, 11).and_hms(12, 3, 1)),
Ok(date(2021, 11, 11).and_hms(12, 5, 1))
Interval::minutes(2).add_timestamp(&date(2021, 11, 11).and_hms_opt(12, 3, 1).unwrap()),
Ok(date(2021, 11, 11).and_hms_opt(12, 5, 1).unwrap())
);
assert_eq!(
Interval::hours(30).add_timestamp(&date(2021, 11, 11).and_hms(0, 30, 0)),
Ok(date(2021, 11, 12).and_hms(6, 30, 0))
Interval::hours(30).add_timestamp(&date(2021, 11, 11).and_hms_opt(0, 30, 0).unwrap()),
Ok(date(2021, 11, 12).and_hms_opt(6, 30, 0).unwrap())
);
assert_eq!(
Interval::years(999_999).add_timestamp(&date(2021, 11, 11).and_hms(1, 1, 1)),
Interval::years(999_999)
.add_timestamp(&date(2021, 11, 11).and_hms_opt(1, 1, 1).unwrap()),
Err(IntervalError::DateOverflow {
year: 1_002_020,
month: 11,
}
.into())
);
assert_eq!(
Month(2).subtract_from_timestamp(&date(2021, 11, 11).and_hms(1, 3, 59)),
Ok(date(2021, 9, 11).and_hms(1, 3, 59))
Month(2).subtract_from_timestamp(&date(2021, 11, 11).and_hms_opt(1, 3, 59).unwrap()),
Ok(date(2021, 9, 11).and_hms_opt(1, 3, 59).unwrap())
);
assert_eq!(
Month(14).subtract_from_timestamp(&date(2021, 11, 11).and_hms(23, 1, 1)),
Ok(date(2020, 9, 11).and_hms(23, 1, 1))
Month(14).subtract_from_timestamp(&date(2021, 11, 11).and_hms_opt(23, 1, 1).unwrap()),
Ok(date(2020, 9, 11).and_hms_opt(23, 1, 1).unwrap())
);
assert_eq!(
Interval::seconds(30).subtract_from_timestamp(&date(2021, 11, 11).and_hms(0, 0, 0)),
Ok(date(2021, 11, 10).and_hms(23, 59, 30))
Interval::seconds(30)
.subtract_from_timestamp(&date(2021, 11, 11).and_hms_opt(0, 0, 0).unwrap()),
Ok(date(2021, 11, 10).and_hms_opt(23, 59, 30).unwrap())
);
assert_eq!(
Interval::years(999_999).subtract_from_timestamp(&date(2021, 11, 11).and_hms(0, 0, 0)),
Interval::years(999_999)
.subtract_from_timestamp(&date(2021, 11, 11).and_hms_opt(0, 0, 0).unwrap()),
Err(IntervalError::DateOverflow {
year: -997977,
month: -1,
Expand Down
26 changes: 18 additions & 8 deletions core/src/data/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,24 +497,34 @@ mod tests {
assert_eq!(cmp(&n5, &n4), Ordering::Greater);
assert_eq!(cmp(&n1, &null), Ordering::Less);

let n1 = Date(NaiveDate::from_ymd(2021, 1, 1)).to_cmp_be_bytes();
let n2 = Date(NaiveDate::from_ymd(1989, 3, 20)).to_cmp_be_bytes();
let n1 = Date(NaiveDate::from_ymd_opt(2021, 1, 1).unwrap()).to_cmp_be_bytes();
let n2 = Date(NaiveDate::from_ymd_opt(1989, 3, 20).unwrap()).to_cmp_be_bytes();

assert_eq!(cmp(&n2, &n2), Ordering::Equal);
assert_eq!(cmp(&n1, &n2), Ordering::Greater);
assert_eq!(cmp(&n1, &null), Ordering::Less);

let n1 = Time(NaiveTime::from_hms_milli(20, 1, 9, 100)).to_cmp_be_bytes();
let n2 = Time(NaiveTime::from_hms_milli(3, 10, 30, 0)).to_cmp_be_bytes();
let n1 = Time(NaiveTime::from_hms_milli_opt(20, 1, 9, 100).unwrap()).to_cmp_be_bytes();
let n2 = Time(NaiveTime::from_hms_milli_opt(3, 10, 30, 0).unwrap()).to_cmp_be_bytes();

assert_eq!(cmp(&n2, &n2), Ordering::Equal);
assert_eq!(cmp(&n1, &n2), Ordering::Greater);
assert_eq!(cmp(&n1, &null), Ordering::Less);

let n1 =
Timestamp(NaiveDate::from_ymd(2021, 1, 1).and_hms_milli(1, 2, 3, 0)).to_cmp_be_bytes();
let n2 = Timestamp(NaiveDate::from_ymd(1989, 3, 20).and_hms_milli(10, 0, 0, 1000))
.to_cmp_be_bytes();
let n1 = Timestamp(
NaiveDate::from_ymd_opt(2021, 1, 1)
.unwrap()
.and_hms_milli_opt(1, 2, 3, 0)
.unwrap(),
)
.to_cmp_be_bytes();
let n2 = Timestamp(
NaiveDate::from_ymd_opt(1989, 3, 20)
.unwrap()
.and_hms_milli_opt(10, 0, 0, 1000)
.unwrap(),
)
.to_cmp_be_bytes();

assert_eq!(cmp(&n2, &n2), Ordering::Equal);
assert_eq!(cmp(&n1, &n2), Ordering::Greater);
Expand Down

0 comments on commit 15e8a08

Please sign in to comment.