Skip to content

Commit

Permalink
chrono: Add support for NaiveDate
Browse files Browse the repository at this point in the history
  • Loading branch information
TrackerSB committed Jan 5, 2024
1 parent 8c9bda7 commit 9b71906
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/chrono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,25 @@ impl IntoDart for chrono::DateTime<chrono::Local> {
}
}

impl IntoDart for chrono::NaiveDate {
/// 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 [NaiveDate](chrono::NaiveDate)
/// ```rust,ignore
/// let s = (raw / 1_000_000) as i64;
/// let ns = (raw.rem_euclid(1_000_000) * 1_000) as u32;
/// chrono::NaiveDate::from_timestamp(s, ns)
/// ```
///
/// note that it could overflow under the same conditions as of [chrono::NaiveDate::from_timestamp](https://docs.rs/chrono/0.4.20/chrono/naive/struct.NaiveDate.html#method.from_timestamp)
fn into_dart(self) -> DartCObject {
self.timestamp_micros().into_dart()

Check failure on line 71 in src/chrono.rs

View workflow job for this annotation

GitHub Actions / Memory Check

no method named `timestamp_micros` found for struct `NaiveDate` in the current scope
}
}

impl IntoDart for chrono::NaiveDateTime {
/// on the other side of FFI, value should be reconstructed like:
///
Expand Down Expand Up @@ -137,6 +156,32 @@ impl DartTypedDataTypeTrait for chrono::DateTime<chrono::Local> {
}
}

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

Check failure on line 162 in src/chrono.rs

View workflow job for this annotation

GitHub Actions / Memory Check

no function or associated item named `timestamp_micros` found for struct `NaiveDate` in the current scope
.collect::<Vec<_>>()
.into_dart()
}
}

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

impl DartTypedDataTypeTrait for chrono::NaiveDate {
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()
Expand Down
4 changes: 4 additions & 0 deletions tests/containers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,10 @@ fn return_backtrace() -> backtrace::Backtrace {
backtrace::Backtrace::new()
}

#[cfg(feature = "chrono")]
fn return_chrono_naive_date_time() -> chrono::NaiveDate {
chrono::NaiveDate::from_ymd(1776, 7, 4)
}
#[cfg(feature = "chrono")]
fn return_chrono_naive_date_time() -> chrono::NaiveDateTime {
chrono::NaiveDate::from_ymd(2016, 7, 8).and_hms_micro(9, 10, 11, 123_456)
Expand Down

0 comments on commit 9b71906

Please sign in to comment.