You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
DateTime: Support adding months and yeards to dates #4890
Hi,
In Robot currently, we don't have the option to add months to the current data in the DateTime library.
We can now add Hours, Days, and, Years using Add Time to data.
${one_month_later} Add Time To Date ${current_date} 1 months
it we try to add the above it will not recognize as a keyword.
Month is problematic because it can e 28-31 days. The code for handling formats like "1 day 2 hours" is generic and cannot really handle months. In this particular context incrementing and decrementing months could probably be handled separately, though. I personally don't consider this too high priority, but if someone is interested to implement this I'm happy to review a pull request and discuss about some of the corner cases dealing with months would contain.
The problem is that our custom time string format that can be used like "1 day 2 hours" doesn't support months. This format is internally converted to seconds (as a float) that can then be converted to a timedelta object.
Adding support for months is problematic, because the exact amount depends on the context. I think it's pretty much impossible to add months support to our time string format.
It would probably be possible to support months with rhis particular keyword, though. It would require something like this:
The keyword needed to have a custom support for months. Possibly it could separately recognize values like "1 month" and "4 months". Something like "1 month 1 day" would be harder to handle.
Incrementing by months could be handled by changing the month attribute of the underlying datetime object. How to handle cases like adding one month to 2025-01-31 would need to be solved, though.
All in all, I think there could be too many corner cases and too many design decisions to be made for this to be worth implementing. If you @kadsat are interested you certainly can try, but there should be more relevant and more straightforward issues available as well.
@pekkaklarck , hey sorry I couldn't respond earlier. I was on vacation travel, I am thinking of something along these lines, please let me know your opinion...
seeking guidance to see if I am on the right track..
defadd_month_to_a_date(
date : datetime.datetime,
months: int
) ->datetime.datetime:
''' need to implement negative months '''day=date.daymonth=date.monthyear=date.yearrel_year , rel_month=int(months/12), int(math.remainder(months,12))
_, max_days=calendar.monthrange(year+rel_year,month+rel_month)
rel_day=min(
day,
max_days
)
returndatetime.datetime(
month=month+rel_month,
day=rel_day,
year=year+rel_year
)
please let me know if there are any additional edge cases to consider. Note, I am only considering addition.. Subtraction would need additional logic..
The basic idea is to just increment/decrement the month and leave the day part to the original value, right? The day would need to be scaled down if it would be over the maximum of the month, though, and the year would also need to be updated if going over the boundary one way or the other. I guess that makes sense. Could you anyway ask opinions from others e.g. on the #robotframework channel on our Slack? You can point them to this issue.
@pekkaklarck , yes you're correct. I am aiming to just change the month and leave the dates intact.If it falls out of range then I try to bring it down to the most reasonable value.
Certainly I will post on the channel and solicit feedback.
There are some design decisions related to how incrementing or decrementing months is handled that I want to explicitly document:
The day part is generally not changed. If the day is not valid to the resulting month, it is scaled down to the last valid day, though. For example, adding a month to Match 31 yields April 30.
PR #5729 adds separate keywords for incrementing and decrementing months. We could still decide to change that.
First of all, we could just add one keyword for adding months that works also with negative values. The added keywords already handle negative values, so this would only require removing the keyword for decrementing months. We have separate keywords for adding and removing other time intervals, so for symmetry having two keywords also for this usage is probably better.
Yet another alternative would be adding months support the current Add Time To Date and Subtract Time From Date keywords. As I have commented above, adding months support to the time string format is not really possible, though, because those time strings are converted to seconds and we cannot map months to fixed seconds. We could, however, change these keywords so that they handle months given like 1 month or 6 months differently than normal time strings. That would avoid the need to add these special keywords for months altogether. I kind of like that, but I'm not entirely sure is that better or not. In practice we needed to decide do we prefer
PR #5729 has been closed for some reason. If that wasn't an accident and someone else isn't interested to look at this, I'll move this issue away from RF 7.5 scope.
After thinking this enhancement a bit more, I believe it's better to add months support to Add Time To Data and Subtract Time From Date keywords. If we do that, we could also add support to add years using the same approach instead of adding new keywords for that purpose as well.
There are two ways how to do that:
Enhance Add Time To Data and Subtract Time From Date keywords so that they recognize inputs like 1 month and 2 years and handle them separately. The internal Data class could then get methods add_months and add_years similarly as in the now closed PR #5729. This would have a problem that usages like 1 year 1 day wouldn't work. I guess that's better than not supporting months and years at all, but the limitation would still be annoying.
Enhance timestring_to_secs so that it handles years and months. I've earlier commented that it isn't possible because months (and years) don't have a fixed length. That's true in general, but if we know the start date, then handling months and years is actually possible. We could thus enhance timestring_to_secs so that it optionally accepts the start date and when it's given, handles also years and months. This is more complicated than just enhancing Add Time To Data and Subtract Time From Date, but it would also make it easy to add year and month support to other keywords and other usages in general.
I believe the second option is much better. It avoids the need to explain why you can use 1 month but not 1 month 2 days, and it also makes it easy to support months and years also elsewhere. If someone needs months and years support urgently, I'd be fine with using the option 1 in RF 7.5 and switching to option 2 later, though.
changed the title [-]Feature Request :Add option to add months in Current date[/-][+]DateTime: Support adding months and yeards to dates[/+]on Aug 4, 2026
I'd like to take this over now that #5729 is closed, following the second
direction from the latest discussion.
I'll extend timestr_to_secs() with an optional start date so integer calendar
years and months can be combined with the existing fixed units. Then I'll wire
that through the existing Add Time To Date and Subtract Time From Date
keywords. The subtraction path will apply calendar components backwards from
the input date rather than simply negating a forward duration, which matters at
month boundaries such as March 31.
The regression coverage will include end-of-month clamping, leap years,
cross-year changes, negative values, mixed calendar/fixed units, and unchanged
behavior when no start date is supplied. I'll work from current master and
keep the change scoped to #4890.
Activity
[-]Add option to add months in Current date[/-][+]Feature Request :Add option to add months in Current date[/+]pekkaklarck commentedon Oct 25, 2023
Month is problematic because it can e 28-31 days. The code for handling formats like "1 day 2 hours" is generic and cannot really handle months. In this particular context incrementing and decrementing months could probably be handled separately, though. I personally don't consider this too high priority, but if someone is interested to implement this I'm happy to review a pull request and discuss about some of the corner cases dealing with months would contain.
kadsat commentedon Mar 7, 2025
@pekkaklarck , Is this because timedelta object doesn't support months? can I give this a try?
pekkaklarck commentedon Mar 12, 2025
The problem is that our custom time string format that can be used like "1 day 2 hours" doesn't support months. This format is internally converted to seconds (as a float) that can then be converted to a timedelta object.
Adding support for months is problematic, because the exact amount depends on the context. I think it's pretty much impossible to add months support to our time string format.
It would probably be possible to support months with rhis particular keyword, though. It would require something like this:
monthattribute of the underlying datetime object. How to handle cases like adding one month to 2025-01-31 would need to be solved, though.All in all, I think there could be too many corner cases and too many design decisions to be made for this to be worth implementing. If you @kadsat are interested you certainly can try, but there should be more relevant and more straightforward issues available as well.
kadsat commentedon Apr 15, 2025
@pekkaklarck , hey sorry I couldn't respond earlier. I was on vacation travel, I am thinking of something along these lines, please let me know your opinion...
seeking guidance to see if I am on the right track..
pekkaklarck commentedon Apr 16, 2025
Can you give examples with different inputs?
kadsat commentedon Apr 19, 2025
please let me know if there are any additional edge cases to consider. Note, I am only considering addition.. Subtraction would need additional logic..
pekkaklarck commentedon Apr 19, 2025
The basic idea is to just increment/decrement the month and leave the day part to the original value, right? The day would need to be scaled down if it would be over the maximum of the month, though, and the year would also need to be updated if going over the boundary one way or the other. I guess that makes sense. Could you anyway ask opinions from others e.g. on the #robotframework channel on our Slack? You can point them to this issue.
kadsat commentedon Apr 19, 2025
@pekkaklarck , yes you're correct. I am aiming to just change the month and leave the dates intact.If it falls out of range then I try to bring it down to the most reasonable value.
Certainly I will post on the channel and solicit feedback.
pekkaklarck commentedon Aug 3, 2026
PR #5729 looks so good that I added this to RF 7.5 scope.
pekkaklarck commentedon Aug 3, 2026
There are some design decisions related to how incrementing or decrementing months is handled that I want to explicitly document:
pekkaklarck commentedon Aug 3, 2026
PR #5729 adds separate keywords for incrementing and decrementing months. We could still decide to change that.
First of all, we could just add one keyword for adding months that works also with negative values. The added keywords already handle negative values, so this would only require removing the keyword for decrementing months. We have separate keywords for adding and removing other time intervals, so for symmetry having two keywords also for this usage is probably better.
Yet another alternative would be adding months support the current
Add Time To DateandSubtract Time From Datekeywords. As I have commented above, adding months support to the time string format is not really possible, though, because those time strings are converted to seconds and we cannot map months to fixed seconds. We could, however, change these keywords so that they handle months given like1 monthor6 monthsdifferently than normal time strings. That would avoid the need to add these special keywords for months altogether. I kind of like that, but I'm not entirely sure is that better or not. In practice we needed to decide do we preferor
pekkaklarck commentedon Aug 4, 2026
PR #5729 has been closed for some reason. If that wasn't an accident and someone else isn't interested to look at this, I'll move this issue away from RF 7.5 scope.
pekkaklarck commentedon Aug 4, 2026
After thinking this enhancement a bit more, I believe it's better to add months support to
Add Time To DataandSubtract Time From Datekeywords. If we do that, we could also add support to add years using the same approach instead of adding new keywords for that purpose as well.There are two ways how to do that:
Enhance
Add Time To DataandSubtract Time From Datekeywords so that they recognize inputs like1 monthand2 yearsand handle them separately. The internalDataclass could then get methodsadd_monthsandadd_yearssimilarly as in the now closed PR #5729. This would have a problem that usages like1 year 1 daywouldn't work. I guess that's better than not supporting months and years at all, but the limitation would still be annoying.Enhance
timestring_to_secsso that it handles years and months. I've earlier commented that it isn't possible because months (and years) don't have a fixed length. That's true in general, but if we know the start date, then handling months and years is actually possible. We could thus enhancetimestring_to_secsso that it optionally accepts the start date and when it's given, handles also years and months. This is more complicated than just enhancingAdd Time To DataandSubtract Time From Date, but it would also make it easy to add year and month support to other keywords and other usages in general.I believe the second option is much better. It avoids the need to explain why you can use
1 monthbut not1 month 2 days, and it also makes it easy to support months and years also elsewhere. If someone needs months and years support urgently, I'd be fine with using the option 1 in RF 7.5 and switching to option 2 later, though.[-]Feature Request :Add option to add months in Current date[/-][+]DateTime: Support adding months and yeards to dates[/+]fzlzjerry commentedon Aug 10, 2026
I'd like to take this over now that #5729 is closed, following the second
direction from the latest discussion.
I'll extend
timestr_to_secs()with an optional start date so integer calendaryears and months can be combined with the existing fixed units. Then I'll wire
that through the existing
Add Time To DateandSubtract Time From Datekeywords. The subtraction path will apply calendar components backwards from
the input date rather than simply negating a forward duration, which matters at
month boundaries such as March 31.
The regression coverage will include end-of-month clamping, leap years,
cross-year changes, negative values, mixed calendar/fixed units, and unchanged
behavior when no start date is supplied. I'll work from current
masterandkeep the change scoped to #4890.