jhorstmann commented on code in PR #2326:
URL: https://github.com/apache/arrow-rs/pull/2326#discussion_r944429889
##########
arrow/src/temporal_conversions.rs:
##########
@@ -101,36 +103,59 @@ pub fn timestamp_s_to_datetime(v: i64) -> NaiveDateTime {
/// converts a `i64` representing a `timestamp(ms)` to [`NaiveDateTime`]
#[inline]
pub fn timestamp_ms_to_datetime(v: i64) -> NaiveDateTime {
+ let (sec, milli_sec) = split_second(v, MILLISECONDS);
+
NaiveDateTime::from_timestamp(
// extract seconds from milliseconds
- v / MILLISECONDS,
+ sec,
// discard extracted seconds and convert milliseconds to nanoseconds
- (v % MILLISECONDS * MICROSECONDS) as u32,
+ milli_sec * MICROSECONDS as u32,
)
}
/// converts a `i64` representing a `timestamp(us)` to [`NaiveDateTime`]
#[inline]
pub fn timestamp_us_to_datetime(v: i64) -> NaiveDateTime {
+ let (sec, micro_sec) = split_second(v, MICROSECONDS);
+
NaiveDateTime::from_timestamp(
// extract seconds from microseconds
- v / MICROSECONDS,
+ sec,
// discard extracted seconds and convert microseconds to nanoseconds
- (v % MICROSECONDS * MILLISECONDS) as u32,
+ micro_sec * MILLISECONDS as u32,
)
}
/// converts a `i64` representing a `timestamp(ns)` to [`NaiveDateTime`]
#[inline]
pub fn timestamp_ns_to_datetime(v: i64) -> NaiveDateTime {
+ let (sec, nano_sec) = split_second(v, NANOSECONDS);
+
NaiveDateTime::from_timestamp(
// extract seconds from nanoseconds
- v / NANOSECONDS,
- // discard extracted seconds
- if v > 0 { (v % NANOSECONDS) as u32 } else { 0 },
+ sec, // discard extracted seconds
+ nano_sec,
)
}
+///
+#[inline]
+pub(crate) fn split_second(v: i64, base: i64) -> (i64, u32) {
+ if v < 0 {
Review Comment:
Might be possible to simplify this using builtin [`div_euclid` /
`rem_euclid`](https://doc.rust-lang.org/std/primitive.i64.html#method.div_euclid)
functions
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]