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
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.
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.
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.
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 😅
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.C++ has proven (at great expense) that documenting footguns is not sufficient. At all. You have to make them impossible.
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.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_daysright 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.