Skip to content

Commit

Permalink
Fix deprecated warning from chrono (jorgecarleitao#1299)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xuanwo authored and ritchie46 committed Apr 5, 2023
1 parent e7b2829 commit f6d62e6
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 18 deletions.
6 changes: 4 additions & 2 deletions benches/cast_kernels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ fn build_utf8_date_array(size: usize, with_nulls: bool) -> Utf8Array<i32> {
None
} else {
Some(
NaiveDate::from_num_days_from_ce(rng.sample(range))
NaiveDate::from_num_days_from_ce_opt(rng.sample(range))
.unwrap()
.format("%Y-%m-%d")
.to_string(),
)
Expand All @@ -59,7 +60,8 @@ fn build_utf8_date_time_array(size: usize, with_nulls: bool) -> Utf8Array<i32> {
None
} else {
Some(
NaiveDateTime::from_timestamp(rng.sample(range), 0)
NaiveDateTime::from_timestamp_opt(rng.sample(range), 0)
.unwrap()
.format("%Y-%m-%dT%H:%M:%S")
.to_string(),
)
Expand Down
2 changes: 2 additions & 0 deletions src/array/list/mutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ impl<O: Offset, M: MutableArray> MutableListArray<O, M> {
/// - the new offsets are not in monotonic increasing order.
/// - any new offset is not in bounds of the backing array.
/// - the passed iterator has no upper bound.
#[allow(dead_code)]
pub(crate) fn extend_offsets<II>(&mut self, expansion: II)
where
II: TrustedLen<Item = Option<O>>,
Expand Down Expand Up @@ -214,6 +215,7 @@ impl<O: Offset, M: MutableArray> MutableListArray<O, M> {
/// zero if the array is currently empty.
///
/// Panics if the passed iterator has no upper bound.
#[allow(dead_code)]
pub(crate) unsafe fn unsafe_extend_offsets<II>(&mut self, expansion: II)
where
II: TrustedLen<Item = Option<O>>,
Expand Down
2 changes: 1 addition & 1 deletion src/io/odbc/read/deserialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ fn date_optional(
}

fn days_since_epoch(date: &odbc_api::sys::Date) -> i32 {
let unix_epoch = NaiveDate::from_ymd(1970, 1, 1);
let unix_epoch = NaiveDate::from_ymd_opt(1970, 1, 1).expect("invalid or out-of-range date");
let date = NaiveDate::from_ymd_opt(date.year as i32, date.month as u32, date.day as u32)
.unwrap_or(unix_epoch);
let duration = date.signed_duration_since(unix_epoch);
Expand Down
42 changes: 27 additions & 15 deletions src/temporal_conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,26 @@ pub const EPOCH_DAYS_FROM_CE: i32 = 719_163;
/// converts a `i32` representing a `date32` to [`NaiveDateTime`]
#[inline]
pub fn date32_to_datetime(v: i32) -> NaiveDateTime {
NaiveDateTime::from_timestamp(v as i64 * SECONDS_IN_DAY, 0)
NaiveDateTime::from_timestamp_opt(v as i64 * SECONDS_IN_DAY, 0)
.expect("invalid or out-of-range datetime")
}

/// converts a `i32` representing a `date32` to [`NaiveDate`]
#[inline]
pub fn date32_to_date(days: i32) -> NaiveDate {
NaiveDate::from_num_days_from_ce(EPOCH_DAYS_FROM_CE + days)
NaiveDate::from_num_days_from_ce_opt(EPOCH_DAYS_FROM_CE + days).expect("out-of-range date")
}

/// converts a `i64` representing a `date64` to [`NaiveDateTime`]
#[inline]
pub fn date64_to_datetime(v: i64) -> NaiveDateTime {
NaiveDateTime::from_timestamp(
NaiveDateTime::from_timestamp_opt(
// extract seconds from milliseconds
v / MILLISECONDS,
// discard extracted seconds and convert milliseconds to nanoseconds
(v % MILLISECONDS * MICROSECONDS) as u32,
)
.expect("invalid or out-of-range datetime")
}

/// converts a `i64` representing a `date64` to [`NaiveDate`]
Expand All @@ -60,7 +62,7 @@ pub fn date64_to_date(milliseconds: i64) -> NaiveDate {
/// converts a `i32` representing a `time32(s)` to [`NaiveDateTime`]
#[inline]
pub fn time32s_to_time(v: i32) -> NaiveTime {
NaiveTime::from_num_seconds_from_midnight(v as u32, 0)
NaiveTime::from_num_seconds_from_midnight_opt(v as u32, 0).expect("invalid time")
}

/// converts a `i32` representing a `time32(ms)` to [`NaiveTime`]
Expand All @@ -71,69 +73,75 @@ pub fn time32ms_to_time(v: i32) -> NaiveTime {

let milli_to_nano = 1_000_000;
let nano = (v - seconds * MILLISECONDS) * milli_to_nano;
NaiveTime::from_num_seconds_from_midnight(seconds as u32, nano as u32)
NaiveTime::from_num_seconds_from_midnight_opt(seconds as u32, nano as u32)
.expect("invalid time")
}

/// converts a `i64` representing a `time64(us)` to [`NaiveDateTime`]
#[inline]
pub fn time64us_to_time(v: i64) -> NaiveTime {
NaiveTime::from_num_seconds_from_midnight(
NaiveTime::from_num_seconds_from_midnight_opt(
// extract seconds from microseconds
(v / MICROSECONDS) as u32,
// discard extracted seconds and convert microseconds to
// nanoseconds
(v % MICROSECONDS * MILLISECONDS) as u32,
)
.expect("invalid time")
}

/// converts a `i64` representing a `time64(ns)` to [`NaiveDateTime`]
#[inline]
pub fn time64ns_to_time(v: i64) -> NaiveTime {
NaiveTime::from_num_seconds_from_midnight(
NaiveTime::from_num_seconds_from_midnight_opt(
// extract seconds from nanoseconds
(v / NANOSECONDS) as u32,
// discard extracted seconds
(v % NANOSECONDS) as u32,
)
.expect("invalid time")
}

/// converts a `i64` representing a `timestamp(s)` to [`NaiveDateTime`]
#[inline]
pub fn timestamp_s_to_datetime(seconds: i64) -> NaiveDateTime {
NaiveDateTime::from_timestamp(seconds, 0)
NaiveDateTime::from_timestamp_opt(seconds, 0).expect("invalid or out-of-range datetime")
}

/// converts a `i64` representing a `timestamp(ms)` to [`NaiveDateTime`]
#[inline]
pub fn timestamp_ms_to_datetime(v: i64) -> NaiveDateTime {
NaiveDateTime::from_timestamp(
NaiveDateTime::from_timestamp_opt(
// extract seconds from milliseconds
v / MILLISECONDS,
// discard extracted seconds and convert milliseconds to nanoseconds
(v % MILLISECONDS * MICROSECONDS) as u32,
)
.expect("invalid or out-of-range datetime")
}

/// converts a `i64` representing a `timestamp(us)` to [`NaiveDateTime`]
#[inline]
pub fn timestamp_us_to_datetime(v: i64) -> NaiveDateTime {
NaiveDateTime::from_timestamp(
NaiveDateTime::from_timestamp_opt(
// extract seconds from microseconds
v / MICROSECONDS,
// discard extracted seconds and convert microseconds to nanoseconds
(v % MICROSECONDS * MILLISECONDS) as u32,
)
.expect("invalid or out-of-range datetime")
}

/// converts a `i64` representing a `timestamp(ns)` to [`NaiveDateTime`]
#[inline]
pub fn timestamp_ns_to_datetime(v: i64) -> NaiveDateTime {
NaiveDateTime::from_timestamp(
NaiveDateTime::from_timestamp_opt(
// extract seconds from nanoseconds
v / NANOSECONDS,
// discard extracted seconds
(v % NANOSECONDS) as u32,
)
.expect("invalid or out-of-range datetime")
}

/// Converts a timestamp in `time_unit` and `timezone` into [`chrono::DateTime`].
Expand Down Expand Up @@ -186,7 +194,7 @@ pub fn timeunit_scale(a: TimeUnit, b: TimeUnit) -> f64 {
/// If the offset is not in any of the allowed forms.
pub fn parse_offset(offset: &str) -> Result<FixedOffset> {
if offset == "UTC" {
return Ok(FixedOffset::east(0));
return Ok(FixedOffset::east_opt(0).expect("FixedOffset::east out of bounds"));
}
let error = "timezone offset must be of the form [-]00:00";

Expand All @@ -206,7 +214,8 @@ pub fn parse_offset(offset: &str) -> Result<FixedOffset> {
.parse()
.map_err(|_| Error::InvalidArgumentError(error.to_string()))?;

Ok(FixedOffset::east(hours * 60 * 60 + minutes * 60))
Ok(FixedOffset::east_opt(hours * 60 * 60 + minutes * 60)
.expect("FixedOffset::east out of bounds"))
}

/// Parses `value` to `Option<i64>` consistent with the Arrow's definition of timestamp with timezone.
Expand Down Expand Up @@ -330,12 +339,15 @@ pub fn utf8_to_naive_timestamp_ns<O: Offset>(
fn add_month(year: i32, month: u32, months: i32) -> chrono::NaiveDate {
let new_year = (year * 12 + (month - 1) as i32 + months) / 12;
let new_month = (year * 12 + (month - 1) as i32 + months) % 12 + 1;
chrono::NaiveDate::from_ymd(new_year, new_month as u32, 1)
chrono::NaiveDate::from_ymd_opt(new_year, new_month as u32, 1)
.expect("invalid or out-of-range date")
}

fn get_days_between_months(year: i32, month: u32, months: i32) -> i64 {
add_month(year, month, months)
.signed_duration_since(chrono::NaiveDate::from_ymd(year, month, 1))
.signed_duration_since(
chrono::NaiveDate::from_ymd_opt(year, month, 1).expect("invalid or out-of-range date"),
)
.num_days()
}

Expand Down

0 comments on commit f6d62e6

Please sign in to comment.