How to advance to next month #131
-
This snippet failed: next = advance_time_and_round(next, 1.month(), Some(Unit::Month))?;
fn advance_time_and_round(zoned: Zoned, span: Span, unit: Option<Unit>) -> Result<Zoned, Error> {
let mut next = zoned;
next = next
.checked_add(span)
.map_err(time_error_with_context(&format!(
"failed to advance timestamp; end with {next}"
)))?;
if let Some(unit) = unit {
next = next.round(unit).map_err(time_error_with_context(&format!(
"failed to round timestamp; end with {next}"
)))?;
}
Ok(next)
}
I wonder if there is an alternative to advent to next month correctly. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Perhaps: let rest_days = next.days_in_month() - next.day() + 1;
next = advance_time_and_round(next, rest_days.days(), Some(Unit::Day))?; But I don't know if there is some edge case. |
Beta Was this translation helpful? Give feedback.
-
For the specific error you're seeing, see #1. As for how to advance to the next month, just add it: It looks like you're doing that already. So it looks like what your code is doing is something more than just "go to next month." Perhaps what you want is to "go to the first day of the next month." I would do that by Also, in the future, please provide a complete MRE. Thanks. |
Beta Was this translation helpful? Give feedback.
For the specific error you're seeing, see #1.
As for how to advance to the next month, just add it:
zdt.checked_add(1.month())
.It looks like you're doing that already. So it looks like what your code is doing is something more than just "go to next month." Perhaps what you want is to "go to the first day of the next month." I would do that by
zdt.first_of_month()?.checked_add(1.month())?
.Also, in the future, please provide a complete MRE. Thanks.