Commit c1dfe5d8 authored by Nicolas Wavrant's avatar Nicolas Wavrant

erp5_core: fix addToDate when removing a month

See merge request !1768
parents 1b017c2c 34d26a74
Pipeline #27722 failed with stage
in 0 seconds
......@@ -62,6 +62,17 @@ class TestDateUtils(unittest.TestCase):
addToDate(date, month=90).toZone('UTC').ISO())
self.assertEqual(DateTime('2090/01/01 %s' % self.timezone).toZone('UTC').ISO(),
addToDate(date, year=90).toZone('UTC').ISO())
april_1 = DateTime('2001/04/01 %s' % self.timezone)
self.assertEqual(DateTime('2001/05/01 %s' % self.timezone).toZone('UTC').ISO(),
addToDate(april_1, month=1).toZone('UTC').ISO())
december_1 = DateTime('2001/12/01 %s' % self.timezone)
self.assertEqual(DateTime('2002/01/01 %s' % self.timezone).toZone('UTC').ISO(),
addToDate(december_1, month=1).toZone('UTC').ISO())
march_31 = DateTime('2001/03/31 %s' % self.timezone)
self.assertEqual(DateTime('2001/04/30 %s' % self.timezone).toZone('UTC').ISO(),
addToDate(march_31, month=1).toZone('UTC').ISO())
self.assertEqual(DateTime('2001/05/1 %s' % self.timezone).toZone('UTC').ISO(),
addToDate(march_31, month=1, day=1).toZone('UTC').ISO())
def test_negative_add_to_date(self):
date = DateTime('2000/01/01 %s' % self.timezone)
......@@ -77,6 +88,14 @@ class TestDateUtils(unittest.TestCase):
addToDate(date, month=-1).toZone('UTC').ISO())
self.assertEqual(DateTime('1999/01/01 %s' % self.timezone).toZone('UTC').ISO(),
addToDate(date, year=-1).toZone('UTC').ISO())
march_31 = DateTime('2001/03/31 %s' % self.timezone)
self.assertEqual(DateTime('2001/02/28 %s' % self.timezone).toZone('UTC').ISO(),
addToDate(march_31, month=-1).toZone('UTC').ISO())
self.assertEqual(DateTime('2001/02/27 %s' % self.timezone).toZone('UTC').ISO(),
addToDate(march_31, month=-1, day=-1).toZone('UTC').ISO())
may_31 = DateTime('2000/05/31 %s' % self.timezone)
self.assertEqual(DateTime('2000/04/30 %s' % self.timezone).toZone('UTC').ISO(),
addToDate(may_31, month=-1).toZone('UTC').ISO())
def test_float_add_to_date(self):
date = DateTime('2000/01/01 %s' % self.timezone)
......
......@@ -110,6 +110,8 @@ def addToDate(date, to_add=None, **kw):
return_value[larger_key_dict[key]] = return_value[larger_key_dict[key]] + 1
for key in key_list:
if key == 'day':
continue
if to_add.get(key, None) is not None:
return_value[key] = return_value[key] + to_add[key]
del to_add[key]
......@@ -125,7 +127,12 @@ def addToDate(date, to_add=None, **kw):
if local_key not in ('day', 'year'):
treatPositiveValues(return_value, local_key)
day_to_add = return_value['day'] - 1
day_to_add = min(
return_value['day'],
getNumberOfDayInMonth(
DateTime(return_value['year'], int(return_value['month']), 1)
)
) - 1
if to_add.get('day', None) is not None:
day_to_add += to_add['day']
return_value['day'] = 1
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment