-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: time: add time.AddMonth to add a month without normalization #52775
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
Comments
What is exactly meant by 'normalization'? |
Thank you for your comment, I took the term from the specification of |
We have to have some kind of normalization, and in your example of 2022-03-31 minus one month, you can see that happening, in that we don't wind up with 2022-02-31. I think that what you are suggesting is that if |
Yes exactly @ianlancetaylor. This could be practical to have that in addition of |
I tend to find the need to add months/years indicates that a "civil time" representation may be better "instant time" ( time.Date already normalizes out of range days into the next month (eg, October 32 converts to November 1). It would be confusing to have 2 different conversations within the |
I would expect that AddDate and AddMonth would do the same thing with what look like equivalent parameter lists. #19700 is the proposal (unprocessed) for civil time generally. |
This proposal has been added to the active column of the proposals project |
Why not use AddDate? func AddMonth(t time.Time, m int) time.Time {
x := t.AddDate(0, m, 0)
if d := x.Day(); d != t.Day() {
return x.AddDate(0, 0, -d)
}
return x
} |
Thank you for your comments. Sames as you, I don't expecting different behaviour for using After reflexion, I feel like |
This proposal has been declined as retracted. |
While the
time.AddDate
is mathematically correct with the normalization results. The need to add/substract a month in date without nomalization is there in several areas.AddMonth
returns the time corresponding to adding the given number of months, increment/decrement years if needed and set days tot
by staying in the range days of the month added, without normalize the result.Example:
Same if we reduce the date by one month.
Example:
The text was updated successfully, but these errors were encountered: