-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Tracking Issue for duration_constructors #120301
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Thanks for adding this context. I was fairly certain that this would have been discussed before, but searching the issue tracker (for open issues) didn't turn up any existing discussion. I think it's okay to have this discussion here/now? From RFC 1040:
It goes on to quote JodaTime documentation; I'll quote the current version of this:
Note that the RFC is from 2015 and the latest discussion in #52556 is from 2018 so I feel like revisiting this decision isn't all that crazy. Personally I feel this distinction is well-established in the Rust ecosystem at this point, and there's a clear interplay between IMO each of these constructors have a clear unambiguous meaning in the context of a Note: I happen to be the current chrono maintainer, so I have some background in these things. |
We may want to consider as part of stabilization whether these could "just" (or in addition should be) associated constants. e.g., |
…Simulacrum core: add Duration constructors Add more `Duration` constructors. Tracking issue: rust-lang#120301. These match similar convenience constructors available on both `chrono::Duration` and `time::Duration`. What's the best ordering for these with respect to the existing constructors?
Rollup merge of rust-lang#120307 - djc:duration-constructors, r=Mark-Simulacrum core: add Duration constructors Add more `Duration` constructors. Tracking issue: rust-lang#120301. These match similar convenience constructors available on both `chrono::Duration` and `time::Duration`. What's the best ordering for these with respect to the existing constructors?
I'm happy to add them, but I'd be disappointed to remove the new constructors in favor of these. |
Just thought about this and found this issue :) I prefer the constructors to associated constants. Have you considered making the constructors take u32 instead of u64, so they don't need to worry about overflow and hence panicking? You lose the option of making a duration longer than (PS: I think the panic message in |
Yup. That looks like a classic case of copy-paste error. Also
is rather confusing.
|
@Mark-Simulacrum @djc I have attempted to add these associated constants in an idiomatic manner: #127700 I would love your feedback! |
I notice that it isn't currently proposed to add |
We also don't have It's notable that neither |
1 minute = 60 seconds. I don't think the leap second should be involved in (if then, 1 second != 1 second, |
Is there motivation for these constructors beyond the following?
If there's no motivation except for this, I think we should rather omit them from the standard library and let users be explicit that they want to sleep for 300 seconds and not 5 minutes (the latter being ambiguous wrt. leap seconds). |
Leap second is a concept about absolute time - more specifically, about calendar. 23:59:45 + 60 seconds could be one of 24:00:44, 24:00:45 or 24:00:46 by how to design the absolute time. But the duration 60 seconds could mean a physical quantity 60 seconds. If this still can be ambiguous, this can be documented enough on Duration. |
Some prior art from other languages / libraries:
Ohter languages and libraries that I checked1 did not have a comparable "Duration" concept. If you know of any, let me know and I'll be happy to add them. Personal thoughts:
Footnotes
|
I'm fine with adding hours/minutes constructors. I think those are pretty uncontroversial in what their semantics ought to be. Only leap seconds impact the length of an hour/minute, but we should just do what everyone else does and what But I'm strongly opposed to adding a constructor based on units of days (or anything bigger). I think that's a major mistake because it is common for days to not be 24 hours and for this to actually matter to humans. The new Javascript Temporal API, for example, generally does not let you treat days as "always 24 hours" unless you're explicitly working with civil time. When you're working with zoned datetimes, then |
I don't think the ("absolute") time keeping subtleties like leap second smearing, timezone differences or daylight saving time changes would trip people. A Maybe English isn't the best language to describe this as "time" is used for both, "absolute time" and relative time, but I think it's better to admit this ambiguity that often appears on spoken language and stick to well defined I'm not aware of real-world confusion, but only some cautious pushback around "standard" minutes/hours/days/weeks. (Yeah, I guess |
They can and do. Compare adding 1 day to 2025-03-08T17:00-05 in New York and adding 24 hours.
Those might be reasonable in specific contexts where the error is deemed acceptable, but I think it is a terrible choice to make for a general purpose library. |
Doing that where? This to me seems to be an issue with defining the When trying to define the length of Maybe what we need to avoid possible footguns is to properly define |
Also, like BurntSushi suggested, since the controversial bits seem to be mostly around I'm mostly interested on not forcing people to download a new nightly build every day just because of these 2 missing functions. |
I would question that "one day in elapsed actual time" has an unambiguous meaning. But I have no problem asking users to write |
Then how would you normally provide this context? Should we make duration generic over a clock source? I don't think this is possible retroactively because durations can be transplanted between Instant and SystemTime mentioned above. |
In |
@BurntSushi I am no expert in time libraries, but Chrono seems do the job: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=f1cd329588b14f9580cddc59965481a5. |
Note that on the example on 2025-03-09 NY is no longer at UTC-05:00, but at UTC-04:00. |
It does not. Case in point, with your setup, Chrono will let you generate a time that will literally never appear on the clocks in New York: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=8c06a26675f11c2f1e17c5f572e3b40b You need to be using (This is one of the many reasons I went out and built Jiff.) |
Well, they're also physical units because almost nobody uses megaseconds. This is what I was aiming for with |
Isn't the problem with days and DST the same for minutes and leap seconds? |
Sure. The problem is that "day" is an overload term. It is difficult to establish the right context while still using the word "day." Like it just doesn't seem worth it to be given how easy the footgun is personally. |
It seems to me we just need a // | |
let appointment = SystemTime::UNIX_EPOCH + Duration::from_secs(1741456800); // 2025-03-08T13:00:00-05:00[America/New_York]
let poorly_rescheduled = appointment + Duration::from_days(1); // 2025-03-09T14:00:00-04:00[America/New_York]
let civicly_rescheduled = appointment + CivicTimeOffset::days(1); // 2025-03-09T13:00:00-04:00[America/New_York] We should probably delay (had to move this because it got lost amidst a discussion) |
Already addressed above: #120301 (comment) |
Adding an entirely different duration type seems like a major escalation to me. It's also a totally different discussion from my perspective, and starts walking down the path of "let's add datetime support to std." Doing it half-assed would be a disaster. If you really want to have that discussion, I'd suggest opening a new issue so that we don't get derailed from considering the much more narrow proposal of convenience constructors on |
I'm aware of this. The progression we all seem to foresee is,
And of course 2 is a huge task, but that shouldn't stop everyone from getting |
If |
I am glad we seem overcoming the leap second issue. Unfortunately, the definition of day is too much ambiguous. It even doesn't have a single definition in science. I'd like to remind a thing. Convenience constructors are provided for convenience. They don't provide a new real feature. If the name matters, I hope the next discussion to be not to have or not the specifically named function |
While I have no idea how much At the same time |
So, to play the devil's advocate against This is way more unlikely to be a problem. Even if we could end up just saying the programmer was wrong and had a fundamental misunderstanding of what Also, we may not even have more leap seconds in the future due to a decrease on the Earth's rotation cancelling it1. Footnotes |
I don't think there's any need to play devil's advocate nor a reason to continue blocking stabilizing |
@djc are you open to gut this to get I truly believe we should be free to get all of it, but I'd rather see some progress than waiting on a DateTime implementation getting to std. |
I don't know at which point is this possible, but can't this be splitted into 2 issues ?
This would stabilize some useful API, and will minify the content of each PR so that the comments on |
I'm fine with someone splitting the feature, although I am not particularly motivated to do so.
I think this is coming in pretty strong; did you read my early comment where I argued that in the context of a type named Maybe we can add a word to the name instead? Like |
common days should be named commonly, other than that, i suggest adding |
This is not the point. "1.00000 day" still doesnt define any strict physical duration. This is matter of unit, not the counts |
Yeah I feel strongly about this.
Yes. I don't understand why you're suggesting that the name
I don't agree. This is what I tried to show with my example above.
I renamed
I don't really think it's worth it to be honest, and it's not clear that this name will do much if anything to avoid the footguns I've pointed out. |
I forked this feature into |
Little update, the watered down version of this feature made it into unstable, so please comment on |
Feature gate:
#![feature(duration_constructors)]
This is a tracking issue for supporting larger unit sizes for
Duration
constructors.Public API
Add the following constructors to
Duration
:Duration::from_weeks()
Duration::from_days()
Duration::from_hours()
Duration::from_mins()
Steps / History
The text was updated successfully, but these errors were encountered: