chrono icon indicating copy to clipboard operation
chrono copied to clipboard

Consider adding a "Default" implementation to Duration

Open eminence opened this issue 2 years ago • 3 comments

Would you consider having chrono::Duration implement Default, which would produce a zero-duration Duration ?

This would be consistent with std::time::Duration

eminence avatar Jun 22 '22 21:06 eminence

Right now the implementation of the Duration type depends on the Cargo features: it can be from time 0.1 or the internal version. The external version is enabled by default, so if we implement this only for the internal version, so this would make the oldtime Cargo feature non-additive. On the other hand... I guess it already is?

djc avatar Jun 23 '22 10:06 djc

Ah, I had forgotten about oldtime. chrono::Duration already had a slightly different API from time::Duration. For example chrono::Duration implements Hash but time::Duration does not. (Though otherwise their APIs are nearly identical).

Maybe chrono 0.5 could add a impl Default for Duration ?

eminence avatar Jun 23 '22 14:06 eminence

I'd be happy to add that once we straighten out the compatibility situation.

djc avatar Jun 27 '22 14:06 djc

What's the current state of this issue?

As far as I understood from @djc, the reason for not implementing Default was because std::time::Duration didn't implement it in 0.1.x.

As of 0.3.17 default is implemented, does that mean this is no longer "API-Incompatible" and could be implemented?

delightfulagony avatar Nov 23 '22 17:11 delightfulagony

On the main branch (which will eventually become 0.5) we can add a Default implementation to what's now called TimeDelta. On the 0.4.x branch due to compatibility concerns that will not be viable.

You seem to be confusing the std::time::Duration type with the Duration type from the time crate. Due to compatibility, chrono 0.4.x will always (conditionally) depend on time 0.1, while chrono 0.5 does not depend on time at all.

djc avatar Nov 23 '22 17:11 djc

You're right, I mistook std::time with the time crate.

But if this time I checked right, as of 0.1.3 for std::time::Duration, default is also implemented

delightfulagony avatar Nov 23 '22 19:11 delightfulagony

Yes, but the Duration we expose in chrono is actually the one from the time crate, so std::time::Duration has no bearing on this issue.

djc avatar Nov 23 '22 20:11 djc

@delightfulagony - if this https://github.com/chronotope/chrono/pull/860 or a linked PR gets merged, then you will be able to use std::time::Duration directly, including its Default implementation, would that satisfy your use case?

esheppa avatar Nov 23 '22 21:11 esheppa

Yes, but the Duration we expose in chrono is actually the one from the time crate, so std::time::Duration has no bearing on this issue.

Oh ok, so I misunderstood you, alright, thank you

@delightfulagony - if this #860 or a linked PR gets merged, then you will be able to use std::time::Duration directly, including its Default implementation, would that satisfy your use case?

I suppose it would, in any case I changed to using the time crate that also works for my needs, and since you folks are working on 5.x.x don't worry about it, thank you for taking the time to answer and explain :smile:

You're both the best! :tada: :+1:

delightfulagony avatar Nov 24 '22 00:11 delightfulagony

Added in https://github.com/chronotope/chrono/pull/1327.

pitdicker avatar Oct 02 '23 06:10 pitdicker