Commit 6dd69557 authored by Nicolas Delaby's avatar Nicolas Delaby

* strftime return a string representation of date in timezone server.

  This bug leads to produce unexpected result if timezone of day is changing.
  use ISO() string representation instead
* rename some variables
* add test 


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@24523 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 5242c4af
...@@ -242,31 +242,36 @@ def getIntervalListBetweenDates(from_date=None, to_date=None, ...@@ -242,31 +242,36 @@ def getIntervalListBetweenDates(from_date=None, to_date=None,
else: else:
to_inverse = 0 to_inverse = 0
diff_value = {} diff_value_dict = {}
for key in keys.keys():
if key:
diff_value[key] = []
for current_key in ('year', 'month', 'week', 'day'): for current_key in ('year', 'month', 'week', 'day'):
if keys.get(current_key, None): if keys.get(current_key, None):
new_date = from_date new_date = from_date
while new_date <= to_date: while new_date <= to_date:
diff_value[current_key].append(new_date.strftime(format_dict[current_key])) if current_key == 'day':
new_strftime = new_date.ISO()
new_strftime = new_strftime[:new_strftime.index(' ')]
diff_value_dict.setdefault(current_key, []).append(new_strftime)
else:
diff_value_dict.setdefault(current_key,
[]).append(new_date.strftime(format_dict[current_key]))
if current_key == "week": if current_key == "week":
new_date = addToDate(new_date, to_add={'day':7}) new_date = addToDate(new_date, to_add={'day':7})
else: else:
new_date = addToDate(new_date, to_add={current_key:1}) new_date = addToDate(new_date, to_add={current_key:1})
if to_date.strftime(format_dict[current_key]) not in diff_value[current_key]: if to_date.strftime(format_dict[current_key]) not in\
diff_value[current_key].append(to_date.strftime(format_dict[current_key])) diff_value_dict[current_key]:
diff_value_dict.setdefault(current_key,
[]).append(to_date.strftime(format_dict[current_key]))
returned_value = {} returned_value_dict = {}
for key, value in diff_value.items(): for key, value in diff_value_dict.iteritems():
if to_inverse: if to_inverse:
value.reverse() value.reverse()
returned_value[key] = value returned_value_dict[key] = value
else: else:
returned_value[key] = value returned_value_dict[key] = value
return returned_value return returned_value_dict
def getMonthAndDaysBetween(from_date=None, to_date=None): def getMonthAndDaysBetween(from_date=None, to_date=None):
""" """
......
...@@ -53,6 +53,7 @@ ZopeTestCase.installProduct('ERP5Type') ...@@ -53,6 +53,7 @@ ZopeTestCase.installProduct('ERP5Type')
from DateTime import DateTime from DateTime import DateTime
from Products.ERP5Type.DateUtils import addToDate from Products.ERP5Type.DateUtils import addToDate
from Products.ERP5Type.DateUtils import getIntervalListBetweenDates
class TestDateUtils(unittest.TestCase): class TestDateUtils(unittest.TestCase):
""" """
...@@ -124,6 +125,41 @@ class TestDateUtils(unittest.TestCase): ...@@ -124,6 +125,41 @@ class TestDateUtils(unittest.TestCase):
self.assertEqual(DateTime('2004/09/16 01:23:54 %s' % self.timezone).toZone('UTC').ISO(), self.assertEqual(DateTime('2004/09/16 01:23:54 %s' % self.timezone).toZone('UTC').ISO(),
addToDate(complex_date, year=0.5).toZone('UTC').ISO()) addToDate(complex_date, year=0.5).toZone('UTC').ISO())
def test_interval_list_between_dates(self):
from_date = DateTime('2008/10/23')
to_date = DateTime('2008/11/03')
aggregation_level = 'day'
interval_list_dict = getIntervalListBetweenDates(from_date=from_date,
to_date=to_date,
keys={'day':True})
date_list = interval_list_dict.get('day')
self.assertEqual(len(date_list), 12)
for index, date in enumerate(date_list):
if index == 0:
self.assertEqual(date, '2008-10-23')
elif index == 1:
self.assertEqual(date, '2008-10-24')
elif index == 2:
self.assertEqual(date, '2008-10-25')
elif index == 3:
self.assertEqual(date, '2008-10-26')
elif index == 4:
self.assertEqual(date, '2008-10-27')
elif index == 5:
self.assertEqual(date, '2008-10-28')
elif index == 6:
self.assertEqual(date, '2008-10-29')
elif index == 7:
self.assertEqual(date, '2008-10-30')
elif index == 8:
self.assertEqual(date, '2008-10-31')
elif index == 9:
self.assertEqual(date, '2008-11-01')
elif index == 10:
self.assertEqual(date, '2008-11-02')
elif index == 11:
self.assertEqual(date, '2008-11-03')
def test_suite(): def test_suite():
suite = unittest.TestSuite() suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestDateUtils)) suite.addTest(unittest.makeSuite(TestDateUtils))
......
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