this post was submitted on 30 Oct 2025
64 points (100.0% liked)

Rust

7450 readers
20 users here now

Welcome to the Rust community! This is a place to discuss about the Rust programming language.

Wormhole

!performance@programming.dev

Credits

  • The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)

founded 2 years ago
MODERATORS
top 24 comments
sorted by: hot top controversial new old
[–] SorteKanin@feddit.dk 16 points 3 days ago (2 children)

Duration::from_mins and Duration::from_hours seems nice. Otherwise kind of a boring release if you ask me.

[–] FizzyOrange@programming.dev 13 points 3 days ago (1 children)

They're released on a schedule so they are often quite boring.

[–] SorteKanin@feddit.dk 9 points 3 days ago* (last edited 3 days ago)

I know, but they used to be more exciting I feel like - but I guess it's only natural that development speed goes down as more and more low-hanging fruit has already been picked. What remains of the non-boring stuff is probably quite complicated and difficult to stabilize.

[–] sugar_in_your_tea@sh.itjust.works 7 points 3 days ago (1 children)

I actually wanted that just the other day. I actually wanted days, but at least now I only need Duration::from_hours(24).

[–] SorteKanin@feddit.dk 13 points 2 days ago* (last edited 2 days ago) (2 children)

Duration::from_days was proposed but consensus was not reached as "day" is somewhat complicated as that sometimes can be 23 hours or 25 hours due to daylight savings or 23:59 due to leap seconds shenanigans.

[–] anton@lemmy.blahaj.zone 2 points 2 days ago (1 children)

Leap seconds are already a problem for minutes and hours, which is probably why they weren't added until now.

They're also a problem for adding seconds to system times, since one second duration could mean two seconds wall clock time. As mentioned in discussions, we accept this for seconds, but not daylight savings, which is odd IMO because leap seconds are more "real" than daylight savings.

Ideally, Duration should always represent multiples of logical seconds, and they only make sense in the context of a clock, either monotonic or system. There should be specialized functions for translating between the two (e.g. SystemTime::add_monotonic(Duration) and SystemTime.sub_monotonic(SystemTime)), which would account for leap seconds and daylight savings.

But all of that can live in an external crate like "chrono". I just want a way to clearly communicate intent. In my case, I'm writing a ping test tool to find out how often my internet drops and I want to print logs hourly (make sure it's still running) and daily (longer term logging purposes). This use case doesn't need exact precision, it just needs to print roughly on a schedule (I'm running for a week or two, not multiple years). If I do actual time math, I'd use a specialized crate with clear documentation, but for something quick and dirty, I'd prefer to use the stdlib.

[–] sugar_in_your_tea@sh.itjust.works 0 points 2 days ago* (last edited 2 days ago) (1 children)

That sounds like an irrelevant problem though. A duration is explicitly not concerned with daylight savings at all, I've always thought of it as the amount of actual time (i.e. from a monotonic clock) that has elapsed between two absolute times. So a time diff between noon on daylight savings change day and the day prior would either be 23 or 25 hours, not exactly equal to Duration::from_days(1). There's no time zone or even year context for Duration::from_days(1), so it should always be defined as a naïve calculation involving fixed definitions for hours per day and seconds per hour.

Years are a bit more controversial, to the point where 365 days and 365.24 days are both acceptable in different contexts, and that latter value could change in the very long term (millions of years). But I think up to days, most can agree that the value assumes exactly 24 hours and is completely separate from the idea of wall clocks changing based on local law.

Now, if we want to support "logical" time math, add that to the date/time methods likelogical_add(Duration) that adds/subtracts daylight savings hours and leap seconds as needed so if you add a multiple of 24 hours, you'll get the same wall clock time just on a different day. Maybe chrono does that, IDK, but it would be nice to have day resolution supported at the stdlib level for simple cases where I'm looking for day chunks. It's not a big deal, and Duration::from_hours(24) is plenty readable.

[–] SorteKanin@feddit.dk 5 points 2 days ago* (last edited 2 days ago) (1 children)

