1use chrono::{DateTime, Duration, NaiveDate, Utc};
4
5pub fn date_to_f64(date: NaiveDate) -> f64 {
7 date_to_i64(date) as f64
8}
9
10pub fn date_time_to_f64(date_time: DateTime<Utc>) -> f64 {
13 const MICRO: i64 = 1_000_000;
14 let us = date_time_to_i64(date_time);
15 let s = us / MICRO;
16 let f = us % MICRO;
17 (s as f64) + (f as f64) / (MICRO as f64)
18}
19
20pub fn date_to_i64(date: NaiveDate) -> i64 {
22 date.signed_duration_since(NaiveDate::default()).num_days()
23}
24
25pub fn date_time_to_i64(date_time: DateTime<Utc>) -> i64 {
27 date_time.timestamp_micros()
28}
29
30pub fn i64_to_date(value: i64) -> NaiveDate {
32 Duration::try_days(value)
33 .and_then(|d| NaiveDate::default().checked_add_signed(d))
34 .unwrap_or(if value < 0 {
35 NaiveDate::MIN
36 } else {
37 NaiveDate::MAX
38 })
39}
40
41pub fn i64_to_date_time(value: i64) -> DateTime<Utc> {
43 DateTime::<Utc>::default()
44 .checked_add_signed(Duration::microseconds(value))
45 .unwrap_or(if value < 0 {
46 DateTime::<Utc>::MIN_UTC
47 } else {
48 DateTime::<Utc>::MAX_UTC
49 })
50}
51
52pub fn i64_milli_to_date_time(value: i64) -> DateTime<Utc> {
54 Duration::try_milliseconds(value)
55 .and_then(|d| DateTime::<Utc>::default().checked_add_signed(d))
56 .unwrap_or(if value < 0 {
57 DateTime::<Utc>::MIN_UTC
58 } else {
59 DateTime::<Utc>::MAX_UTC
60 })
61}
62
63pub fn i64_nano_to_date_time(value: i64) -> DateTime<Utc> {
65 DateTime::<Utc>::default()
66 .checked_add_signed(Duration::nanoseconds(value))
67 .unwrap_or(if value < 0 {
68 DateTime::<Utc>::MIN_UTC
69 } else {
70 DateTime::<Utc>::MAX_UTC
71 })
72}
73
74pub fn utc_now() -> DateTime<Utc> {
76 chrono::Utc::now()
77}