diff options
author | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> | 2008-01-31 18:04:11 +0100 |
---|---|---|
committer | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> | 2008-01-31 18:04:11 +0100 |
commit | cf39ca34062fd291ea98a14a41523a89955e8f04 (patch) | |
tree | 6c91a52f3053b3e0636ecf60ab6c4c111b316a13 | |
parent | 21a8afc7f8ae353b4d5102189d6131276ca598ac (diff) | |
download | logilab-common-cf39ca34062fd291ea98a14a41523a89955e8f04.tar.gz |
little function that adds a number of days worked to a date
-rw-r--r-- | ChangeLog | 3 | ||||
-rw-r--r-- | date.py | 16 | ||||
-rw-r--r-- | test/unittest_date.py | 18 |
3 files changed, 33 insertions, 4 deletions
@@ -2,7 +2,8 @@ ChangeLog for logilab.common ============================ -- - * shellutils: new `chmod` function + * date: new `add_days_worked` function + * shellutils: new `chown` function * testlib: new `strict` argument to assertIsInstance * __init__: new `attrdict` and `nullobject` classes @@ -56,8 +56,11 @@ else: 'paques2007' : '2007-04-09', 'ascension2007' : '2007-05-17', 'pentecote2007' : '2007-05-28', - } + 'paques2008' : '2008-03-24', + 'ascension2008' : '2008-05-01', + 'pentecote2008' : '2008-05-12', + } def get_national_holidays(begin, end): """return french national days off between begin and end""" @@ -69,6 +72,16 @@ else: return [day for day in holidays if begin <= day < end] + def add_days_worked(start, days): + """adds date but try to only take days worked into account""" + weeks, plus = divmod(days, 5) + end = start+(weeks * 7) + plus + if end.day_of_week > 4: + end += 2 + end += len([x for x in get_national_holidays(start, end+1) + if x.day_of_week < 5]) + return end + def date_range(begin, end, step=STEP): """ @@ -81,3 +94,4 @@ def date_range(begin, end, step=STEP): while date < end : yield date date += step + diff --git a/test/unittest_date.py b/test/unittest_date.py index 545b1b7..7d7ad18 100644 --- a/test/unittest_date.py +++ b/test/unittest_date.py @@ -8,10 +8,10 @@ from logilab.common.date import date_range try: from mx.DateTime import Date, RelativeDate - from logilab.common.date import endOfMonth + from logilab.common.date import endOfMonth, add_days_worked except ImportError: from datetime import date as Date - endOfMonth = None + endOfMonth = add_days_worked = None class DateTC(TestCase): @@ -35,6 +35,20 @@ class DateTC(TestCase): expected = [Date(2000,11,30), Date(2000,12,31), Date(2001,1,31)] self.assertListEquals(r, expected) + def test_add_days_worked(self): + if add_days_worked is None: + self.skip('mx.DateTime is not installed') + add = add_days_worked + # normal + self.assertEquals(add(Date(2008, 1, 3), 1), Date(2008, 1, 4)) + # skip week-end + self.assertEquals(add(Date(2008, 1, 3), 2), Date(2008, 1, 7)) + # skip 2 week-ends + self.assertEquals(add(Date(2008, 1, 3), 8), Date(2008, 1, 15)) + # skip holiday + week-end + self.assertEquals(add(Date(2008, 4, 30), 2), Date(2008, 5, 4)) + + if __name__ == '__main__': unittest_main() |