Skip to content

Commit ae1d10f

Browse files
authored
Update latest chrono (#5479)
1 parent 8caec3b commit ae1d10f

File tree

8 files changed

+90
-71
lines changed

8 files changed

+90
-71
lines changed

arrow-array/src/array/primitive_array.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1557,7 +1557,10 @@ mod tests {
15571557
// roundtrip to and from datetime
15581558
assert_eq!(
15591559
1550902545147,
1560-
arr.value_as_datetime(i).unwrap().timestamp_millis()
1560+
arr.value_as_datetime(i)
1561+
.unwrap()
1562+
.and_utc()
1563+
.timestamp_millis()
15611564
);
15621565
} else {
15631566
assert!(arr.is_null(i));

arrow-array/src/temporal_conversions.rs

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,21 @@ pub const EPOCH_DAYS_FROM_CE: i32 = 719_163;
4343
/// converts a `i32` representing a `date32` to [`NaiveDateTime`]
4444
#[inline]
4545
pub fn date32_to_datetime(v: i32) -> Option<NaiveDateTime> {
46-
NaiveDateTime::from_timestamp_opt(v as i64 * SECONDS_IN_DAY, 0)
46+
Some(DateTime::from_timestamp(v as i64 * SECONDS_IN_DAY, 0)?.naive_utc())
4747
}
4848

4949
/// converts a `i64` representing a `date64` to [`NaiveDateTime`]
5050
#[inline]
5151
pub fn date64_to_datetime(v: i64) -> Option<NaiveDateTime> {
5252
let (sec, milli_sec) = split_second(v, MILLISECONDS);
5353

54-
NaiveDateTime::from_timestamp_opt(
54+
let datetime = DateTime::from_timestamp(
5555
// extract seconds from milliseconds
5656
sec,
5757
// discard extracted seconds and convert milliseconds to nanoseconds
5858
milli_sec * MICROSECONDS as u32,
59-
)
59+
)?;
60+
Some(datetime.naive_utc())
6061
}
6162

6263
/// converts a `i32` representing a `time32(s)` to [`NaiveDateTime`]
@@ -130,45 +131,48 @@ pub fn time_to_time64ns(v: NaiveTime) -> i64 {
130131
/// converts a `i64` representing a `timestamp(s)` to [`NaiveDateTime`]
131132
#[inline]
132133
pub fn timestamp_s_to_datetime(v: i64) -> Option<NaiveDateTime> {
133-
NaiveDateTime::from_timestamp_opt(v, 0)
134+
Some(DateTime::from_timestamp(v, 0)?.naive_utc())
134135
}
135136

136137
/// converts a `i64` representing a `timestamp(ms)` to [`NaiveDateTime`]
137138
#[inline]
138139
pub fn timestamp_ms_to_datetime(v: i64) -> Option<NaiveDateTime> {
139140
let (sec, milli_sec) = split_second(v, MILLISECONDS);
140141

141-
NaiveDateTime::from_timestamp_opt(
142+
let datetime = DateTime::from_timestamp(
142143
// extract seconds from milliseconds
143144
sec,
144145
// discard extracted seconds and convert milliseconds to nanoseconds
145146
milli_sec * MICROSECONDS as u32,
146-
)
147+
)?;
148+
Some(datetime.naive_utc())
147149
}
148150

149151
/// converts a `i64` representing a `timestamp(us)` to [`NaiveDateTime`]
150152
#[inline]
151153
pub fn timestamp_us_to_datetime(v: i64) -> Option<NaiveDateTime> {
152154
let (sec, micro_sec) = split_second(v, MICROSECONDS);
153155

154-
NaiveDateTime::from_timestamp_opt(
156+
let datetime = DateTime::from_timestamp(
155157
// extract seconds from microseconds
156158
sec,
157159
// discard extracted seconds and convert microseconds to nanoseconds
158160
micro_sec * MILLISECONDS as u32,
159-
)
161+
)?;
162+
Some(datetime.naive_utc())
160163
}
161164

162165
/// converts a `i64` representing a `timestamp(ns)` to [`NaiveDateTime`]
163166
#[inline]
164167
pub fn timestamp_ns_to_datetime(v: i64) -> Option<NaiveDateTime> {
165168
let (sec, nano_sec) = split_second(v, NANOSECONDS);
166169

167-
NaiveDateTime::from_timestamp_opt(
170+
let datetime = DateTime::from_timestamp(
168171
// extract seconds from nanoseconds
169172
sec, // discard extracted seconds
170173
nano_sec,
171-
)
174+
)?;
175+
Some(datetime.naive_utc())
172176
}
173177

174178
#[inline]
@@ -179,13 +183,13 @@ pub(crate) fn split_second(v: i64, base: i64) -> (i64, u32) {
179183
/// converts a `i64` representing a `duration(s)` to [`Duration`]
180184
#[inline]
181185
pub fn duration_s_to_duration(v: i64) -> Duration {
182-
Duration::seconds(v)
186+
Duration::try_seconds(v).unwrap()
183187
}
184188

185189
/// converts a `i64` representing a `duration(ms)` to [`Duration`]
186190
#[inline]
187191
pub fn duration_ms_to_duration(v: i64) -> Duration {
188-
Duration::milliseconds(v)
192+
Duration::try_milliseconds(v).unwrap()
189193
}
190194

191195
/// converts a `i64` representing a `duration(us)` to [`Duration`]
@@ -272,57 +276,57 @@ mod tests {
272276
date64_to_datetime, split_second, timestamp_ms_to_datetime, timestamp_ns_to_datetime,
273277
timestamp_us_to_datetime, NANOSECONDS,
274278
};
275-
use chrono::NaiveDateTime;
279+
use chrono::DateTime;
276280

277281
#[test]
278282
fn negative_input_timestamp_ns_to_datetime() {
279283
assert_eq!(
280284
timestamp_ns_to_datetime(-1),
281-
NaiveDateTime::from_timestamp_opt(-1, 999_999_999)
285+
DateTime::from_timestamp(-1, 999_999_999).map(|x| x.naive_utc())
282286
);
283287

284288
assert_eq!(
285289
timestamp_ns_to_datetime(-1_000_000_001),
286-
NaiveDateTime::from_timestamp_opt(-2, 999_999_999)
290+
DateTime::from_timestamp(-2, 999_999_999).map(|x| x.naive_utc())
287291
);
288292
}
289293

290294
#[test]
291295
fn negative_input_timestamp_us_to_datetime() {
292296
assert_eq!(
293297
timestamp_us_to_datetime(-1),
294-
NaiveDateTime::from_timestamp_opt(-1, 999_999_000)
298+
DateTime::from_timestamp(-1, 999_999_000).map(|x| x.naive_utc())
295299
);
296300

297301
assert_eq!(
298302
timestamp_us_to_datetime(-1_000_001),
299-
NaiveDateTime::from_timestamp_opt(-2, 999_999_000)
303+
DateTime::from_timestamp(-2, 999_999_000).map(|x| x.naive_utc())
300304
);
301305
}
302306

303307
#[test]
304308
fn negative_input_timestamp_ms_to_datetime() {
305309
assert_eq!(
306310
timestamp_ms_to_datetime(-1),
307-
NaiveDateTime::from_timestamp_opt(-1, 999_000_000)
311+
DateTime::from_timestamp(-1, 999_000_000).map(|x| x.naive_utc())
308312
);
309313

310314
assert_eq!(
311315
timestamp_ms_to_datetime(-1_001),
312-
NaiveDateTime::from_timestamp_opt(-2, 999_000_000)
316+
DateTime::from_timestamp(-2, 999_000_000).map(|x| x.naive_utc())
313317
);
314318
}
315319

316320
#[test]
317321
fn negative_input_date64_to_datetime() {
318322
assert_eq!(
319323
date64_to_datetime(-1),
320-
NaiveDateTime::from_timestamp_opt(-1, 999_000_000)
324+
DateTime::from_timestamp(-1, 999_000_000).map(|x| x.naive_utc())
321325
);
322326

323327
assert_eq!(
324328
date64_to_datetime(-1_001),
325-
NaiveDateTime::from_timestamp_opt(-2, 999_000_000)
329+
DateTime::from_timestamp(-2, 999_000_000).map(|x| x.naive_utc())
326330
);
327331
}
328332

arrow-array/src/types.rs

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -390,31 +390,34 @@ impl ArrowTimestampType for TimestampSecondType {
390390
const UNIT: TimeUnit = TimeUnit::Second;
391391

392392
fn make_value(naive: NaiveDateTime) -> Option<i64> {
393-
Some(naive.timestamp())
393+
Some(naive.and_utc().timestamp())
394394
}
395395
}
396396
impl ArrowTimestampType for TimestampMillisecondType {
397397
const UNIT: TimeUnit = TimeUnit::Millisecond;
398398

399399
fn make_value(naive: NaiveDateTime) -> Option<i64> {
400-
let millis = naive.timestamp().checked_mul(1_000)?;
401-
millis.checked_add(naive.timestamp_subsec_millis() as i64)
400+
let utc = naive.and_utc();
401+
let millis = utc.timestamp().checked_mul(1_000)?;
402+
millis.checked_add(utc.timestamp_subsec_millis() as i64)
402403
}
403404
}
404405
impl ArrowTimestampType for TimestampMicrosecondType {
405406
const UNIT: TimeUnit = TimeUnit::Microsecond;
406407

407408
fn make_value(naive: NaiveDateTime) -> Option<i64> {
408-
let micros = naive.timestamp().checked_mul(1_000_000)?;
409-
micros.checked_add(naive.timestamp_subsec_micros() as i64)
409+
let utc = naive.and_utc();
410+
let micros = utc.timestamp().checked_mul(1_000_000)?;
411+
micros.checked_add(utc.timestamp_subsec_micros() as i64)
410412
}
411413
}
412414
impl ArrowTimestampType for TimestampNanosecondType {
413415
const UNIT: TimeUnit = TimeUnit::Nanosecond;
414416

415417
fn make_value(naive: NaiveDateTime) -> Option<i64> {
416-
let nanos = naive.timestamp().checked_mul(1_000_000_000)?;
417-
nanos.checked_add(naive.timestamp_subsec_nanos() as i64)
418+
let utc = naive.and_utc();
419+
let nanos = utc.timestamp().checked_mul(1_000_000_000)?;
420+
nanos.checked_add(utc.timestamp_subsec_nanos() as i64)
418421
}
419422
}
420423

@@ -438,7 +441,7 @@ fn add_day_time<T: ArrowTimestampType>(
438441
let (days, ms) = IntervalDayTimeType::to_parts(delta);
439442
let res = as_datetime_with_timezone::<T>(timestamp, tz)?;
440443
let res = add_days_datetime(res, days)?;
441-
let res = res.checked_add_signed(Duration::milliseconds(ms as i64))?;
444+
let res = res.checked_add_signed(Duration::try_milliseconds(ms as i64)?)?;
442445
let res = res.naive_utc();
443446
T::make_value(res)
444447
}
@@ -477,7 +480,7 @@ fn subtract_day_time<T: ArrowTimestampType>(
477480
let (days, ms) = IntervalDayTimeType::to_parts(delta);
478481
let res = as_datetime_with_timezone::<T>(timestamp, tz)?;
479482
let res = sub_days_datetime(res, days)?;
480-
let res = res.checked_sub_signed(Duration::milliseconds(ms as i64))?;
483+
let res = res.checked_sub_signed(Duration::try_milliseconds(ms as i64)?)?;
481484
let res = res.naive_utc();
482485
T::make_value(res)
483486
}
@@ -1001,7 +1004,7 @@ impl Date32Type {
10011004
/// * `i` - The Date32Type to convert
10021005
pub fn to_naive_date(i: <Date32Type as ArrowPrimitiveType>::Native) -> NaiveDate {
10031006
let epoch = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap();
1004-
epoch.add(Duration::days(i as i64))
1007+
epoch.add(Duration::try_days(i as i64).unwrap())
10051008
}
10061009

10071010
/// Converts a chrono::NaiveDate into an arrow Date32Type
@@ -1042,8 +1045,8 @@ impl Date32Type {
10421045
) -> <Date32Type as ArrowPrimitiveType>::Native {
10431046
let (days, ms) = IntervalDayTimeType::to_parts(delta);
10441047
let res = Date32Type::to_naive_date(date);
1045-
let res = res.add(Duration::days(days as i64));
1046-
let res = res.add(Duration::milliseconds(ms as i64));
1048+
let res = res.add(Duration::try_days(days as i64).unwrap());
1049+
let res = res.add(Duration::try_milliseconds(ms as i64).unwrap());
10471050
Date32Type::from_naive_date(res)
10481051
}
10491052

@@ -1060,7 +1063,7 @@ impl Date32Type {
10601063
let (months, days, nanos) = IntervalMonthDayNanoType::to_parts(delta);
10611064
let res = Date32Type::to_naive_date(date);
10621065
let res = shift_months(res, months);
1063-
let res = res.add(Duration::days(days as i64));
1066+
let res = res.add(Duration::try_days(days as i64).unwrap());
10641067
let res = res.add(Duration::nanoseconds(nanos));
10651068
Date32Type::from_naive_date(res)
10661069
}
@@ -1093,8 +1096,8 @@ impl Date32Type {
10931096
) -> <Date32Type as ArrowPrimitiveType>::Native {
10941097
let (days, ms) = IntervalDayTimeType::to_parts(delta);
10951098
let res = Date32Type::to_naive_date(date);
1096-
let res = res.sub(Duration::days(days as i64));
1097-
let res = res.sub(Duration::milliseconds(ms as i64));
1099+
let res = res.sub(Duration::try_days(days as i64).unwrap());
1100+
let res = res.sub(Duration::try_milliseconds(ms as i64).unwrap());
10981101
Date32Type::from_naive_date(res)
10991102
}
11001103

@@ -1111,7 +1114,7 @@ impl Date32Type {
11111114
let (months, days, nanos) = IntervalMonthDayNanoType::to_parts(delta);
11121115
let res = Date32Type::to_naive_date(date);
11131116
let res = shift_months(res, -months);
1114-
let res = res.sub(Duration::days(days as i64));
1117+
let res = res.sub(Duration::try_days(days as i64).unwrap());
11151118
let res = res.sub(Duration::nanoseconds(nanos));
11161119
Date32Type::from_naive_date(res)
11171120
}
@@ -1125,7 +1128,7 @@ impl Date64Type {
11251128
/// * `i` - The Date64Type to convert
11261129
pub fn to_naive_date(i: <Date64Type as ArrowPrimitiveType>::Native) -> NaiveDate {
11271130
let epoch = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap();
1128-
epoch.add(Duration::milliseconds(i))
1131+
epoch.add(Duration::try_milliseconds(i).unwrap())
11291132
}
11301133

11311134
/// Converts a chrono::NaiveDate into an arrow Date64Type
@@ -1166,8 +1169,8 @@ impl Date64Type {
11661169
) -> <Date64Type as ArrowPrimitiveType>::Native {
11671170
let (days, ms) = IntervalDayTimeType::to_parts(delta);
11681171
let res = Date64Type::to_naive_date(date);
1169-
let res = res.add(Duration::days(days as i64));
1170-
let res = res.add(Duration::milliseconds(ms as i64));
1172+
let res = res.add(Duration::try_days(days as i64).unwrap());
1173+
let res = res.add(Duration::try_milliseconds(ms as i64).unwrap());
11711174
Date64Type::from_naive_date(res)
11721175
}
11731176

@@ -1184,7 +1187,7 @@ impl Date64Type {
11841187
let (months, days, nanos) = IntervalMonthDayNanoType::to_parts(delta);
11851188
let res = Date64Type::to_naive_date(date);
11861189
let res = shift_months(res, months);
1187-
let res = res.add(Duration::days(days as i64));
1190+
let res = res.add(Duration::try_days(days as i64).unwrap());
11881191
let res = res.add(Duration::nanoseconds(nanos));
11891192
Date64Type::from_naive_date(res)
11901193
}
@@ -1217,8 +1220,8 @@ impl Date64Type {
12171220
) -> <Date64Type as ArrowPrimitiveType>::Native {
12181221
let (days, ms) = IntervalDayTimeType::to_parts(delta);
12191222
let res = Date64Type::to_naive_date(date);
1220-
let res = res.sub(Duration::days(days as i64));
1221-
let res = res.sub(Duration::milliseconds(ms as i64));
1223+
let res = res.sub(Duration::try_days(days as i64).unwrap());
1224+
let res = res.sub(Duration::try_milliseconds(ms as i64).unwrap());
12221225
Date64Type::from_naive_date(res)
12231226
}
12241227

@@ -1235,7 +1238,7 @@ impl Date64Type {
12351238
let (months, days, nanos) = IntervalMonthDayNanoType::to_parts(delta);
12361239
let res = Date64Type::to_naive_date(date);
12371240
let res = shift_months(res, -months);
1238-
let res = res.sub(Duration::days(days as i64));
1241+
let res = res.sub(Duration::try_days(days as i64).unwrap());
12391242
let res = res.sub(Duration::nanoseconds(nanos));
12401243
Date64Type::from_naive_date(res)
12411244
}

0 commit comments

Comments
 (0)