Skip to content

Commit

Permalink
refactor: eliminate the deprecation warning (#195)
Browse files Browse the repository at this point in the history
chrono has deprecated some methods of NaiveDataTime:
https://docs.rs/chrono/latest/chrono/naive/struct.NaiveDateTime.html#method.from_timestamp
I apply `cargo clippy` to correct it, and all tests are passed locally.
  • Loading branch information
yingmanwumen authored Mar 31, 2024
1 parent 28280a4 commit f68904c
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/types/value.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use chrono::format::{DelayedFormat, StrftimeItems};
use chrono::{Datelike, NaiveDate, NaiveDateTime, NaiveTime, Timelike};
use chrono::{DateTime, Datelike, NaiveDate, NaiveDateTime, NaiveTime, Timelike};
use integer_encoding::{FixedInt, FixedIntWriter};
use lazy_static::lazy_static;
use rust_decimal::Decimal;
Expand All @@ -20,7 +20,7 @@ use super::LogicalType;

lazy_static! {
pub static ref NULL_VALUE: ValueRef = Arc::new(DataValue::Null);
static ref UNIX_DATETIME: NaiveDateTime = NaiveDateTime::from_timestamp_opt(0, 0).unwrap();
static ref UNIX_DATETIME: NaiveDateTime = DateTime::from_timestamp(0, 0).unwrap().naive_utc();
static ref UNIX_TIME: NaiveTime = NaiveTime::from_hms_opt(0, 0, 0).unwrap();
}

Expand Down Expand Up @@ -305,7 +305,7 @@ impl DataValue {

pub fn datetime(&self) -> Option<NaiveDateTime> {
if let DataValue::Date64(Some(val)) = self {
NaiveDateTime::from_timestamp_opt(*val, 0)
DateTime::from_timestamp(*val, 0).map(|dt| dt.naive_utc())
} else {
None
}
Expand Down Expand Up @@ -478,7 +478,7 @@ impl DataValue {
unit: *unit,
},
LogicalType::Date => DataValue::Date32(Some(UNIX_DATETIME.num_days_from_ce())),
LogicalType::DateTime => DataValue::Date64(Some(UNIX_DATETIME.timestamp())),
LogicalType::DateTime => DataValue::Date64(Some(UNIX_DATETIME.and_utc().timestamp())),
LogicalType::Time => DataValue::Time(Some(UNIX_TIME.num_seconds_from_midnight())),
LogicalType::Decimal(_, _) => DataValue::Decimal(Some(Decimal::new(0, 0))),
LogicalType::Tuple => DataValue::Tuple(Some(vec![])),
Expand Down Expand Up @@ -1208,7 +1208,7 @@ impl DataValue {
NaiveDate::parse_from_str(&v, DATE_FMT)
.map(|date| date.and_hms_opt(0, 0, 0).unwrap())
})
.map(|date_time| date_time.timestamp())
.map(|date_time| date_time.and_utc().timestamp())
})
.transpose()?;

Expand Down Expand Up @@ -1252,7 +1252,7 @@ impl DataValue {
let option = value.and_then(|v| {
NaiveDate::from_num_days_from_ce_opt(v)
.and_then(|date| date.and_hms_opt(0, 0, 0))
.map(|date_time| date_time.timestamp())
.map(|date_time| date_time.and_utc().timestamp())
});

Ok(DataValue::Date64(option))
Expand All @@ -1279,7 +1279,8 @@ impl DataValue {
}
LogicalType::Date => {
let option = value.and_then(|v| {
NaiveDateTime::from_timestamp_opt(v, 0)
DateTime::from_timestamp(v, 0)
.map(|dt| dt.naive_utc())
.map(|date_time| date_time.date().num_days_from_ce())
});

Expand All @@ -1288,7 +1289,7 @@ impl DataValue {
LogicalType::DateTime => Ok(DataValue::Date64(value)),
LogicalType::Time => {
let option = value.and_then(|v| {
NaiveDateTime::from_timestamp_opt(v, 0)
DateTime::from_timestamp(v, 0)
.map(|date_time| date_time.time().num_seconds_from_midnight())
});

Expand Down Expand Up @@ -1393,7 +1394,7 @@ impl DataValue {
}

fn date_time_format<'a>(v: i64) -> Option<DelayedFormat<StrftimeItems<'a>>> {
NaiveDateTime::from_timestamp_opt(v, 0).map(|date_time| date_time.format(DATE_TIME_FMT))
DateTime::from_timestamp(v, 0).map(|date_time| date_time.format(DATE_TIME_FMT))
}

fn time_format<'a>(v: u32) -> Option<DelayedFormat<StrftimeItems<'a>>> {
Expand Down

0 comments on commit f68904c

Please sign in to comment.