Commits

Erik Grinaker committed 4385512

added support for changing Date calendar

Comments (0)

Files changed (2)

     :mod:`chrono.parser` for available parsers.
     """
 
+    calendar = calendar.ISOCalendar
+    """
+    Calendar to use for calendar operations, defaults to
+    :class:`chrono.calendar.ISOCalendar`. See :mod:`chrono.calendar`
+    for available calendars.
+    """
+
     day = None
     """
     Day number, range 1-31 depending on :attr:`chrono.Date.month` and
 
         return self.__cmp__(other) > 0
 
-    def __init__(self, date=None, parser=None, **kwargs):
+    def __init__(self, date=None, parser=None, calendar=None, **kwargs):
 
         if parser:
             self.parser = parser
 
+        if calendar:
+            self.calendar = calendar
+
         if isinstance(date, str):
             self.set_string(date)
 
 
         self.assert_set()
 
-        return calendar.ISOCalendar.leapyear(self.year)
+        return self.calendar.leapyear(self.year)
 
     def monthdays(self):
         """
 
         self.assert_set()
 
-        return calendar.ISOCalendar.monthdays(self.year, self.month)
+        return self.calendar.monthdays(self.year, self.month)
 
     def ordinal(self):
         """
 
         self.assert_set()
 
-        return calendar.ISOCalendar.ordinal(self.year, self.month, self.day)
+        return self.calendar.ordinal(self.year, self.month, self.day)
 
     def set(self, year, month, day):
         """
         month = utility.int_month(month)
         day = utility.int_day(day)
 
-        calendar.ISOCalendar.validate(year, month, day)
+        self.calendar.validate(year, month, day)
 
         self.clear()
 
 
         self.assert_set()
 
-        return calendar.ISOCalendar.week(self.year, self.month, self.day)
+        return self.calendar.week(self.year, self.month, self.day)
 
     def weekdate(self):
         """
 
         self.assert_set()
 
-        return calendar.ISOCalendar.weekdate(self.year, self.month, self.day)
+        return self.calendar.weekdate(self.year, self.month, self.day)
 
     def weekday(self):
         """
 
         self.assert_set()
 
-        return calendar.ISOCalendar.weekday(self.year, self.month, self.day)
+        return self.calendar.weekday(self.year, self.month, self.day)
 
     def weeks(self):
         """
 
         self.assert_set()
 
-        return calendar.ISOCalendar.weeks(self.year)
+        return self.calendar.weeks(self.year)
 
     def yeardays(self):
         """
 
         self.assert_set()
 
-        return calendar.ISOCalendar.yeardays(self.year)
+        return self.calendar.yeardays(self.year)

tests/test_date.py

 
 class Date__initTest(unittest.TestCase):
 
+    def test_calendar(self):
+        "Date.__init__() takes calendar as input"
+
+        c = chrono.calendar.USCalendar
+
+        self.assertEqual(chrono.Date(None, None, c).calendar, c)
+
+    def test_calendar_default(self):
+        "Date.__init__() defaults to ISOCalendar"
+
+        self.assertEqual(chrono.Date().calendar, chrono.calendar.ISOCalendar)
+
     def test_date(self):
         "Date.__init__() with Date instance parameter copies attributes"