so it should always be defined as a naïve calculation involving fixed definitions for hours per day and seconds per hour.

Well... that's your opinion. But other people disagree and say that this could be confusing. But I won't rehash the whole debate, you can read it here instead: https://github.com/rust-lang/rust/issues/120301

I'm sure it's already been discussed to death 😅

[–] sugar_in_your_tea@sh.itjust.works 1 points 2 days ago (1 children)

As long as it's documented well, I don't think it should matter. If it doesn't do the thing you expect, don't use it. I'm assuming there's almost a 1:1 association between devs who will use Duration::from_days() wrong and devs who would make their own constants and use them wrong, so the least harmful approach is to make them available and document them clearly.

But yeah, this topic has been beaten to death and I'm mostly whining about a bit of typing, which is now substantially less with Duration::from_hours() being stabilized. I'm happy they stabilized minutes and hours, and I'm hoping they stabilize days at some point.

[–] xav@programming.dev 4 points 2 days ago (1 children)

C++ has proven (at great expense) that documenting footguns is not sufficient. At all. You have to make them impossible.

[–] sugar_in_your_tea@sh.itjust.works 3 points 2 days ago (1 children)

Then make it explicit, e.g. Duration::from_days_monotonic(...) . I'm also fine with just having constants, but those are nightly only. I want a way to clearly express durations in terms of days, and constants and functions are clearer than comments.

[–] SorteKanin@feddit.dk 2 points 2 days ago (1 children)

I want a way to clearly express durations in terms of days

The argument here is that expressing durations in terms of days is a bad idea because "day" does not really convey a very precise duration, as it is not always 24 hours.

Maybe you won't be confused by Duration::from_days right now, but maybe a junior dev before they get their coffee, or even the senior dev on code review might miss stuff like that.

But a day does have exactly 24 hours, at least when using Unix timestamps. Leap seconds aren't accounted for in a Unix timestamp, and Unix timestamps assume UTC timezone, which doesn't have daylight savings.

If you don't make a "from_days()" or "DAYS" constant, people will make their own and they'll be wrong in exactly the same way as if that function or constant was defined in the stdlib, but at least in the stdlib, you have the opportunity to centralize the documentation for it and maybe educate that junior dev before they make a mistake.

[–] BB_C@programming.dev 12 points 3 days ago (1 children)

You see, rust platform support can't be bad when even meme platforms can get to tier 1.

[–] alex_is@infosec.exchange 2 points 3 days ago

@BB_C @jwr1

that's awesome. gave me a chuckle. gotta love rust (i use a lot of rust)

[–] NGram@piefed.ca 7 points 3 days ago (1 children)

The strict_* set of integer function look interesting though I'm unlikely to use something that panics by design. I'm sure that's useful in programs that panic to indicate problems. Do those exist? I always treat panics as a design failure.

Duration::from_mins() is useful for me since I've been doing Duration::from_secs(minutes * 60) for some things in my projects, which bugged me a bit.

[–] FizzyOrange@programming.dev 3 points 3 days ago (1 children)

The non-strict versions also panic by default, but only in debug mode. So if you were willing to use abs() you should be willing to use strict_abs().

Arguably a bit of a mistake to have the "obvious" function names be surprisingly unsafe, but I guess it's too late to fix that.

[–] flying_sheep@lemmy.ml 1 points 2 days ago (1 children)

I think this is where safety and performance are actually in conflict.

I think guaranteeing a panic leads to much worse code gen in this case.

[–] FizzyOrange@programming.dev 1 points 2 days ago

Yes that's true, but the point is you should have to opt in to surprising or risky behaviour.

Those const context additions are neat.

[–] 1984@lemmy.today 3 points 3 days ago (1 children)

So much excitement here.. Dont all talk at once.

[–] NGram@piefed.ca 13 points 3 days ago* (last edited 3 days ago) (1 children)

User named 1984 wanting us to talk... hmmm

[–] 1984@lemmy.today 3 points 3 days ago

:) Probably right to be careful, im a bit nuts.