diff options
author | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> | 2007-03-09 09:11:42 +0100 |
---|---|---|
committer | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> | 2007-03-09 09:11:42 +0100 |
commit | b7e21e539ca02132aac5af459e4ade5d54836ec8 (patch) | |
tree | 873701217284e0415fa6c3c2bcce6906fdbc3837 | |
parent | 275eb5bccec69d4007d2d2dd53b5a0f1536a18de (diff) | |
download | logilab-common-b7e21e539ca02132aac5af459e4ade5d54836ec8.tar.gz |
date.py / unittest_date.py reorganization
date_range is now available if mx.datetime is not installed with
a default implementation using python's std datetime module
updated tests accordingly
-rw-r--r-- | date.py | 98 | ||||
-rw-r--r-- | test/unittest_date.py | 13 |
2 files changed, 60 insertions, 51 deletions
@@ -16,11 +16,61 @@ """date manipulation helper functions""" -from mx.DateTime import RelativeDateTime, strptime -endOfMonth = RelativeDateTime(months=1,day=-1) +try: + from mx.DateTime import RelativeDateTime, strptime + STEP = 1 +except ImportError: + from warnings import warn + warn("mxDateTime not found, holiday management won't be available") + from datetime import timedelta + STEP = timedelta(days=1) +else: + endOfMonth = RelativeDateTime(months=1, day=-1) -def date_range(begin, end, step=1): + FRENCH_FIXED_HOLIDAYS = { + 'jour_an' : '%s-01-01', + 'fete_travail' : '%s-05-01', + 'armistice1945' : '%s-05-08', + 'fete_nat' : '%s-07-14', + 'assomption' : '%s-08-15', + 'toussaint' : '%s-11-01', + 'armistice1918' : '%s-11-11', + 'noel' : '%s-12-25', + } + + + FRENCH_MOBILE_HOLIDAYS = { + 'paques2004' : '2004-04-12', + 'ascension2004' : '2004-05-20', + 'pentecote2004' : '2004-05-31', + + 'paques2005' : '2005-03-28', + 'ascension2005' : '2005-05-05', + 'pentecote2005' : '2005-05-16', + + 'paques2006' : '2006-04-17', + 'ascension2006' : '2006-05-25', + 'pentecote2006' : '2006-06-05', + + 'paques2007' : '2007-04-09', + 'ascension2007' : '2007-05-17', + 'pentecote2007' : '2007-05-28', + } + + + def get_national_holidays(begin, end): + """return french national days off between begin and end""" + holidays = [strptime(datestr, '%Y-%m-%d') + for datestr in FRENCH_MOBILE_HOLIDAYS.values()] + for year in xrange(begin.year, end.year+1): + holidays += [strptime(datestr % year, '%Y-%m-%d') + for datestr in FRENCH_FIXED_HOLIDAYS.values()] + return [day for day in holidays if begin <= day < end] + + + +def date_range(begin, end, step=STEP): """ enumerate dates between begin and end dates. @@ -31,45 +81,3 @@ def date_range(begin, end, step=1): while date < end : yield date date += step - -FRENCH_FIXED_HOLIDAYS = { - 'jour_an' : '%s-01-01', - 'fete_travail' : '%s-05-01', - 'armistice1945' : '%s-05-08', - 'fete_nat' : '%s-07-14', - 'assomption' : '%s-08-15', - 'toussaint' : '%s-11-01', - 'armistice1918' : '%s-11-11', - 'noel' : '%s-12-25', - } - - -FRENCH_MOBILE_HOLIDAYS = { - 'paques2004' : '2004-04-12', - 'ascension2004' : '2004-05-20', - 'pentecote2004' : '2004-05-31', - - 'paques2005' : '2005-03-28', - 'ascension2005' : '2005-05-05', - 'pentecote2005' : '2005-05-16', - - 'paques2006' : '2006-04-17', - 'ascension2006' : '2006-05-25', - 'pentecote2006' : '2006-06-05', - - 'paques2007' : '2007-04-09', - 'ascension2007' : '2007-05-17', - 'pentecote2007' : '2007-05-28', - } - - -def get_national_holidays(begin, end): - """return french national days off between begin and end""" - holidays = [strptime(datestr, '%Y-%m-%d') - for datestr in FRENCH_MOBILE_HOLIDAYS.values()] - for year in xrange(begin.year, end.year+1): - holidays += [strptime(datestr % year, '%Y-%m-%d') - for datestr in FRENCH_FIXED_HOLIDAYS.values()] - return [day for day in holidays if begin <= day < end] - - diff --git a/test/unittest_date.py b/test/unittest_date.py index fdcb644..545b1b7 100644 --- a/test/unittest_date.py +++ b/test/unittest_date.py @@ -3,19 +3,20 @@ Unittests for date helpers """ from logilab.common.testlib import TestCase, unittest_main -from logilab.common.date import date_range, endOfMonth + +from logilab.common.date import date_range try: from mx.DateTime import Date, RelativeDate + from logilab.common.date import endOfMonth except ImportError: - Date = None + from datetime import date as Date + endOfMonth = None class DateTC(TestCase): - + def test_day(self): """enumerate days""" - if Date is None: - self.skip('mx.DateTime is not installed') r = list(date_range(Date(2000,1,1), Date(2000,1,4))) expected = [Date(2000,1,1), Date(2000,1,2), Date(2000,1,3)] self.assertListEquals(r, expected) @@ -25,7 +26,7 @@ class DateTC(TestCase): def test_month(self): """enumerate months""" - if Date is None: + if endOfMonth is None: self.skip('mx.DateTime is not installed') r = list(date_range(Date(2000,1,2), Date(2000,4,4), endOfMonth)) expected = [Date(2000,1,2), Date(2000,2,29), Date(2000,3,31)] |