Skip to content

Commit

Permalink
Update latest chrono (apache#5479)
Browse files Browse the repository at this point in the history
  • Loading branch information
tustvold authored Mar 7, 2024
1 parent 8caec3b commit ae1d10f
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 71 deletions.
5 changes: 4 additions & 1 deletion arrow-array/src/array/primitive_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1557,7 +1557,10 @@ mod tests {
// roundtrip to and from datetime
assert_eq!(
1550902545147,
arr.value_as_datetime(i).unwrap().timestamp_millis()
arr.value_as_datetime(i)
.unwrap()
.and_utc()
.timestamp_millis()
);
} else {
assert!(arr.is_null(i));
Expand Down
46 changes: 25 additions & 21 deletions arrow-array/src/temporal_conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,21 @@ 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) -> Option<NaiveDateTime> {
NaiveDateTime::from_timestamp_opt(v as i64 * SECONDS_IN_DAY, 0)
Some(DateTime::from_timestamp(v as i64 * SECONDS_IN_DAY, 0)?.naive_utc())
}

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

NaiveDateTime::from_timestamp_opt(
let datetime = DateTime::from_timestamp(
// extract seconds from milliseconds
sec,
// discard extracted seconds and convert milliseconds to nanoseconds
milli_sec * MICROSECONDS as u32,
)
)?;
Some(datetime.naive_utc())
}

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

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

NaiveDateTime::from_timestamp_opt(
let datetime = DateTime::from_timestamp(
// extract seconds from milliseconds
sec,
// discard extracted seconds and convert milliseconds to nanoseconds
milli_sec * MICROSECONDS as u32,
)
)?;
Some(datetime.naive_utc())
}

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

NaiveDateTime::from_timestamp_opt(
let datetime = DateTime::from_timestamp(
// extract seconds from microseconds
sec,
// discard extracted seconds and convert microseconds to nanoseconds
micro_sec * MILLISECONDS as u32,
)
)?;
Some(datetime.naive_utc())
}

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

NaiveDateTime::from_timestamp_opt(
let datetime = DateTime::from_timestamp(
// extract seconds from nanoseconds
sec, // discard extracted seconds
nano_sec,
)
)?;
Some(datetime.naive_utc())
}

