Commits

yikelu  committed 0b25463

Adding biz_days_between with tests

Also, hgignore ignores build directory.

  • Participants
  • Parent commits 84af897

Comments (0)

Files changed (3)

 
 *.pyc
 *~
-
+build/

File bizdatetime.py

 
         return delta.days - self.weekends_between(day1, day2) - self.holidays_between(day1, day2)
 
+    def biz_days_between(self, day1, day2):
+        """
+        Returns a list of all business days between day1 and day2, inclusive.
+
+        >>> policy = Policy(weekends=(SAT, SUN), holidays=(date(2013, 9, 2),))
+        >>> dlist = policy.biz_days_between(date(2013, 9, 1), date(2013, 9, 30))
+        >>> len(dlist) # there are 20 biz days between
+        20
+        >>> dlist[0] == date(2013, 9, 1) # should not include the starting point if it's not a biz day
+        False
+        >>> dlist[19] == date(2013, 9, 30) # should include end point
+        True
+        """
+        day1 = self.closest_biz_day(day1, forward = True)
+        day2 = self.closest_biz_day(day2, forward = False)
+        delta_incl = self.biz_day_delta(day1, day2) + 1
+        return [self.add(day1, x) for x in xrange(delta_incl)]
 
 if __name__ == "__main__":
     # run tests when called directly
     date(2011, 10, 10), # October 10. Thanksgiving Day
     date(2011, 12, 26), # December 26. Christmas Day
     date(2011, 12, 27), # December 27. Boxing Day
+    date(2013, 9, 2), # Labor day 2013
 )
 
 
     def setUp(self):
         self.policy = Policy(weekends=(SAT, SUN), holidays=holidays)
 
+    def test_biz_days_between(self):
+        policy = self.policy
+
+        FULL_DAYS = 20
+        # Beginning is off day
+        dlist = policy.biz_days_between(date(2013, 9, 1), date(2013, 9, 30))
+        self.assertEqual(len(dlist), FULL_DAYS)
+        self.assertFalse(dlist[0] == date(2013, 9, 1))
+        self.assertTrue(dlist[FULL_DAYS - 1] == date(2013, 9, 30))
+        # Neither day is off
+        dlist = policy.biz_days_between(date(2013, 9, 3), date(2013, 9, 30))
+        self.assertEqual(len(dlist), FULL_DAYS)
+        self.assertTrue(dlist[0] == date(2013, 9, 3))
+        self.assertTrue(dlist[FULL_DAYS - 1] == date(2013, 9, 30))
+
+        LESS_DAYS = 19
+        # Ending day is off
+        dlist = policy.biz_days_between(date(2013, 9, 3), date(2013, 9, 28))
+        self.assertEqual(len(dlist), LESS_DAYS)
+        self.assertTrue(dlist[0] == date(2013, 9, 3))
+        self.assertFalse(dlist[LESS_DAYS - 1] == date(2013, 9, 28))
+        # Both are off
+        dlist = policy.biz_days_between(date(2013, 9, 1), date(2013, 9, 28))
+        self.assertEqual(len(dlist), LESS_DAYS)
+        self.assertFalse(dlist[0] == date(2013, 9, 1))
+        self.assertFalse(dlist[LESS_DAYS - 1] == date(2013, 9, 28))
+
     def test_positive_addition(self):
         self.assertEqual(self.policy.add(date(2011, 3, 3), 1), date(2011, 3, 4))
         self.assertEqual(self.policy.add(date(2011, 3, 3), 2), date(2011, 3, 7))