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.
Wrap the hifitime::Epoch with HifiEpoch to enable simple type conversions to chrono types.
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.
Manipulate the struct as a chrono type as if it were chrono all along.
Wrap the chrono::DateTime with LofiDateTime to enable simple type conversions back to hifitime types.
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`
}
});
hifitime::Epoch
,hifitime::Duration
,chrono::DateTime<Utc>
, andchrono::Duration
instead of traitshifitime
andchrono
types usingFrom<T>
andInto<T>
Deref
,DerefMut
, andTimelike
for wrapper typesSimpleDateLike
trait for common date operations. (Honestly this is a crutch to avoid making a fullDateLike
implementation for the wrapper type.)Example
Here's an example I'm using to adapt
hifitime::Epoch
s into egui'sDatePickerButton
, which only works withchrono::NaiveDate
s.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.
hifitime::Epoch
withHifiEpoch
to enable simple type conversions tochrono
types.chrono::NaiveDateTime
with a simple.into()
. This keeps precision up tochrono
's limits, which is in the millisecond range. Note that this is no longer inside a wrapper, it's an actualchrono
type.chrono
type as if it werechrono
all along.chrono::DateTime
withLofiDateTime
to enable simple type conversions back tohifitime
types..into()
to get the datetime back as ahifitime::Epoch
. At this point the epoch only has the precision ofchrono
but from now on we can use it as a precision epoch withhifitime
as if it were an epoch all along.