time-rs / time

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

Question: How to get the duration between two DateTimes #702

Closed KnorrFG closed 3 months ago

KnorrFG commented 3 months ago

Hey, I'm in the following situation: I have Datetimes in a json file defined via single ints for each value (year, month, day, h, m, s, ms). At some point, I want to know the signed duration that lies between some of them. And I have a hard time figuring out how to do this.

It seems the only api that does this is defined in time::ext::InstantExt. There is no way to get this information without two std::time::Instant values, but I can't construct an Instant from a DateTime (neither primitive nor offset). I also can't construct an Instant from my integer values.

Am I missing something, or is this really not possible?

jhpratt commented 3 months ago

You'll want to construct a PrimitiveDateTime or OffsetDateTime, which can then be subtracted directly, yielding a Duration.

Instant is definitely not what you are looking for, as the primary use case there is for measuring elapsed time.