Skip to content

Commit

Permalink
chrono: Add support for NaiveDate (#56)
Browse files Browse the repository at this point in the history
* chrono: Add support for NaiveDate

* Fix actual conversion of NaiveDate to Int64

* Do not use duplicate method names

* Include test method in tests

* Fix deprecation warnings

* Apply formatting
  • Loading branch information
TrackerSB authored Jan 7, 2024
1 parent 8c9bda7 commit 8758114
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
46 changes: 46 additions & 0 deletions src/chrono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,26 @@ 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 [NaiveDateTime](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::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.signed_duration_since(chrono::NaiveDate::MIN)
.into_dart()
}
}

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

impl IntoDart for Vec<chrono::NaiveDate> {
fn into_dart(self) -> DartCObject {
self.iter()
.map(|lhs| lhs.signed_duration_since(chrono::NaiveDate::MIN))
.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
11 changes: 10 additions & 1 deletion tests/containers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ fn main() {
assert!(isolate.post(return_backtrace()));
#[cfg(feature = "chrono")]
{
assert!(isolate.post(return_chrono_naive_date()));
assert!(isolate.post(return_chrono_naive_date_time()));
assert!(isolate.post(return_chrono_date_time_utc()));
assert!(isolate.post(return_chrono_date_time_local()));
Expand Down Expand Up @@ -236,9 +237,17 @@ fn return_backtrace() -> backtrace::Backtrace {
backtrace::Backtrace::new()
}

#[cfg(feature = "chrono")]
fn return_chrono_naive_date() -> chrono::NaiveDate {
chrono::NaiveDate::from_ymd_opt(1776, 7, 4)
.expect("The input date for testing is required to be valid")
}
#[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)
chrono::NaiveDate::from_ymd_opt(2016, 7, 8)
.map(|nd| nd.and_hms_micro_opt(9, 10, 11, 123_456))
.flatten()
.expect("The input date and time for testing are required to be valid")
}
#[cfg(feature = "chrono")]
fn return_chrono_date_time_utc() -> chrono::DateTime<chrono::Utc> {
Expand Down

0 comments on commit 8758114

Please sign in to comment.