summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArthur Lutz <arthur.lutz@logilab.fr>2008-03-06 15:36:00 +0100
committerArthur Lutz <arthur.lutz@logilab.fr>2008-03-06 15:36:00 +0100
commitd7c5f67ee6c57331098e85762aaf852410734473 (patch)
tree5c9d391994e971ce64c7e2dd9a8053e2d84d67e0
parentcff1ab3b0447b2c434beee22209195bc168b3ce3 (diff)
downloadlogilab-common-d7c5f67ee6c57331098e85762aaf852410734473.tar.gz
added counting of opendays between two dates
-rw-r--r--date.py23
-rw-r--r--test/unittest_date.py19
2 files changed, 38 insertions, 4 deletions
diff --git a/date.py b/date.py
index fd1d885..b44c8b0 100644
--- a/date.py
+++ b/date.py
@@ -16,6 +16,7 @@
"""date manipulation helper functions"""
+import math
try:
from mx.DateTime import RelativeDateTime, strptime
@@ -82,7 +83,27 @@ else:
if x.day_of_week < 5])
return end
-
+ def nb_open_days(start, end):
+ days = int(math.ceil((end - start).days))
+ weeks, plus = divmod(days, 7)
+ if start.day_of_week > end.day_of_week:
+ plus -= 2
+ elif end.day_of_week == 6:
+ plus -= 1
+ open_days = weeks * 5 + plus
+ nb_week_holidays = len([x for x in get_national_holidays(start,
+ end+1)
+ if x.day_of_week < 5])
+ return open_days - nb_week_holidays
+
+ #def nb_open_days(start, end):
+ # """ brute-force version """
+ # days = [x for x in date_range(start, end) if x.day_of_week < 5]
+ # nb_week_holidays = len([x for x in get_national_holidays(start,
+ # end+1)
+ # if x.day_of_week < 5])
+ # return len(days) - nb_week_holidays
+
def date_range(begin, end, step=STEP):
"""
enumerate dates between begin and end dates.
diff --git a/test/unittest_date.py b/test/unittest_date.py
index 7d7ad18..efd6621 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, add_days_worked
+ from logilab.common.date import endOfMonth, add_days_worked, nb_open_days
except ImportError:
from datetime import date as Date
- endOfMonth = add_days_worked = None
+ endOfMonth = add_days_worked = nb_open_days = None
class DateTC(TestCase):
@@ -48,7 +48,20 @@ class DateTC(TestCase):
# skip holiday + week-end
self.assertEquals(add(Date(2008, 4, 30), 2), Date(2008, 5, 4))
-
+ def test_nb_open_days(self):
+ if nb_open_days is None:
+ self.skip('mx.DateTime is not installed')
+ nb = nb_open_days
+ self.assertEquals(nb(Date(2008, 3, 4), Date(2008, 3, 7)), 3)
+ self.assertEquals(nb(Date(2008, 3, 4), Date(2008, 3, 5)), 1)
+ self.assertEquals(nb(Date(2008, 3, 7), Date(2008, 3, 10)), 1)
+ self.assertEquals(nb(Date(2008, 3, 7), Date(2008, 3, 17)), 6)
+ self.assertEquals(nb(Date(2008, 3, 18), Date(2008, 3, 26)), 5)
+ self.assertEquals(nb(Date(2008, 3, 7), Date(2008, 3, 8)), 1)
+ self.assertEquals(nb(Date(2008, 3, 7), Date(2008, 3, 9)), 1)
+ self.assertEquals(nb(Date(2008, 3, 8), Date(2008, 3, 9)), 0)
+ self.assertEquals(nb(Date(2008, 3, 8), Date(2008, 3, 10)), 0)
+ self.assertEquals(nb(Date(2008, 3, 8), Date(2008, 3, 11)), 1)
if __name__ == '__main__':
unittest_main()