Commit d5cef37e authored by Kazuhiko Shiozaki's avatar Kazuhiko Shiozaki

DateUtils (addToDate): compute day offset in local time manner.

if timezone is Europe/Paris, we expect :

'2021/03/28' + 1 day => '2021/03/29 GMT+1', NOT '2021/03/29 GMT+2.

i.e. 'same time' in the next day.

'2021/03/28' + 3 hours => '2021/03/28 04:00 GMT+2', NOT '2021/03/28 03:00 GMT+1'.

i.e. valid timezone offset at the result time.
parent e051c03e
...@@ -27,12 +27,20 @@ ...@@ -27,12 +27,20 @@
# #
############################################################################## ##############################################################################
import os
import time
import unittest import unittest
from DateTime import DateTime from DateTime import DateTime
from erp5.component.module.DateUtils import addToDate, getIntervalListBetweenDates, \ from erp5.component.module.DateUtils import addToDate, getIntervalListBetweenDates, \
atTheEndOfPeriod, getClosestDate atTheEndOfPeriod, getClosestDate
os.environ['TZ']='Europe/Paris'
time.tzset()
DateTime._localzone0 = 'GMT+1'
DateTime._localzone1 = 'GMT+2'
DateTime._multipleZones = True
class TestDateUtils(unittest.TestCase): class TestDateUtils(unittest.TestCase):
""" """
Tests DateUtils Tests DateUtils
...@@ -175,6 +183,32 @@ class TestDateUtils(unittest.TestCase): ...@@ -175,6 +183,32 @@ class TestDateUtils(unittest.TestCase):
self.assertEqual('Sep. 10, 2008 12:00 am GMT-2', self.assertEqual('Sep. 10, 2008 12:00 am GMT-2',
getClosestDate(date=date, target_date=target_date, precision='month', before=False).pCommonZ()) getClosestDate(date=date, target_date=target_date, precision='month', before=False).pCommonZ())
def test_implicit_local_time(self):
self.assertEqual(
str(addToDate(DateTime('2020/03/10'), month=1)),
'2020/04/10 00:00:00 GMT+2',
)
self.assertEqual(
str(addToDate(DateTime('2020/10/20'), month=1)),
'2020/11/20 00:00:00 GMT+1',
)
self.assertEqual(
str(addToDate(DateTime('2020/03/29'), day=1)),
'2020/03/30 00:00:00 GMT+2',
)
self.assertEqual(
str(addToDate(DateTime('2020/10/25'), day=1)),
'2020/10/26 00:00:00 GMT+1',
)
self.assertEqual(
str(addToDate(DateTime('2020/03/29 00:30:00.123456'), hour=3)),
'2020/03/29 04:30:0.123456 GMT+2',
)
self.assertEqual(
str(addToDate(DateTime('2020/10/25 00:30:00.123456'), hour=3)),
'2020/10/25 02:30:0.123456 GMT+1',
)
def test_suite(): def test_suite():
suite = unittest.TestSuite() suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestDateUtils)) suite.addTest(unittest.makeSuite(TestDateUtils))
......
...@@ -129,15 +129,23 @@ def addToDate(date, to_add=None, **kw): ...@@ -129,15 +129,23 @@ def addToDate(date, to_add=None, **kw):
if to_add.get('day', None) is not None: if to_add.get('day', None) is not None:
day_to_add += to_add['day'] day_to_add += to_add['day']
return_value['day'] = 1 return_value['day'] = 1
return_date = DateTime('%i/%i/%i %i:%i:%d %s' % (return_value['year'], # compute year/month/date with UTC to ignore possible offset change.
return_value['month'], return_date = DateTime('%i/%i/%i UTC' % (return_value['year'],
return_value['day'], return_value['month'],
return_value['hour'], return_value['day'],))
return_value['minute'],
return_value['second'],
date.timezone()))
return_date += day_to_add return_date += day_to_add
timezone = date.timezone()
if DateTime(date.strftime('%Y/%m/%d %H:%M:%S.%f')).timezone() == timezone:
# assume date has local timezone.
timezone = ''
return_date = DateTime(return_date.strftime('%Y/%m/%d ' + timezone)) + \
return_value['hour'] / 24. + \
return_value['minute'] / 1440. + \
return_value['second'] / 86400.
if not timezone:
# convert to valid local time at return_date timing.
return_date = DateTime(float(return_date))
return return_date return return_date
def getClosestDate(date=None, target_date=None, def getClosestDate(date=None, target_date=None,
......
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