-
Notifications
You must be signed in to change notification settings - Fork 543
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
How to create local date time with zero hour/minute/second #1022
Comments
Ai, I can understand your frustration. That was non-trivial. use chrono::{Local, TimeZone};
fn main() {
let today_date_naive = Local::now().date_naive();
let midnight_naive = today_date_naive.and_hms_opt(0, 0, 0).unwrap();
let midnight_local = Local.from_local_datetime(&midnight_naive).unwrap();
println!("{:?}", midnight_local);
} The deprecation of methods that are infallible as long as you pass values that are in range (like It is maybe interesting to know that if you wanted a time 1 minute after midnight today this program could have panicked in some locales, because it is not uncommon to have a DST transition at midnight. Than that time wouldn't exist. I wonder if a |
I'm not sure how is that possible? |
Just for fun, may I suggest you run the following code on a Unix system with the date and timezone set to 2022-03-25 Damascus, Syria (to pick an example)? use chrono::{Local, TimeZone};
fn main() {
let today_date_naive = Local::now().date_naive();
let midnight_naive = today_date_naive.and_hms_opt(0, 0, 1).unwrap();
let midnight_local = Local.from_local_datetime(&midnight_naive).unwrap();
println!("{:?}", midnight_local);
} That will fail with:
The local time jumps from 0:00:00 to 1:00:00, making the day 23 hours long. On the other hand, one second after midnight can exist (it will just be at 1:00:01): use chrono::{Duration, Local, TimeZone};
fn main() {
let today_date_naive = Local::now().date_naive();
let midnight_naive = today_date_naive.and_hms_opt(0, 0, 0).unwrap();
let midnight_local = Local.from_local_datetime(&midnight_naive).unwrap();
println!("{:?}", midnight_local + Duration::seconds(1));
} Result:
|
I don't understand why it should fail? and how second would matter.
That actually makes chrono unusable. |
Thanks for the comments @DoumanAsh and @pitdicker. There has been quite a bit of past discussion on this as we are trying to find a solution where we offer easy construction of date literals (which are obviously correct from inspection), but also don't catch people out with unexpected panics. With regards to construction of literals, there is a lot of discussion in #815. There is also a PR that implements this, but it requires more discouse on the API before being merged (and also some work to ensure there isn't a major performance regression) One example of a possible API is: let my_time = NaiveTime::<22, 05, 05>::new_hms(); @DoumanAsh with regards to the specific requirement of getting midnight on local time, the easiest way is probably: let midnight_result = Local::now().date_naive().and_time(NaiveTime::MIN).and_local_timezone(Local); The only fallible function here is
I appreciate that some of these API changes - by virtue of aiming for correctness - can make some use cases a little more tricky. However I think in this case, you may be better served by using a fixed offset directly or even just a There is also some discussion on finding the nearest earlier or later valid local time at #716, and one method of this will likely be implemented for |
Got bitten by this when upgrading deps. I decide to pin |
@iwinux Don't you think it is better to just use the line |
Thanks for all the work. I suggest adding this issue to the docs. Just started upgrading an old running project from chrono 0.4.19 where I have some arcane Date code to get (for example) the same local time in +- X days. Took me a bit of searching around to find this issue. Have roundabout 10 places where I use the deprecated API, and I was very confused when trying varying versions of replacing it with the non deprecated versions. Especially since I didn't expect NaiveDate to be a better replacement to |
Due to tons of unecessary deprecations it became impossible to use this library for our use case which is:
and_hms(0, 0, 0)
)There is no need to deprecate useful things unless you have alternative for their usage?
chrono::Local::today
cannot be replaced withchrono::Local::now
as it is.It returns two completely different things
Something simple as creating midnight timestamp should be error-less, yet you force user unwrapping all these infallible operations because you cannot even provide const constructors
The text was updated successfully, but these errors were encountered: