time-rs / time

The most used Rust library for date and time handling.
https://time-rs.github.io
Apache License 2.0
1.09k stars 273 forks source link

`Duration` between two `DateTime`s. #536

Closed reitermarkus closed 1 year ago

reitermarkus commented 1 year ago

Currently it seems that only Instant::elapsed provides a way to get a Duration, but there is no way to get a Duration between two OffsetDateTimes.

CryZe commented 1 year ago

You can just subtract them. Though that will be inaccurate in that it misses all the leap seconds between the two date times. I have a branch that takes leap seconds into account, but that's probably a separate issue to discuss.

jhpratt commented 1 year ago

Yeah, subtracting them does the trick.

Leap seconds are a completely different story, and that won't be supported until the tzdb is integrated. That's something I have not done any actual work on, only some thinking about how I want the API to look.

reitermarkus commented 1 year ago

Yeah, subtracting them does the trick.

That's what I tried, but I see my problem. It does not work in const context, e.g.

const GPS_EPOCH: OffsetDateTime = datetime!(1980-01-06 00:00 UTC);
const GPS_TO_UNIX_TIME_OFFSET: time::Duration = OffsetDateTime::UNIX_EPOCH - GPS_EPOCH;

I have a branch that takes leap seconds into account, but that's probably a separate issue to discuss.

This would probably also be beneficial for my use case.

jhpratt commented 1 year ago

That's because trait implementations cannot currently be const. Once they can, it is almost certain that I will constify that and many others. It doesn't mean that the trait isn't currently implemented.

reitermarkus commented 1 year ago

Yeah, the error message is actually quite clear after reading it the second time:

the trait `Sub<_>` is implemented for `OffsetDateTime`, but that implementation is not `const`
jhpratt commented 1 year ago

That's actually a significant improvement than it was last time I was playing around with const traits on nightly. It used to be far more confusing.