Open curiousdannii opened 9 months ago
Okay, here's what I ended up with. It's not the worst, but a helper function would still be appreciated:
let mut normalised_date = NaiveDate::from_ymd_opt(year, 1, 1).unwrap();
let months = month - 1;
if months > 0 {
normalised_date = normalised_date.checked_add_months(chrono::Months::new(months as u32)).unwrap();
}
if months < 0 {
normalised_date = normalised_date.checked_sub_months(chrono::Months::new(months as u32)).unwrap();
}
let mut normalised_date = NaiveDateTime::from(normalised_date);
let duration = Duration::days(day as i64 - 1)
+ Duration::hours(hour as i64)
+ Duration::minutes(minute as i64)
+ Duration::seconds(second as i64)
+ Duration::nanoseconds(microsec as i64 * 1000);
normalised_date = normalised_date.checked_add_signed(duration).unwrap();
Other than trying to come up with an exact match for time_gm()
's semantics, what is your code trying to achieve? What is the use case? I'd like to avoid the XY problem here.
I'm porting a library from C to Rust which basically wraps timegm. I have to normalise the dates or I don't pass the unit tests. Rejecting invalid dates like the _opt functions probably does make more sense, but the API isn't up to me.
That said, my code seems to be working, so if you want you can just close this feature request.
Using libc's wrapper of timegm would be another option (albeit it's an unsafe function.)
It sounds like #1290 might address your use case?
I'm porting code which makes use of the normalisation features of C's
timegm
)I couldn't see anything in the current Chrono API which looks suitable for this, though it looks like I could manually do it by calling
checked_add_months
,checked_add_days
, etc (though as negative values are possible I'd need to test and call thesub
functions too.)Is there a part of the API that I missed, or does anyone know of another Rust library that can handle this?