Chrono constants

Hi,

Chrono have (not so recently) made most init functions return options to ensure no panics. This is a very good change but leads to a challenge in how to initiate Date / Time constants without resulting to unwrap().

Consider e.g: wanting to create the constant NaiveDateTime 1900-03-01T00:00:00.

It seems that the only way right now is: NaiveDate::from_ymd_opt(1900, 3, 1).unwrap().and_hms_opt(0, 0, 0).unwrap()

Is there really no way to create such constants without having to resort to .unwrap()? I dislike the unwraps since we try to avoid them as much as possible in our code-base - they are usually an indication of subtle code that needs to be carefully considered - but end up all over the place when working with chrono types.

Any suggestions / ideas would be aprechiated

Where are you creating those instances? If it's within a fallible function, you can convert the Option into a `Result.

If creating those instances is part of the initialization of your application, then you could use expect rather than unwrap if you have a lintern on unwraps or something.

Its typically in infallible functions, yes sure I could use expect but it's really the same "problem" as with unwrap

You can do the unwrap/panic in a const context so that any failures happen at compile time instead of runtime:

use chrono::NaiveDate;

const fn const_unwrap<T:Copy>(x:Option<T>)->T {
    if let Some(x)=x { x } else { panic!("") }
}

static INVALID: NaiveDate = const_unwrap(NaiveDate::from_ymd_opt(2000,12,32));

(Unfortunately, const unwrap and expect aren’t stabilized yet so you have to write your own const function to do the job at the moment)

Thx, agreed, that is a significantly better solution.

Still surprised there is no "cleaner" way to create chrono constants, but this does indeed work