Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upAdd std::time::Duration::{from_days, from_hours, from_mins} #47097
Conversation
rust-highfive
assigned
BurntSushi
Dec 31, 2017
This comment has been minimized.
This comment has been minimized.
|
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @BurntSushi (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
ChristopherRabotin
referenced this pull request
Dec 31, 2017
Closed
Allow to add and subtract durations from `Utc` #13
BurntSushi
reviewed
Dec 31, 2017
|
I think in principle this seems fine, but we should check what other time libraries do. Go, for example, has minutes and hours, but not days. Are there other issues aside from leap seconds with using If we do want to keep the |
| /// assert_eq!(86_400, duration.as_secs()); | ||
| /// assert_eq!(0, duration.subsec_nanos()); | ||
| /// ``` | ||
| #[unstable(feature = "duration_from_hours", issue = "47097")] |
This comment has been minimized.
This comment has been minimized.
BurntSushi
Dec 31, 2017
Member
I think we generally create a separate (non-PR) tracking issue for new features. I think you can leave it like this for now, but if we do merge this, then I think we'll want to create a tracking issue and then update the PR.
Is that right @alexcrichton?
This comment has been minimized.
This comment has been minimized.
BurntSushi
Dec 31, 2017
Member
Also, I note that this feature name is duration_from_hours, which is the same as the from_hours method but different from the name duration_from_mins used for the from_mins method.
I suspect you probably just want to use one feature name for all of them (duration_from perhaps?).
| /// ``` | ||
| #[unstable(feature = "duration_from_mins", issue = "47097")] | ||
| #[inline] | ||
| pub fn from_mins(mins: u64) -> Duration { |
This comment has been minimized.
This comment has been minimized.
BurntSushi
added
S-waiting-on-author
S-waiting-on-team
T-libs
labels
Dec 31, 2017
This comment has been minimized.
This comment has been minimized.
|
Well there is also the issue of daylight savings time. 5:00 on some day plus Basically, splitting the timestamp into parts, adding one to the day part, and joining it back together can give a different result than adding |
This comment has been minimized.
This comment has been minimized.
|
Thanks for the prompt review! Concerning the length of days, they are always strictly 86_400 seconds long, which is strictly 24 hours. (Note that there's also a "sideral day" which lasts 23 hours 56 minutes 4 seconds and a bit, but that notion is only used in some astronomical observations and relies on a different definition of the day.) The confusing part with leap seconds is that there are several "TAI seconds" associated to a unique UTC time. In other words, UTC moves slightly faster than the real atomic time. The issue pinpointed by retep998 #47097 (comment) is valid (and it's the reason Go didn't include Days, cf. golang/go#11473 (comment)), but I would argue that it's an implementor's problem. All that said, if there's a strong preference to not include |
This comment has been minimized.
This comment has been minimized.
|
I guess I would personally want to be conservative here and not have a But, I do not have a lot of domain knowledge here so my opinion is held pretty weakly. Let's see what the rest of the library team thinks. :) |
This comment has been minimized.
This comment has been minimized.
|
The initial RFC didn’t add these constructors specifically because these units only make sense with a starting date in mind. EDIT:
|
This comment has been minimized.
This comment has been minimized.
|
I don't find the argumentation in the initial RFC to be that convincing, if I'm honest. The Joda Time library specifically provides these I'll go ahead and remove the |
alexcrichton
removed
the
S-waiting-on-author
label
Jan 2, 2018
This comment has been minimized.
This comment has been minimized.
|
Triage ping @rust-lang/libs! (Note that this PR now only provides |
This comment has been minimized.
This comment has been minimized.
|
cc @wycats |
This comment has been minimized.
This comment has been minimized.
|
Sorry for the delay on this -- @wycats (who has thought deeply about these issues) plans to reply this weekend. |
This comment has been minimized.
This comment has been minimized.
|
As noted, the original RFC left out a lot of expected APIs for a few reasons:
An example of (2) is the way that people often use time and duration APIs to benchmark. In many languages, it's common to create the equivalent of There are some problems with this:
Because of the fact that certain times (like the time that comes from In contrast, the RFC I wrote creates a very nice API for tracking elapsed time: let i = Instant::now();
// later
let amount = i.elapsed();The My philosophy for extensions to the After that very long aside, in this case, I'd like to start by understanding why people want these very large constants to begin with. I can see why somebody would genuinely want a Duration in the seconds (or perhaps even minutes) to conveniently construct parameters to pass to timeouts. But I'm not quite sure why somebody would want hours or days. When looking at my own backend code, I see cases where people are using these very large constants as an approximation for day-based math, which I have strong objections to (day-based math has even more gotchas). So I'd like us to try to enumerate some good examples of code that constructs durations lasting days (or even hours) so we can see what gotchas might apply to them. |
This comment has been minimized.
This comment has been minimized.
|
The only good examples I have for measuring hours or days are simulation software. Specifically, a large part of my day-to-day coding involves writing integration software of dynamic systems, and these have time spans on the order of months or years. Even more specifically, I wrote a simulation toolkit last year which, for many reasons, I'll be converting to Rust while adding numerous functionalities. This requires long term precise time measurements (and justifies the coding of hifitime). You may find specific time scale examples in this test file in Go. You'll notice that the integration time steps for the tests run between 24 hours to 190 days. |
alexcrichton
added
S-waiting-on-review
and removed
S-waiting-on-team
labels
Jan 23, 2018
This comment has been minimized.
This comment has been minimized.
|
@ChristopherRabotin Thanks so much for that information. I've done a review of a lot of my own codebases in various languages, and I don't have any reservations about standardizing seconds. In my own codebases, one antipattern I found with very long durations is that code sometimes relied upon the process itself remaining up, and would have been more resilient by using crons or other externalized timers. Because Instants aren't shareable across processes easily (because monotonic time backends might rely on time-since-process-start), it's not even particularly easy using Writing this comment, I realize we probably should do something about that use case (the ability to store Instant::now() + durably). In my own code, some of the uses of minutes had this problem (better off as crons), but I don't mind adding minutes (as Once you get beyond seconds and minutes, in addition to the problem of reliance on long-lived processes, there is also the problem of edge cases interacting poorly with calendars (adding 1 standard day to midnight might not increment the day when another calendar is in play), and I'd want to see a broader set of use-cases to help drive a possible design for this feature. |
This comment has been minimized.
This comment has been minimized.
|
I agree that calendar and specifically time zones are not easy to handle. That's why in Anyhow, would you encourage me to change my PR such that |
This comment has been minimized.
This comment has been minimized.
|
|
aturon
assigned
aturon
and unassigned
BurntSushi
Feb 1, 2018
This comment has been minimized.
This comment has been minimized.
|
@ChristopherRabotin Sorry for the delayed response! My take from @wycats's comment is that he feels comfortable adding minutes, but still feels like hours are ripe for misuse (and are relatively niche). So I think at this point we'd be happy to take |
This comment has been minimized.
This comment has been minimized.
|
@aturon , @wycats , if this PR will only add minutes, I really don't see much of a point in all honesty, especially if I need to rewrite this PR to accommodate for the upstream changes. =/ Converting from seconds to minutes doesn't change a whole lot (factor of 60), but I definitely have use in the seconds to hours conversion. Now, if I'm the only one who will ever use this, then I can just continue using the hack I currently use. |
BatmanAoD
added
S-waiting-on-team
and removed
S-waiting-on-review
labels
Feb 7, 2018
This comment has been minimized.
This comment has been minimized.
|
Soo... where do we stand on this PR? Should it just be closed due to disinterest? |
This comment has been minimized.
This comment has been minimized.
|
Is the consensus that there should only be a standard minutes initializer?
If so then yes, we should close this PR.
…On Fri, Mar 2, 2018, 17:15 Jake Goulding ***@***.***> wrote:
Soo... where do we stand on this PR? Should it just be closed due to
disinterest?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#47097 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AEma6O037K8RGcxbbZNT7A5812u_lMl9ks5taeCGgaJpZM4RP80O>
.
|
This comment has been minimized.
This comment has been minimized.
|
Yep, the consensus seems to have only a minutes initializer. Closing as the author's wish. |
pietroalbini
closed this
Mar 5, 2018
ChristopherRabotin
deleted the
ChristopherRabotin:std-time-duration-from-mins-hours-days
branch
Mar 5, 2018
This comment has been minimized.
This comment has been minimized.
|
I would love to hear your opinions on minute, hour and day constants discussed in #52556. Proposed solution is to name them as |
ChristopherRabotin commentedDec 31, 2017
•
edited
Minutes, hours and days* have a fixed number of seconds. Allowing to construct a
Durationfrom a number of these common time spans alleviates a minor implementation by each dev needing this.Note of warning: This is my first contribution attempt to the rust code base. I think I've followed all the rules, but let me know if I haven't.
I am already planning to using this in hifitime, and I thought it may be useful to the broader Rust community. BTW,
hifitimeis a crate for date time handling in rust built on top of the current std::time::Duration and it supports leap seconds (which I need for other projects).(*) Days may have an extra second called the leap second. It's announced and determined by the IETF one year around the end of December. In recent years, there's been one leap second every two years. So we can safely assume that when a dev would called
Duration::from_daysthey expect the usual number of seconds in a day.Tests passed locally: