• Nicolas Wavrant's avatar
    erp5_core: fix addToDate when removing a month · 34d26a74
    Nicolas Wavrant authored
    The way addToDate was working with dates was not good, and creating
    confusion when removing 1 month from the last days of a 31-day month, as
    the previous day had less days than the current month:
    
    date = DateTime(2023, 5, 31)
    print date
    print addToDate(date, month=-1)
    > 2023/05/31 00:00:00 GMT+2
    > 2023/05/01 00:00:00 GMT+2
    
    This was even more confusing in March, with february having only 28
    days:
    
    date = DateTime(2023, 3, 31)
    print date
    print addToDate(date, month=-1)
    > 2023/03/31 00:00:00 GMT+2
    > 2023/03/03 00:00:00 GMT+2
    
    The new behavior is to, when removing a month, if the new day of the new
    month is more than the number of days in month to default to the last
    day of the month. For exemple, removing one month from 31/05 becomes
    30/04, and from there it will add/remove the days as necessary.
    
    The real issue being that removing a month is ambiguous and can mean
    a different thing for different people.
    
    For reference, the reference implementation of timedelta in python
    doesn't support adding months:
    
    https://docs.python.org/3/library/datetime.html#datetime.timedelta
    
    I hope my solution will make the more sense in ERP5's context.
    34d26a74
test.erp5.testDateUtils.py 17.6 KB