1use crate::timezone::Tz;
21use crate::ArrowPrimitiveType;
22use arrow_schema::{DataType, TimeUnit};
23use chrono::{DateTime, Duration, NaiveDate, NaiveDateTime, NaiveTime, TimeZone, Timelike, Utc};
24
25pub const SECONDS_IN_DAY: i64 = 86_400;
27pub const MILLISECONDS: i64 = 1_000;
29pub const MICROSECONDS: i64 = 1_000_000;
31pub const NANOSECONDS: i64 = 1_000_000_000;
33
34pub const MILLISECONDS_IN_DAY: i64 = SECONDS_IN_DAY * MILLISECONDS;
36pub const MICROSECONDS_IN_DAY: i64 = SECONDS_IN_DAY * MICROSECONDS;
38pub const NANOSECONDS_IN_DAY: i64 = SECONDS_IN_DAY * NANOSECONDS;
40pub const EPOCH_DAYS_FROM_CE: i32 = 719_163;
42
43#[inline]
45pub fn date32_to_datetime(v: i32) -> Option<NaiveDateTime> {
46 Some(DateTime::from_timestamp(v as i64 * SECONDS_IN_DAY, 0)?.naive_utc())
47}
48
49#[inline]
51pub fn date64_to_datetime(v: i64) -> Option<NaiveDateTime> {
52 let (sec, milli_sec) = split_second(v, MILLISECONDS);
53
54 let datetime = DateTime::from_timestamp(
55 sec,
57 milli_sec * MICROSECONDS as u32,
59 )?;
60 Some(datetime.naive_utc())
61}
62
63#[inline]
65pub fn time32s_to_time(v: i32) -> Option<NaiveTime> {
66 NaiveTime::from_num_seconds_from_midnight_opt(v as u32, 0)
67}
68
69#[inline]
71pub fn time32ms_to_time(v: i32) -> Option<NaiveTime> {
72 let v = v as i64;
73 NaiveTime::from_num_seconds_from_midnight_opt(
74 (v / MILLISECONDS) as u32,
76 (v % MILLISECONDS * MICROSECONDS) as u32,
79 )
80}
81
82#[inline]
84pub fn time64us_to_time(v: i64) -> Option<NaiveTime> {
85 NaiveTime::from_num_seconds_from_midnight_opt(
86 (v / MICROSECONDS) as u32,
88 (v % MICROSECONDS * MILLISECONDS) as u32,
91 )
92}
93
94#[inline]
96pub fn time64ns_to_time(v: i64) -> Option<NaiveTime> {
97 NaiveTime::from_num_seconds_from_midnight_opt(
98 (v / NANOSECONDS) as u32,
100 (v % NANOSECONDS) as u32,
102 )
103}
104
105#[inline]
107pub fn time_to_time32s(v: NaiveTime) -> i32 {
108 v.num_seconds_from_midnight() as i32
109}
110
111#[inline]
113pub fn time_to_time32ms(v: NaiveTime) -> i32 {
114 (v.num_seconds_from_midnight() as i64 * MILLISECONDS
115 + v.nanosecond() as i64 * MILLISECONDS / NANOSECONDS) as i32
116}
117
118#[inline]
120pub fn time_to_time64us(v: NaiveTime) -> i64 {
121 v.num_seconds_from_midnight() as i64 * MICROSECONDS
122 + v.nanosecond() as i64 * MICROSECONDS / NANOSECONDS
123}
124
125#[inline]
127pub fn time_to_time64ns(v: NaiveTime) -> i64 {
128 v.num_seconds_from_midnight() as i64 * NANOSECONDS + v.nanosecond() as i64
129}
130
131#[inline]
133pub fn timestamp_s_to_datetime(v: i64) -> Option<NaiveDateTime> {
134 Some(DateTime::from_timestamp(v, 0)?.naive_utc())
135}
136
137#[inline]
139pub fn timestamp_ms_to_datetime(v: i64) -> Option<NaiveDateTime> {
140 let (sec, milli_sec) = split_second(v, MILLISECONDS);
141
142 let datetime = DateTime::from_timestamp(
143 sec,
145 milli_sec * MICROSECONDS as u32,
147 )?;
148 Some(datetime.naive_utc())
149}
150
151#[inline]
153pub fn timestamp_us_to_datetime(v: i64) -> Option<NaiveDateTime> {
154 let (sec, micro_sec) = split_second(v, MICROSECONDS);
155
156 let datetime = DateTime::from_timestamp(
157 sec,
159 micro_sec * MILLISECONDS as u32,
161 )?;
162 Some(datetime.naive_utc())
163}
164
165#[inline]
167pub fn timestamp_ns_to_datetime(v: i64) -> Option<NaiveDateTime> {
168 let (sec, nano_sec) = split_second(v, NANOSECONDS);
169
170 let datetime = DateTime::from_timestamp(
171 sec, nano_sec,
174 )?;
175 Some(datetime.naive_utc())
176}
177
178#[inline]
179pub(crate) fn split_second(v: i64, base: i64) -> (i64, u32) {
180 (v.div_euclid(base), v.rem_euclid(base) as u32)
181}
182
183#[inline]
185pub fn duration_s_to_duration(v: i64) -> Duration {
186 Duration::try_seconds(v).unwrap()
187}
188
189#[inline]
191pub fn duration_ms_to_duration(v: i64) -> Duration {
192 Duration::try_milliseconds(v).unwrap()
193}
194
195#[inline]
197pub fn duration_us_to_duration(v: i64) -> Duration {
198 Duration::microseconds(v)
199}
200
201#[inline]
203pub fn duration_ns_to_duration(v: i64) -> Duration {
204 Duration::nanoseconds(v)
205}
206
207pub fn as_datetime<T: ArrowPrimitiveType>(v: i64) -> Option<NaiveDateTime> {
209 match T::DATA_TYPE {
210 DataType::Date32 => date32_to_datetime(v as i32),
211 DataType::Date64 => date64_to_datetime(v),
212 DataType::Time32(_) | DataType::Time64(_) => None,
213 DataType::Timestamp(unit, _) => match unit {
214 TimeUnit::Second => timestamp_s_to_datetime(v),
215 TimeUnit::Millisecond => timestamp_ms_to_datetime(v),
216 TimeUnit::Microsecond => timestamp_us_to_datetime(v),
217 TimeUnit::Nanosecond => timestamp_ns_to_datetime(v),
218 },
219 DataType::Interval(_) => None,
221 _ => None,
222 }
223}
224
225pub fn as_datetime_with_timezone<T: ArrowPrimitiveType>(v: i64, tz: Tz) -> Option<DateTime<Tz>> {
227 let naive = as_datetime::<T>(v)?;
228 Some(Utc.from_utc_datetime(&naive).with_timezone(&tz))
229}
230
231pub fn as_date<T: ArrowPrimitiveType>(v: i64) -> Option<NaiveDate> {
233 as_datetime::<T>(v).map(|datetime| datetime.date())
234}
235
236pub fn as_time<T: ArrowPrimitiveType>(v: i64) -> Option<NaiveTime> {
238 match T::DATA_TYPE {
239 DataType::Time32(unit) => {
240 let v = v as u32;
242 match unit {
243 TimeUnit::Second => time32s_to_time(v as i32),
244 TimeUnit::Millisecond => time32ms_to_time(v as i32),
245 _ => None,
246 }
247 }
248 DataType::Time64(unit) => match unit {
249 TimeUnit::Microsecond => time64us_to_time(v),
250 TimeUnit::Nanosecond => time64ns_to_time(v),
251 _ => None,
252 },
253 DataType::Timestamp(_, _) => as_datetime::<T>(v).map(|datetime| datetime.time()),
254 DataType::Date32 | DataType::Date64 => NaiveTime::from_hms_opt(0, 0, 0),
255 DataType::Interval(_) => None,
256 _ => None,
257 }
258}
259
260pub fn as_duration<T: ArrowPrimitiveType>(v: i64) -> Option<Duration> {
262 match T::DATA_TYPE {
263 DataType::Duration(unit) => match unit {
264 TimeUnit::Second => Some(duration_s_to_duration(v)),
265 TimeUnit::Millisecond => Some(duration_ms_to_duration(v)),
266 TimeUnit::Microsecond => Some(duration_us_to_duration(v)),
267 TimeUnit::Nanosecond => Some(duration_ns_to_duration(v)),
268 },
269 _ => None,
270 }
271}
272
273#[cfg(test)]
274mod tests {
275 use crate::temporal_conversions::{
276 date64_to_datetime, split_second, timestamp_ms_to_datetime, timestamp_ns_to_datetime,
277 timestamp_us_to_datetime, NANOSECONDS,
278 };
279 use chrono::DateTime;
280
281 #[test]
282 fn negative_input_timestamp_ns_to_datetime() {
283 assert_eq!(
284 timestamp_ns_to_datetime(-1),
285 DateTime::from_timestamp(-1, 999_999_999).map(|x| x.naive_utc())
286 );
287
288 assert_eq!(
289 timestamp_ns_to_datetime(-1_000_000_001),
290 DateTime::from_timestamp(-2, 999_999_999).map(|x| x.naive_utc())
291 );
292 }
293
294 #[test]
295 fn negative_input_timestamp_us_to_datetime() {
296 assert_eq!(
297 timestamp_us_to_datetime(-1),
298 DateTime::from_timestamp(-1, 999_999_000).map(|x| x.naive_utc())
299 );
300
301 assert_eq!(
302 timestamp_us_to_datetime(-1_000_001),
303 DateTime::from_timestamp(-2, 999_999_000).map(|x| x.naive_utc())
304 );
305 }
306
307 #[test]
308 fn negative_input_timestamp_ms_to_datetime() {
309 assert_eq!(
310 timestamp_ms_to_datetime(-1),
311 DateTime::from_timestamp(-1, 999_000_000).map(|x| x.naive_utc())
312 );
313
314 assert_eq!(
315 timestamp_ms_to_datetime(-1_001),
316 DateTime::from_timestamp(-2, 999_000_000).map(|x| x.naive_utc())
317 );
318 }
319
320 #[test]
321 fn negative_input_date64_to_datetime() {
322 assert_eq!(
323 date64_to_datetime(-1),
324 DateTime::from_timestamp(-1, 999_000_000).map(|x| x.naive_utc())
325 );
326
327 assert_eq!(
328 date64_to_datetime(-1_001),
329 DateTime::from_timestamp(-2, 999_000_000).map(|x| x.naive_utc())
330 );
331 }
332
333 #[test]
334 fn test_split_seconds() {
335 let (sec, nano_sec) = split_second(100, NANOSECONDS);
336 assert_eq!(sec, 0);
337 assert_eq!(nano_sec, 100);
338
339 let (sec, nano_sec) = split_second(123_000_000_456, NANOSECONDS);
340 assert_eq!(sec, 123);
341 assert_eq!(nano_sec, 456);
342
343 let (sec, nano_sec) = split_second(-1, NANOSECONDS);
344 assert_eq!(sec, -1);
345 assert_eq!(nano_sec, 999_999_999);
346
347 let (sec, nano_sec) = split_second(-123_000_000_001, NANOSECONDS);
348 assert_eq!(sec, -124);
349 assert_eq!(nano_sec, 999_999_999);
350 }
351}