Skip to content

Commit

Permalink
chore(deps): update to latest chrono (#4956)
Browse files Browse the repository at this point in the history
  • Loading branch information
Weakky committed Jul 19, 2024
1 parent 77b82f6 commit 68bfdeb
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 45 deletions.
23 changes: 6 additions & 17 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ tokio = { version = "1.25", features = [
"parking_lot",
"time",
] }
chrono = { version = "0.4", features = ["serde"] }
chrono = { version = "0.4.38", features = ["serde"] }
user-facing-errors = { path = "./libs/user-facing-errors" }
uuid = { version = "1", features = ["serde", "v4"] }
indoc = "2.0.1"
Expand Down
6 changes: 3 additions & 3 deletions psl/psl-core/src/builtin_connectors/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub(crate) mod common {

pub(crate) fn parse_date(str: &str) -> Result<DateTime<FixedOffset>, chrono::ParseError> {
chrono::NaiveDate::parse_from_str(str, "%Y-%m-%d")
.map(|date| DateTime::<Utc>::from_utc(date.and_hms_opt(0, 0, 0).unwrap(), Utc))
.map(|date| DateTime::<Utc>::from_naive_utc_and_offset(date.and_hms_opt(0, 0, 0).unwrap(), Utc))
.map(DateTime::<FixedOffset>::from)
}

Expand All @@ -13,14 +13,14 @@ pub(crate) mod common {
.map(|time| {
let base_date = chrono::NaiveDate::from_ymd_opt(1970, 1, 1).unwrap();

DateTime::<Utc>::from_utc(base_date.and_time(time), Utc)
DateTime::<Utc>::from_naive_utc_and_offset(base_date.and_time(time), Utc)
})
.map(DateTime::<FixedOffset>::from)
}

pub(crate) fn parse_timestamp(str: &str, fmt: &str) -> Result<DateTime<FixedOffset>, chrono::ParseError> {
NaiveDateTime::parse_from_str(str, fmt)
.map(|dt| DateTime::from_utc(dt, Utc))
.map(|dt| DateTime::from_naive_utc_and_offset(dt, Utc))
.or_else(|_| DateTime::parse_from_rfc3339(str).map(DateTime::<Utc>::from))
.map(DateTime::<FixedOffset>::from)
}
Expand Down
6 changes: 3 additions & 3 deletions quaint/src/connector/mssql/native/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ impl TryFrom<ColumnData<'static>> for Value<'static> {
dt @ ColumnData::DateTime(_) => {
use tiberius::time::chrono::{DateTime, NaiveDateTime, Utc};

let dt = NaiveDateTime::from_sql(&dt)?.map(|dt| DateTime::<Utc>::from_utc(dt, Utc));
let dt = NaiveDateTime::from_sql(&dt)?.map(|dt| DateTime::<Utc>::from_naive_utc_and_offset(dt, Utc));
ValueType::DateTime(dt)
}
dt @ ColumnData::SmallDateTime(_) => {
use tiberius::time::chrono::{DateTime, NaiveDateTime, Utc};

let dt = NaiveDateTime::from_sql(&dt)?.map(|dt| DateTime::<Utc>::from_utc(dt, Utc));
let dt = NaiveDateTime::from_sql(&dt)?.map(|dt| DateTime::<Utc>::from_naive_utc_and_offset(dt, Utc));
ValueType::DateTime(dt)
}
dt @ ColumnData::Time(_) => {
Expand All @@ -70,7 +70,7 @@ impl TryFrom<ColumnData<'static>> for Value<'static> {
dt @ ColumnData::DateTime2(_) => {
use tiberius::time::chrono::{DateTime, NaiveDateTime, Utc};

let dt = NaiveDateTime::from_sql(&dt)?.map(|dt| DateTime::<Utc>::from_utc(dt, Utc));
let dt = NaiveDateTime::from_sql(&dt)?.map(|dt| DateTime::<Utc>::from_naive_utc_and_offset(dt, Utc));

ValueType::DateTime(dt)
}
Expand Down
2 changes: 1 addition & 1 deletion quaint/src/connector/mysql/native/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ impl TakeRow for my::Row {
let date = NaiveDate::from_ymd_opt(year.into(), month.into(), day.into()).unwrap();
let dt = NaiveDateTime::new(date, time);

Value::datetime(DateTime::<Utc>::from_utc(dt, Utc))
Value::datetime(DateTime::<Utc>::from_naive_utc_and_offset(dt, Utc))
}
my::Value::Time(is_neg, days, hours, minutes, seconds, micros) => {
if is_neg {
Expand Down
8 changes: 4 additions & 4 deletions quaint/src/connector/postgres/native/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ impl GetRow for PostgresRow {
PostgresType::TIMESTAMP => match row.try_get(i)? {
Some(val) => {
let ts: NaiveDateTime = val;
let dt = DateTime::<Utc>::from_utc(ts, Utc);
let dt = DateTime::<Utc>::from_naive_utc_and_offset(ts, Utc);
Value::datetime(dt)
}
None => Value::null_datetime(),
Expand Down Expand Up @@ -332,9 +332,9 @@ impl GetRow for PostgresRow {
Some(val) => {
let val: Vec<Option<NaiveDateTime>> = val;

let dates = val
.into_iter()
.map(|dt| ValueType::DateTime(dt.map(|dt| DateTime::<Utc>::from_utc(dt, Utc))));
let dates = val.into_iter().map(|dt| {
ValueType::DateTime(dt.map(|dt| DateTime::<Utc>::from_naive_utc_and_offset(dt, Utc)))
});

Value::array(dates)
}
Expand Down
10 changes: 5 additions & 5 deletions quaint/src/connector/sqlite/native/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ impl<'a> GetRow for SqliteRow<'a> {
}
}
c if c.is_date() => {
let dt = chrono::NaiveDateTime::from_timestamp_opt(i / 1000, 0).unwrap();
Value::date(dt.date())
let dt = chrono::DateTime::from_timestamp(i / 1000, 0).unwrap();
Value::date(dt.date_naive())
}
c if c.is_datetime() => {
let dt = chrono::Utc.timestamp_millis_opt(i).unwrap();
Expand Down Expand Up @@ -211,7 +211,7 @@ impl<'a> GetRow for SqliteRow<'a> {

parse_res.and_then(|s| {
chrono::NaiveDateTime::parse_from_str(s, "%Y-%m-%d %H:%M:%S")
.map(|nd| chrono::DateTime::<chrono::Utc>::from_utc(nd, chrono::Utc))
.map(|nd| chrono::DateTime::<chrono::Utc>::from_naive_utc_and_offset(nd, chrono::Utc))
.or_else(|_| {
chrono::DateTime::parse_from_rfc3339(s).map(|dt| dt.with_timezone(&chrono::Utc))
})
Expand Down Expand Up @@ -282,14 +282,14 @@ impl<'a> ToSql for Value<'a> {
ValueType::DateTime(value) => value.map(|value| ToSqlOutput::from(value.timestamp_millis())),
ValueType::Date(date) => date
.and_then(|date| date.and_hms_opt(0, 0, 0))
.map(|dt| ToSqlOutput::from(dt.timestamp_millis())),
.map(|dt| ToSqlOutput::from(dt.and_utc().timestamp_millis())),
ValueType::Time(time) => time
.and_then(|time| chrono::NaiveDate::from_ymd_opt(1970, 1, 1).map(|d| (d, time)))
.and_then(|(date, time)| {
use chrono::Timelike;
date.and_hms_opt(time.hour(), time.minute(), time.second())
})
.map(|dt| ToSqlOutput::from(dt.timestamp_millis())),
.map(|dt| ToSqlOutput::from(dt.and_utc().timestamp_millis())),
};

match value {
Expand Down
2 changes: 1 addition & 1 deletion quaint/src/tests/types/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ async fn test_type_text_datetime_custom(api: &mut dyn TestApi) -> crate::Result<
let res = api.conn().select(select).await?.into_single()?;

let naive = chrono::NaiveDateTime::parse_from_str("2020-04-20 16:20:00", "%Y-%m-%d %H:%M:%S").unwrap();
let expected = chrono::DateTime::from_utc(naive, chrono::Utc);
let expected = chrono::DateTime::from_naive_utc_and_offset(naive, chrono::Utc);

assert_eq!(Some(&Value::datetime(expected)), res.at(0));

Expand Down
7 changes: 3 additions & 4 deletions query-engine/connectors/sql-query-connector/src/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,7 @@ fn row_value_to_prisma_value(p_value: Value, meta: ColumnMetadata<'_>) -> Result
let ts = value.as_integer().unwrap();
let nsecs = ((ts % 1000) * 1_000_000) as u32;
let secs = ts / 1000;
let naive = chrono::NaiveDateTime::from_timestamp_opt(secs, nsecs).unwrap();
let datetime: DateTime<Utc> = DateTime::from_utc(naive, Utc);
let datetime = chrono::DateTime::from_timestamp(secs, nsecs).unwrap();

PrismaValue::DateTime(datetime.into())
}
Expand All @@ -189,12 +188,12 @@ fn row_value_to_prisma_value(p_value: Value, meta: ColumnMetadata<'_>) -> Result
PrismaValue::DateTime(dt.with_timezone(&Utc).into())
}
ValueType::Date(Some(d)) => {
let dt = DateTime::<Utc>::from_utc(d.and_hms_opt(0, 0, 0).unwrap(), Utc);
let dt = DateTime::<Utc>::from_naive_utc_and_offset(d.and_hms_opt(0, 0, 0).unwrap(), Utc);
PrismaValue::DateTime(dt.into())
}
ValueType::Time(Some(t)) => {
let d = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap();
let dt = DateTime::<Utc>::from_utc(d.and_time(t), Utc);
let dt = DateTime::<Utc>::from_naive_utc_and_offset(d.and_time(t), Utc);
PrismaValue::DateTime(dt.into())
}
_ => return Err(create_error(&p_value)),
Expand Down
4 changes: 2 additions & 2 deletions query-engine/connectors/sql-query-connector/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ pub fn to_prisma_value<'a, T: Into<ValueType<'a>>>(qv: T) -> crate::Result<Prism

ValueType::Date(d) => d
.map(|d| {
let dt = DateTime::<Utc>::from_utc(d.and_hms_opt(0, 0, 0).unwrap(), Utc);
let dt = DateTime::<Utc>::from_naive_utc_and_offset(d.and_hms_opt(0, 0, 0).unwrap(), Utc);
PrismaValue::DateTime(dt.into())
})
.unwrap_or(PrismaValue::Null),

ValueType::Time(t) => t
.map(|t| {
let d = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap();
let dt = DateTime::<Utc>::from_utc(d.and_time(t), Utc);
let dt = DateTime::<Utc>::from_naive_utc_and_offset(d.and_time(t), Utc);
PrismaValue::DateTime(dt.into())
})
.unwrap_or(PrismaValue::Null),
Expand Down
8 changes: 4 additions & 4 deletions query-engine/driver-adapters/src/conversion/js_to_quaint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ pub fn js_value_to_quaint(
ColumnType::DateTime => match json_value {
// TODO: change parsing order to prefer RFC3339
serde_json::Value::String(s) => quaint::chrono::NaiveDateTime::parse_from_str(&s, "%Y-%m-%d %H:%M:%S%.f")
.map(|dt| DateTime::from_utc(dt, Utc))
.map(|dt| DateTime::from_naive_utc_and_offset(dt, Utc))
.or_else(|_| DateTime::parse_from_rfc3339(&s).map(DateTime::<Utc>::from))
.map(QuaintValue::datetime)
.map_err(|_| conversion_error!("expected a datetime string in column '{column_name}', found {s}")),
Expand Down Expand Up @@ -587,7 +587,7 @@ mod proxy_test {
.unwrap()
.and_hms_milli_opt(23, 59, 59, 415)
.unwrap();
let datetime = DateTime::from_utc(datetime, Utc);
let datetime = DateTime::from_naive_utc_and_offset(datetime, Utc);
assert_eq!(quaint_value, QuaintValue::datetime(datetime));

let s = "2023-01-01 23:59:59.123456";
Expand All @@ -598,7 +598,7 @@ mod proxy_test {
.unwrap()
.and_hms_micro_opt(23, 59, 59, 123_456)
.unwrap();
let datetime = DateTime::from_utc(datetime, Utc);
let datetime = DateTime::from_naive_utc_and_offset(datetime, Utc);
assert_eq!(quaint_value, QuaintValue::datetime(datetime));

let s = "2023-01-01 23:59:59";
Expand All @@ -609,7 +609,7 @@ mod proxy_test {
.unwrap()
.and_hms_milli_opt(23, 59, 59, 0)
.unwrap();
let datetime = DateTime::from_utc(datetime, Utc);
let datetime = DateTime::from_naive_utc_and_offset(datetime, Utc);
assert_eq!(quaint_value, QuaintValue::datetime(datetime));
}

Expand Down

0 comments on commit 68bfdeb

Please sign in to comment.