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 rewrite to use From<T> and Into<T> instead of custom traits #32

Merged
merged 2 commits into from
Sep 4, 2024

Conversation

philiplinden
Copy link
Owner

@philiplinden philiplinden commented Sep 4, 2024

  • Implements wrapper types for hifitime::Epoch, hifitime::Duration,
    chrono::DateTime<Utc>, and chrono::Duration instead of traits
  • Low-friction conversions between hifitime and chrono types using From<T> and Into<T>
  • Implementation of common traits like Deref, DerefMut, and Timelike for
    wrapper types
  • Simplified SimpleDateLike trait for common date operations. (Honestly this is a crutch to avoid making a full DateLike implementation for the wrapper type.)
  • Added a Readme.

Example

Here's an example I'm using to adapt hifitime::Epochs into egui's DatePickerButton, which only works with chrono::NaiveDates.

In this case I don't care about the loss of precision. I want to use the button to select a date and then use this new date as the starting epoch for a precision timer.

  1. Wrap the hifitime::Epoch with HifiEpoch to enable simple type conversions to chrono types.
  2. Convert the precision epoch to a loose timezone-naive chrono::NaiveDateTime with a simple .into(). This keeps precision up to chrono's limits, which is in the millisecond range. Note that this is no longer inside a wrapper, it's an actual chrono type.
  3. Manipulate the struct as a chrono type as if it were chrono all along.
  4. Wrap the chrono::DateTime with LofiDateTime to enable simple type conversions back to hifitime types.
  5. Use .into() to get the datetime back as a hifitime::Epoch. At this point the epoch only has the precision of chrono but from now on we can use it as a precision epoch with hifitime as if it were an epoch all along.
ui.menu_button("Epoch...", |ui| {
    let hifi_epoch = HifiEpoch(coordinate_time.epoch());
    let chrono_datetime: chrono::NaiveDateTime = hifi_epoch.into(); // convert precision epoch to timezone-aware `chrono::DateTime`
    let mut selected_date = chrono_datetime.date();
    if DatePickerButton::new(&mut selected_date).ui(ui).changed() {
        // Update the Time resource with the selected date
        let new_time = selected_date.and_time(chrono::NaiveTime::from_hms_milli_opt(0, 0, 0, 0).unwrap());
        let new_datetime = new_time.and_utc();
        let lofi_datetime = LofiDateTime(new_datetime);
        coordinate_time.start_epoch = Some(lofi_datetime.into()); // convert new date back to `hifitime::Epoch`
    }
});

@philiplinden philiplinden added the feature Feature development label Sep 4, 2024
@philiplinden philiplinden self-assigned this Sep 4, 2024
@philiplinden philiplinden merged commit 15eff60 into main Sep 4, 2024
3 of 4 checks passed
@philiplinden philiplinden deleted the lofitime-2 branch September 4, 2024 07:31
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.

1 participant