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

Simplify datetime conversion #5006

Merged
merged 3 commits into from
Oct 30, 2023
Merged
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
14 changes: 3 additions & 11 deletions parquet_derive/src/parquet_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,13 +427,9 @@ impl Field {
quote! { ::chrono::naive::NaiveDateTime::from_timestamp_millis(vals[i]).unwrap() }
}
Some(ThirdPartyType::ChronoNaiveDate) => {
// NaiveDateTime::UNIX_EPOCH.num_days_from_ce() == 719163
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this is a proc macro we would need to explicitly declare all the types and the Datelike trait. It seemed simpler to just define it

quote! {
::chrono::naive::NaiveDate::from_num_days_from_ce_opt(vals[i]
+ ((::chrono::naive::NaiveDate::from_ymd_opt(1970, 1, 1).unwrap()
.signed_duration_since(
::chrono::naive::NaiveDate::from_ymd_opt(0, 12, 31).unwrap()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This caused me some confusion, but it is because days start counting at 1, not zero 😅

)
).num_days()) as i32).unwrap()
::chrono::naive::NaiveDate::from_num_days_from_ce_opt(vals[i].saturating_add(719163)).unwrap()
}
}
Some(ThirdPartyType::Uuid) => {
Expand Down Expand Up @@ -1339,11 +1335,7 @@ mod test {
panic!("Schema and struct disagree on type for {}", stringify!{ henceforth });
}
for (i, r) in &mut records[..num_records].iter_mut().enumerate() {
r.henceforth = ::chrono::naive::NaiveDate::from_num_days_from_ce_opt(vals[i]
+ ((::chrono::naive::NaiveDate::from_ymd_opt(1970, 1, 1).unwrap()
.signed_duration_since(
::chrono::naive::NaiveDate::from_ymd_opt(0, 12, 31).unwrap()
)).num_days()) as i32).unwrap();
r.henceforth = ::chrono::naive::NaiveDate::from_num_days_from_ce_opt(vals[i].saturating_add(719163)).unwrap();
}
}
}).to_string());
Expand Down
Loading