Skip to content
This repository was archived by the owner on Feb 18, 2024. It is now read-only.

Commit 0ba4f8e

Browse files
authored
Fix deprecated warning from chrono (#1299)
1 parent be81cae commit 0ba4f8e

File tree

4 files changed

+34
-18
lines changed

4 files changed

+34
-18
lines changed

benches/cast_kernels.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ fn build_utf8_date_array(size: usize, with_nulls: bool) -> Utf8Array<i32> {
3737
None
3838
} else {
3939
Some(
40-
NaiveDate::from_num_days_from_ce(rng.sample(range))
40+
NaiveDate::from_num_days_from_ce_opt(rng.sample(range))
41+
.unwrap()
4142
.format("%Y-%m-%d")
4243
.to_string(),
4344
)
@@ -59,7 +60,8 @@ fn build_utf8_date_time_array(size: usize, with_nulls: bool) -> Utf8Array<i32> {
5960
None
6061
} else {
6162
Some(
62-
NaiveDateTime::from_timestamp(rng.sample(range), 0)
63+
NaiveDateTime::from_timestamp_opt(rng.sample(range), 0)
64+
.unwrap()
6365
.format("%Y-%m-%dT%H:%M:%S")
6466
.to_string(),
6567
)

src/array/list/mutable.rs

+2
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ impl<O: Offset, M: MutableArray> MutableListArray<O, M> {
181181
/// - the new offsets are not in monotonic increasing order.
182182
/// - any new offset is not in bounds of the backing array.
183183
/// - the passed iterator has no upper bound.
184+
#[allow(dead_code)]
184185
pub(crate) fn extend_offsets<II>(&mut self, expansion: II)
185186
where
186187
II: TrustedLen<Item = Option<O>>,
@@ -214,6 +215,7 @@ impl<O: Offset, M: MutableArray> MutableListArray<O, M> {
214215
/// zero if the array is currently empty.
215216
///
216217
/// Panics if the passed iterator has no upper bound.
218+
#[allow(dead_code)]
217219
pub(crate) unsafe fn unsafe_extend_offsets<II>(&mut self, expansion: II)
218220
where
219221
II: TrustedLen<Item = Option<O>>,

src/io/odbc/read/deserialize.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ fn date_optional(
169169
}
170170

171171
fn days_since_epoch(date: &odbc_api::sys::Date) -> i32 {
172-
let unix_epoch = NaiveDate::from_ymd(1970, 1, 1);
172+
let unix_epoch = NaiveDate::from_ymd_opt(1970, 1, 1).expect("invalid or out-of-range date");
173173
let date = NaiveDate::from_ymd_opt(date.year as i32, date.month as u32, date.day as u32)
174174
.unwrap_or(unix_epoch);
175175
let duration = date.signed_duration_since(unix_epoch);

src/temporal_conversions.rs

+27-15
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,26 @@ pub const EPOCH_DAYS_FROM_CE: i32 = 719_163;
3131
/// converts a `i32` representing a `date32` to [`NaiveDateTime`]
3232
#[inline]
3333
pub fn date32_to_datetime(v: i32) -> NaiveDateTime {
34-
NaiveDateTime::from_timestamp(v as i64 * SECONDS_IN_DAY, 0)
34+
NaiveDateTime::from_timestamp_opt(v as i64 * SECONDS_IN_DAY, 0)
35+
.expect("invalid or out-of-range datetime")
3536
}
3637

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

4344
/// converts a `i64` representing a `date64` to [`NaiveDateTime`]
4445
#[inline]
4546
pub fn date64_to_datetime(v: i64) -> NaiveDateTime {
46-
NaiveDateTime::from_timestamp(
47+
NaiveDateTime::from_timestamp_opt(
4748
// extract seconds from milliseconds
4849
v / MILLISECONDS,
4950
// discard extracted seconds and convert milliseconds to nanoseconds
5051
(v % MILLISECONDS * MICROSECONDS) as u32,
5152
)
53+
.expect("invalid or out-of-range datetime")
5254
}
5355

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

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

7274
let milli_to_nano = 1_000_000;
7375
let nano = (v - seconds * MILLISECONDS) * milli_to_nano;
74-
NaiveTime::from_num_seconds_from_midnight(seconds as u32, nano as u32)
76+
NaiveTime::from_num_seconds_from_midnight_opt(seconds as u32, nano as u32)
77+
.expect("invalid time")
7578
}
7679

7780
/// converts a `i64` representing a `time64(us)` to [`NaiveDateTime`]
7881
#[inline]
7982
pub fn time64us_to_time(v: i64) -> NaiveTime {
80-
NaiveTime::from_num_seconds_from_midnight(
83+
NaiveTime::from_num_seconds_from_midnight_opt(
8184
// extract seconds from microseconds
8285
(v / MICROSECONDS) as u32,
8386
// discard extracted seconds and convert microseconds to
8487
// nanoseconds
8588
(v % MICROSECONDS * MILLISECONDS) as u32,
8689
)
90+
.expect("invalid time")
8791
}
8892

8993
/// converts a `i64` representing a `time64(ns)` to [`NaiveDateTime`]
9094
#[inline]
9195
pub fn time64ns_to_time(v: i64) -> NaiveTime {
92-
NaiveTime::from_num_seconds_from_midnight(
96+
NaiveTime::from_num_seconds_from_midnight_opt(
9397
// extract seconds from nanoseconds
9498
(v / NANOSECONDS) as u32,
9599
// discard extracted seconds
96100
(v % NANOSECONDS) as u32,
97101
)
102+
.expect("invalid time")
98103
}
99104

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

106111
/// converts a `i64` representing a `timestamp(ms)` to [`NaiveDateTime`]
107112
#[inline]
108113
pub fn timestamp_ms_to_datetime(v: i64) -> NaiveDateTime {
109-
NaiveDateTime::from_timestamp(
114+
NaiveDateTime::from_timestamp_opt(
110115
// extract seconds from milliseconds
111116
v / MILLISECONDS,
112117
// discard extracted seconds and convert milliseconds to nanoseconds
113118
(v % MILLISECONDS * MICROSECONDS) as u32,
114119
)
120+
.expect("invalid or out-of-range datetime")
115121
}
116122

117123
/// converts a `i64` representing a `timestamp(us)` to [`NaiveDateTime`]
118124
#[inline]
119125
pub fn timestamp_us_to_datetime(v: i64) -> NaiveDateTime {
120-
NaiveDateTime::from_timestamp(
126+
NaiveDateTime::from_timestamp_opt(
121127
// extract seconds from microseconds
122128
v / MICROSECONDS,
123129
// discard extracted seconds and convert microseconds to nanoseconds
124130
(v % MICROSECONDS * MILLISECONDS) as u32,
125131
)
132+
.expect("invalid or out-of-range datetime")
126133
}
127134

128135
/// converts a `i64` representing a `timestamp(ns)` to [`NaiveDateTime`]
129136
#[inline]
130137
pub fn timestamp_ns_to_datetime(v: i64) -> NaiveDateTime {
131-
NaiveDateTime::from_timestamp(
138+
NaiveDateTime::from_timestamp_opt(
132139
// extract seconds from nanoseconds
133140
v / NANOSECONDS,
134141
// discard extracted seconds
135142
(v % NANOSECONDS) as u32,
136143
)
144+
.expect("invalid or out-of-range datetime")
137145
}
138146

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

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

209-
Ok(FixedOffset::east(hours * 60 * 60 + minutes * 60))
217+
Ok(FixedOffset::east_opt(hours * 60 * 60 + minutes * 60)
218+
.expect("FixedOffset::east out of bounds"))
210219
}
211220

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

336346
fn get_days_between_months(year: i32, month: u32, months: i32) -> i64 {
337347
add_month(year, month, months)
338-
.signed_duration_since(chrono::NaiveDate::from_ymd(year, month, 1))
348+
.signed_duration_since(
349+
chrono::NaiveDate::from_ymd_opt(year, month, 1).expect("invalid or out-of-range date"),
350+
)
339351
.num_days()
340352
}
341353

0 commit comments

Comments
 (0)