#[inline]
Expand All @@ -179,13 +183,13 @@ pub(crate) fn split_second(v: i64, base: i64) -> (i64, u32) {
/// converts a `i64` representing a `duration(s)` to [`Duration`]
#[inline]
pub fn duration_s_to_duration(v: i64) -> Duration {
Duration::seconds(v)
Duration::try_seconds(v).unwrap()
}

/// converts a `i64` representing a `duration(ms)` to [`Duration`]
#[inline]
pub fn duration_ms_to_duration(v: i64) -> Duration {
Duration::milliseconds(v)
Duration::try_milliseconds(v).unwrap()
}

/// converts a `i64` representing a `duration(us)` to [`Duration`]
Expand Down Expand Up @@ -272,57 +276,57 @@ mod tests {
date64_to_datetime, split_second, timestamp_ms_to_datetime, timestamp_ns_to_datetime,
timestamp_us_to_datetime, NANOSECONDS,
};
use chrono::NaiveDateTime;
use chrono::DateTime;

#[test]
fn negative_input_timestamp_ns_to_datetime() {
assert_eq!(
timestamp_ns_to_datetime(-1),
NaiveDateTime::from_timestamp_opt(-1, 999_999_999)
DateTime::from_timestamp(-1, 999_999_999).map(|x| x.naive_utc())
);

assert_eq!(
timestamp_ns_to_datetime(-1_000_000_001),
NaiveDateTime::from_timestamp_opt(-2, 999_999_999)
DateTime::from_timestamp(-2, 999_999_999).map(|x| x.naive_utc())
);
}

#[test]
fn negative_input_timestamp_us_to_datetime() {
assert_eq!(
timestamp_us_to_datetime(-1),
NaiveDateTime::from_timestamp_opt(-1, 999_999_000)
DateTime::from_timestamp(-1, 999_999_000).map(|x| x.naive_utc())
);

assert_eq!(
timestamp_us_to_datetime(-1_000_001),
NaiveDateTime::from_timestamp_opt(-2, 999_999_000)
DateTime::from_timestamp(-2, 999_999_000).map(|x| x.naive_utc())
);
}

#[test]
fn negative_input_timestamp_ms_to_datetime() {
assert_eq!(
timestamp_ms_to_datetime(-1),
NaiveDateTime::from_timestamp_opt(-1, 999_000_000)
DateTime::from_timestamp(-1, 999_000_000).map(|x| x.naive_utc())
);

assert_eq!(
timestamp_ms_to_datetime(-1_001),
NaiveDateTime::from_timestamp_opt(-2, 999_000_000)
DateTime::from_timestamp(-2, 999_000_000).map(|x| x.naive_utc())
);
}

#[test]
fn negative_input_date64_to_datetime() {
assert_eq!(
date64_to_datetime(-1),
NaiveDateTime::from_timestamp_opt(-1, 999_000_000)
DateTime::from_timestamp(-1, 999_000_000).map(|x| x.naive_utc())
);

assert_eq!(
date64_to_datetime(-1_001),
NaiveDateTime::from_timestamp_opt(-2, 999_000_000)
DateTime::from_timestamp(-2, 999_000_000).map(|x| x.naive_utc())
);
}

Expand Down
49 changes: 26 additions & 23 deletions arrow-array/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,31 +390,34 @@ impl ArrowTimestampType for TimestampSecondType {
const UNIT: TimeUnit = TimeUnit::Second;

fn make_value(naive: NaiveDateTime) -> Option<i64> {
Some(naive.timestamp())
Some(naive.and_utc().timestamp())
}
}
impl ArrowTimestampType for TimestampMillisecondType {
const UNIT: TimeUnit = TimeUnit::Millisecond;

fn make_value(naive: NaiveDateTime) -> Option<i64> {
let millis = naive.timestamp().checked_mul(1_000)?;
millis.checked_add(naive.timestamp_subsec_millis() as i64)
let utc = naive.and_utc();
let millis = utc.timestamp().checked_mul(1_000)?;
millis.checked_add(utc.timestamp_subsec_millis() as i64)
}
}
impl ArrowTimestampType for TimestampMicrosecondType {
const UNIT: TimeUnit = TimeUnit::Microsecond;

fn make_value(naive: NaiveDateTime) -> Option<i64> {
let micros = naive.timestamp().checked_mul(1_000_000)?;
micros.checked_add(naive.timestamp_subsec_micros() as i64)
let utc = naive.and_utc();
let micros = utc.timestamp().checked_mul(1_000_000)?;
micros.checked_add(utc.timestamp_subsec_micros() as i64)
}
}
impl ArrowTimestampType for TimestampNanosecondType {
const UNIT: TimeUnit = TimeUnit::Nanosecond;

fn make_value(naive: NaiveDateTime) -> Option<i64> {
let nanos = naive.timestamp().checked_mul(1_000_000_000)?;
nanos.checked_add(naive.timestamp_subsec_nanos() as i64)
let utc = naive.and_utc();
let nanos = utc.timestamp().checked_mul(1_000_000_000)?;
nanos.checked_add(utc.timestamp_subsec_nanos() as i64)
}
}

Expand All @@ -438,7 +441,7 @@ fn add_day_time<T: ArrowTimestampType>(
let (days, ms) = IntervalDayTimeType::to_parts(delta);
let res = as_datetime_with_timezone::<T>(timestamp, tz)?;
let res = add_days_datetime(res, days)?;
let res = res.checked_add_signed(Duration::milliseconds(ms as i64))?;
let res = res.checked_add_signed(Duration::try_milliseconds(ms as i64)?)?;
let res = res.naive_utc();
T::make_value(res)
}
Expand Down Expand Up @@ -477,7 +480,7 @@ fn subtract_day_time<T: ArrowTimestampType>(
let (days, ms) = IntervalDayTimeType::to_parts(delta);
let res = as_datetime_with_timezone::<T>(timestamp, tz)?;
let res = sub_days_datetime(res, days)?;
let res = res.checked_sub_signed(Duration::milliseconds(ms as i64))?;
let res = res.checked_sub_signed(Duration::try_milliseconds(ms as i64)?)?;
let res = res.naive_utc();
T::make_value(res)
}
Expand Down Expand Up @@ -1001,7 +1004,7 @@ impl Date32Type {
/// * `i` - The Date32Type to convert
pub fn to_naive_date(i: <Date32Type as ArrowPrimitiveType>::Native) -> NaiveDate {
let epoch = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap();
epoch.add(Duration::days(i as i64))
epoch.add(Duration::try_days(i as i64).unwrap())
}

/// Converts a chrono::NaiveDate into an arrow Date32Type
Expand Down Expand Up @@ -1042,8 +1045,8 @@ impl Date32Type {
) -> <Date32Type as ArrowPrimitiveType>::Native {
let (days, ms) = IntervalDayTimeType::to_parts(delta);
let res = Date32Type::to_naive_date(date);
let res = res.add(Duration::days(days as i64));
let res = res.add(Duration::milliseconds(ms as i64));
let res = res.add(Duration::try_days(days as i64).unwrap());
let res = res.add(Duration::try_milliseconds(ms as i64).unwrap());
Date32Type::from_naive_date(res)
}

Expand All @@ -1060,7 +1063,7 @@ impl Date32Type {
let (months, days, nanos) = IntervalMonthDayNanoType::to_parts(delta);
let res = Date32Type::to_naive_date(date);
let res = shift_months(res, months);
let res = res.add(Duration::days(days as i64));
let res = res.add(Duration::try_days(days as i64).unwrap());
let res = res.add(Duration::nanoseconds(nanos));
Date32Type::from_naive_date(res)
}
Expand Down Expand Up @@ -1093,8 +1096,8 @@ impl Date32Type {
) -> <Date32Type as ArrowPrimitiveType>::Native {
let (days, ms) = IntervalDayTimeType::to_parts(delta);
let res = Date32Type::to_naive_date(date);
let res = res.sub(Duration::days(days as i64));
let res = res.sub(Duration::milliseconds(ms as i64));
let res = res.sub(Duration::try_days(days as i64).unwrap());
let res = res.sub(Duration::try_milliseconds(ms as i64).unwrap());
Date32Type::from_naive_date(res)
}

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

/// Converts a chrono::NaiveDate into an arrow Date64Type
Expand Down Expand Up @@ -1166,8 +1169,8 @@ impl Date64Type {
) -> <Date64Type as ArrowPrimitiveType>::Native {
let (days, ms) = IntervalDayTimeType::to_parts(delta);
let res = Date64Type::to_naive_date(date);
let res = res.add(Duration::days(days as i64));
let res = res.add(Duration::milliseconds(ms as i64));
let res = res.add(Duration::try_days(days as i64).unwrap());
let res = res.add(Duration::try_milliseconds(ms as i64).unwrap());
Date64Type::from_naive_date(res)
}

Expand All @@ -1184,7 +1187,7 @@ impl Date64Type {
let (months, days, nanos) = IntervalMonthDayNanoType::to_parts(delta);
let res = Date64Type::to_naive_date(date);
let res = shift_months(res, months);
let res = res.add(Duration::days(days as i64));
let res = res.add(Duration::try_days(days as i64).unwrap());
let res = res.add(Duration::nanoseconds(nanos));
Date64Type::from_naive_date(res)
}
Expand Down Expand Up @@ -1217,8 +1220,8 @@ impl Date64Type {
) -> <Date64Type as ArrowPrimitiveType>::Native {
let (days, ms) = IntervalDayTimeType::to_parts(delta);
let res = Date64Type::to_naive_date(date);
let res = res.sub(Duration::days(days as i64));
let res = res.sub(Duration::milliseconds(ms as i64));
let res = res.sub(Duration::try_days(days as i64).unwrap());
let res = res.sub(Duration::try_milliseconds(ms as i64).unwrap());
Date64Type::from_naive_date(res)
}

Expand All @@ -1235,7 +1238,7 @@ impl Date64Type {
let (months, days, nanos) = IntervalMonthDayNanoType::to_parts(delta);
let res = Date64Type::to_naive_date(date);
let res = shift_months(res, -months);
let res = res.sub(Duration::days(days as i64));
let res = res.sub(Duration::try_days(days as i64).unwrap());
let res = res.sub(Duration::nanoseconds(nanos));
Date64Type::from_naive_date(res)
}
Expand Down
Loading

0 comments on commit ae1d10f

Please sign in to comment.