Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chrono: Add support for NaiveDate #56

Merged
merged 6 commits into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading