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

Lofitime: A hifitime <-> chrono adapter #31

Merged
merged 4 commits into from
Aug 13, 2024
Merged

Lofitime: A hifitime <-> chrono adapter #31

merged 4 commits into from
Aug 13, 2024

Conversation

philiplinden
Copy link
Owner

@philiplinden philiplinden commented Aug 13, 2024

Egui uses chrono for displaying dates and times, but I want to use hifitime for all of my internal timings. To adapt between chrono and hifitime datetimes and durations, I created a new crate called lofitime.

lofitime adds a few traits that bridge the gap. I even wrote tests that pass!

NOTE: For convenience, chrono -> hifitime conversions are truncated to the millisecond. hifitime -> chrono conversions use all of chrono's available precision.

FROM TO USE TRAIT WITH FUNCTION
hifitime::Epoch chrono::DateTime<Utc> HifiDateTime .to_lofi_utc()
hifitime::Epoch chrono::NaiveDateTime HifiDateTime .to_lofi_naive()
hifitime::Duration chrono::Duration HifiDateTime .to_lofi_duration()
chrono::DateTime or chrono::NaiveTime hifitime::Epoch LofiDateTime .to_hifi_epoch()
chrono::DateTime or chrono::NaiveTime hifitime::Epoch LofiDateTime .to_hifi_duration()

This PR also contains some quality of life improvements like disabling CI workflows that are doomed to fail.

@philiplinden philiplinden merged commit 845595b into main Aug 13, 2024
3 of 4 checks passed
@philiplinden philiplinden deleted the hifitime_egui branch August 13, 2024 05:02
Copy link
Collaborator

@ChristopherRabotin ChristopherRabotin left a comment

Choose a reason for hiding this comment

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

Interesting idea! I might use this in the ANISE GUI actually.

@@ -27,13 +24,137 @@ pub trait HifiDuration {
fn to_lofi_duration(&self) -> chrono::Duration;
}

impl HifiDuration for hifitime::Duration {
fn to_lofi_duration(&self) -> chrono::Duration {
let (centuries, nanos) = self.to_parts();
Copy link
Collaborator

Choose a reason for hiding this comment

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

This approach works. I would have done it differently: I would have either used decompose or directly to_unit and retrieve the number of nanoseconds as an f64.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Feature development
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants