use chrono::{Duration, NaiveDateTime, NaiveTime};
pub(crate) const SECONDS_IN_DAY: i64 = 86_400;
pub(crate) const MILLISECONDS: i64 = 1_000;
pub(crate) const MICROSECONDS: i64 = 1_000_000;
pub(crate) const NANOSECONDS: i64 = 1_000_000_000;
pub(crate) const MILLISECONDS_IN_DAY: i64 = SECONDS_IN_DAY * MILLISECONDS;
pub(crate) const EPOCH_DAYS_FROM_CE: i32 = 719_163;
#[inline]
pub fn date32_to_datetime(v: i32) -> NaiveDateTime {
NaiveDateTime::from_timestamp(v as i64 * SECONDS_IN_DAY, 0)
}
#[inline]
pub fn date64_to_datetime(v: i64) -> NaiveDateTime {
let (sec, milli_sec) = split_second(v, MILLISECONDS);
NaiveDateTime::from_timestamp(
sec,
milli_sec * MICROSECONDS as u32,
)
}
#[inline]
pub fn time32s_to_time(v: i32) -> NaiveTime {
NaiveTime::from_num_seconds_from_midnight(v as u32, 0)
}
#[inline]
pub fn time32ms_to_time(v: i32) -> NaiveTime {
let v = v as i64;
NaiveTime::from_num_seconds_from_midnight(
(v / MILLISECONDS) as u32,
(v % MILLISECONDS * MICROSECONDS) as u32,
)
}
#[inline]
pub fn time64us_to_time(v: i64) -> NaiveTime {
NaiveTime::from_num_seconds_from_midnight(
(v / MICROSECONDS) as u32,
(v % MICROSECONDS * MILLISECONDS) as u32,
)
}
#[inline]
pub fn time64ns_to_time(v: i64) -> NaiveTime {
NaiveTime::from_num_seconds_from_midnight(
(v / NANOSECONDS) as u32,
(v % NANOSECONDS) as u32,
)
}
#[inline]
pub fn timestamp_s_to_datetime(v: i64) -> NaiveDateTime {
NaiveDateTime::from_timestamp(v, 0)
}
#[inline]
pub fn timestamp_ms_to_datetime(v: i64) -> NaiveDateTime {
let (sec, milli_sec) = split_second(v, MILLISECONDS);
NaiveDateTime::from_timestamp(
sec,
milli_sec * MICROSECONDS as u32,
)
}
#[inline]
pub fn timestamp_us_to_datetime(v: i64) -> NaiveDateTime {
let (sec, micro_sec) = split_second(v, MICROSECONDS);
NaiveDateTime::from_timestamp(
sec,
micro_sec * MILLISECONDS as u32,
)
}
#[inline]
pub fn timestamp_ns_to_datetime(v: i64) -> NaiveDateTime {
let (sec, nano_sec) = split_second(v, NANOSECONDS);
NaiveDateTime::from_timestamp(
sec, nano_sec,
)
}
#[inline]
pub(crate) fn split_second(v: i64, base: i64) -> (i64, u32) {
if v < 0 {
let v = -v;
let mut seconds = v / base;
let mut part = v % base;
if part > 0 {
seconds += 1;
part = base - part;
}
(-seconds, part as u32)
} else {
(v / base, (v % base) as u32)
}
}
#[inline]
pub fn duration_s_to_duration(v: i64) -> Duration {
Duration::seconds(v)
}
#[inline]
pub fn duration_ms_to_duration(v: i64) -> Duration {
Duration::milliseconds(v)
}
#[inline]
pub fn duration_us_to_duration(v: i64) -> Duration {
Duration::microseconds(v)
}
#[inline]
pub fn duration_ns_to_duration(v: i64) -> Duration {
Duration::nanoseconds(v)
}
#[cfg(test)]
mod tests {
use crate::temporal_conversions::{
date64_to_datetime, split_second, timestamp_ms_to_datetime,
timestamp_ns_to_datetime, timestamp_us_to_datetime, NANOSECONDS,
};
use chrono::NaiveDateTime;
#[test]
fn negative_input_timestamp_ns_to_datetime() {
assert_eq!(
timestamp_ns_to_datetime(-1),
NaiveDateTime::from_timestamp(-1, 999_999_999)
);
assert_eq!(
timestamp_ns_to_datetime(-1_000_000_001),
NaiveDateTime::from_timestamp(-2, 999_999_999)
);
}
#[test]
fn negative_input_timestamp_us_to_datetime() {
assert_eq!(
timestamp_us_to_datetime(-1),
NaiveDateTime::from_timestamp(-1, 999_999_000)
);
assert_eq!(
timestamp_us_to_datetime(-1_000_001),
NaiveDateTime::from_timestamp(-2, 999_999_000)
);
}
#[test]
fn negative_input_timestamp_ms_to_datetime() {
assert_eq!(
timestamp_ms_to_datetime(-1),
NaiveDateTime::from_timestamp(-1, 999_000_000)
);
assert_eq!(
timestamp_ms_to_datetime(-1_001),
NaiveDateTime::from_timestamp(-2, 999_000_000)
);
}
#[test]
fn negative_input_date64_to_datetime() {
assert_eq!(
date64_to_datetime(-1),
NaiveDateTime::from_timestamp(-1, 999_000_000)
);
assert_eq!(
date64_to_datetime(-1_001),
NaiveDateTime::from_timestamp(-2, 999_000_000)
);
}
#[test]
fn test_split_seconds() {
let (sec, nano_sec) = split_second(100, NANOSECONDS);
assert_eq!(sec, 0);
assert_eq!(nano_sec, 100);
let (sec, nano_sec) = split_second(123_000_000_456, NANOSECONDS);
assert_eq!(sec, 123);
assert_eq!(nano_sec, 456);
let (sec, nano_sec) = split_second(-1, NANOSECONDS);
assert_eq!(sec, -1);
assert_eq!(nano_sec, 999_999_999);
let (sec, nano_sec) = split_second(-123_000_000_001, NANOSECONDS);
assert_eq!(sec, -124);
assert_eq!(nano_sec, 999_999_999);
}
}