1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//! chrono types
//!
//! based on Dart VM, microseconds unit is used
//!
//! recommendations below implies UTC based conversions,
//! as these are generally easier to work with.
//! > see [timestamp_micros](https://docs.rs/chrono/0.4.20/chrono/naive/struct.NaiveDateTime.html?search=timestamp_micros#method.timestamp_micros)

use crate::{
    ffi::{DartCObject, DartHandleFinalizer, DartTypedDataType},
    into_dart::{free_zero_copy_buffer_i64, DartTypedDataTypeTrait},
    IntoDart,
};

impl IntoDart for chrono::DateTime<chrono::Utc> {
    /// on the other side of FFI, value should be reconstructed like:
    ///
    /// - hydrate into Dart [DateTime](https://api.dart.dev/stable/2.18.0/dart-core/DateTime/DateTime.fromMicrosecondsSinceEpoch.html)
    ///   `DateTime.fromMicrosecondsSinceEpoch(raw, isUtc: true);`
    ///
    /// - hydrate into Rust [DateTime](chrono::DateTime)::<[Utc](chrono::Utc)>
    ///   ```rust,ignore
    ///   let s = (raw / 1_000_000) as i64;
    ///   let ns = (raw.rem_euclid(1_000_000) * 1_000) as u32;
    ///   chrono::DateTime::<chrono::Utc>::from_utc(
    ///     chrono::NaiveDateTime::from_timestamp(s, ns), chrono::Utc);
    ///   ```
    ///
    ///   note that it could overflow under the same conditions as of [chrono::NaiveDateTime::from_timestamp](https://docs.rs/chrono/0.4.20/chrono/naive/struct.NaiveDateTime.html#method.from_timestamp)
    fn into_dart(self) -> DartCObject {
        self.timestamp_micros().into_dart()
    }
}

impl IntoDart for chrono::DateTime<chrono::Local> {
    /// on the other side of FFI, value should be reconstructed like:
    ///
    /// - hydrate into Dart [DateTime](https://api.dart.dev/stable/2.18.0/dart-core/DateTime/DateTime.fromMicrosecondsSinceEpoch.html)
    ///   `DateTime.fromMicrosecondsSinceEpoch(raw, isUtc: false);`
    ///
    /// - hydrate into Rust [DateTime](chrono::DateTime)::<[Local](chrono::Local)>
    ///   ```rust,ignore
    ///   let s = (raw / 1_000_000) as i64;
    ///   let ns = (raw.rem_euclid(1_000_000) * 1_000) as u32;
    ///   chrono::DateTime::<chrono::Local>::from(
    ///     chrono::DateTime::<chrono::Utc>::from_utc(
    ///       chrono::NaiveDateTime::from_timestamp(s, ns), chrono::Utc));
    ///   ```
    ///
    ///   note that it could overflow under the same conditions as of [chrono::NaiveDateTime::from_timestamp](https://docs.rs/chrono/0.4.20/chrono/naive/struct.NaiveDateTime.html#method.from_timestamp)
    fn into_dart(self) -> DartCObject {
        self.timestamp_micros().into_dart()
    }
}

impl IntoDart for chrono::NaiveDateTime {
    /// on the other side of FFI, value should be reconstructed like:
    ///
    /// - hydrate into Dart [DateTime](https://api.dart.dev/stable/2.18.0/dart-core/DateTime/DateTime.fromMicrosecondsSinceEpoch.html)
    ///   `DateTime.fromMicrosecondsSinceEpoch(raw, isUtc: true);`
    ///
    /// - hydrate into Rust [NaiveDateTime](chrono::NaiveDateTime)
    ///   ```rust,ignore
    ///   let s = (raw / 1_000_000) as i64;
    ///   let ns = (raw.rem_euclid(1_000_000) * 1_000) as u32;
    ///   chrono::NaiveDateTime::from_timestamp(s, ns)
    ///   ```
    ///
    ///   note that it could overflow under the same conditions as of [chrono::NaiveDateTime::from_timestamp](https://docs.rs/chrono/0.4.20/chrono/naive/struct.NaiveDateTime.html#method.from_timestamp)
    fn into_dart(self) -> DartCObject {
        self.timestamp_micros().into_dart()
    }
}

impl IntoDart for chrono::Duration {
    /// on the other side of FFI, value should be reconstructed like:
    ///
    /// - hydrate into Dart [Duration](https://api.dart.dev/stable/2.18.0/dart-core/Duration/Duration.html)
    ///   `Duration(microseconds: raw);`
    ///
    /// - hydrate into Rust [Duration](chrono::Duration)
    /// `chrono::Duration::microseconds(raw);`
    fn into_dart(self) -> DartCObject {
        self.num_microseconds().into_dart()
    }
}

impl IntoDart for Vec<chrono::DateTime<chrono::Utc>> {
    fn into_dart(self) -> DartCObject {
        self.iter()
            .map(chrono::DateTime::<chrono::Utc>::timestamp_micros)
            .collect::<Vec<_>>()
            .into_dart()
    }
}

impl<const N: usize> IntoDart for [chrono::DateTime<chrono::Utc>; N] {
    fn into_dart(self) -> DartCObject {
        let vec: Vec<_> = self.into();
        vec.into_dart()
    }
}

impl DartTypedDataTypeTrait for chrono::DateTime<chrono::Utc> {
    fn dart_typed_data_type() -> DartTypedDataType {
        DartTypedDataType::Int64
    }

    fn function_pointer_of_free_zero_copy_buffer() -> DartHandleFinalizer {
        free_zero_copy_buffer_i64
    }
}

impl IntoDart for Vec<chrono::DateTime<chrono::Local>> {
    fn into_dart(self) -> DartCObject {
        self.iter()
            .map(chrono::DateTime::<chrono::Local>::timestamp_micros)
            .collect::<Vec<_>>()
            .into_dart()
    }
}

impl<const N: usize> IntoDart for [chrono::DateTime<chrono::Local>; N] {
    fn into_dart(self) -> DartCObject {
        let vec: Vec<_> = self.into();
        vec.into_dart()
    }
}

impl DartTypedDataTypeTrait for chrono::DateTime<chrono::Local> {
    fn dart_typed_data_type() -> DartTypedDataType {
        DartTypedDataType::Int64
    }

    fn function_pointer_of_free_zero_copy_buffer() -> DartHandleFinalizer {
        free_zero_copy_buffer_i64
    }
}

impl IntoDart for Vec<chrono::NaiveDateTime> {
    fn into_dart(self) -> DartCObject {
        self.iter()
            .map(chrono::NaiveDateTime::timestamp_micros)
            .collect::<Vec<_>>()
            .into_dart()
    }
}

impl<const N: usize> IntoDart for [chrono::NaiveDateTime; N] {
    fn into_dart(self) -> DartCObject {
        let vec: Vec<_> = self.into();
        vec.into_dart()
    }
}

impl DartTypedDataTypeTrait for chrono::NaiveDateTime {
    fn dart_typed_data_type() -> DartTypedDataType {
        DartTypedDataType::Int64
    }

    fn function_pointer_of_free_zero_copy_buffer() -> DartHandleFinalizer {
        free_zero_copy_buffer_i64
    }
}

impl IntoDart for Vec<chrono::Duration> {
    fn into_dart(self) -> DartCObject {
        self.iter()
            .map(chrono::Duration::num_microseconds)
            .collect::<Vec<_>>()
            .into_dart()
    }
}

impl<const N: usize> IntoDart for [chrono::Duration; N] {
    fn into_dart(self) -> DartCObject {
        let vec: Vec<_> = self.into();
        vec.into_dart()
    }
}