Commits

Erik Grinaker committed 946983c

use DateTime.calendar and parser attributes

Comments (0)

Files changed (2)

chrono/datetime.py

 
     Valid values for *datetime* can be:
 
-    * string: parses date/time from a string, see
-      :class:`chrono.parser.ISOParser` for valid formats
+    * string: parses date/time from a string using the set parser,
+      :class:`chrono.parser.CommonParser` by default
     * **True**: sets the date/time to the current date
     * integer: assumes input is a UNIX timestamp, sets date/time accordingly
     * :class:`chrono.DateTime`: sets date/time from another DateTime object
 
     If both *datetime* and keywords are specified, *datetime* takes
     precedence.
+
+    *parser* determines which parser to use for parsing dates and times
+    from strings. By default :class:`chrono.parser.CommonParser` is used,
+    which supports the most common date and time formats. See
+    :mod:`chrono.parser` for available parsers.
+
+    *calendar* determines which calendar to use for calendar operations.
+    By default :class:`chrono.calendar.ISOCalendar` is used, see
+    :mod:`chrono.calendar` for available calendars.
     """
 
     def __cmp__(self, other):
 
         return time.Time.__cmp__(self, other)
 
-    def __init__(self, datetime=None, **kwargs):
+    def __init__(self, datetime=None, parser=None, calendar=None, **kwargs):
+
+        if parser:
+            self.parser = parser
+
+        if calendar:
+            self.calendar = calendar
 
         if isinstance(datetime, str):
             self.set_string(datetime)
         minute = utility.int_minute(minute)
         second = utility.int_second(second)
 
-        calendar.ISOCalendar.validate(year, month, day)
+        self.calendar.validate(year, month, day)
         clock.Clock.validate(hour, minute, second)
 
         self.clear()
         """
 
         year, month, date, hour, minute, second = \
-            parser.ISOParser.parse_datetime(string)
+            self.parser.parse_datetime(string)
 
         self.set(year, month, date, hour, minute, second)
 

tests/test_datetime.py

 
 class DateTime__initTest(unittest.TestCase):
 
+    def test_calendar(self):
+        "DateTime.__init__() takes calendar as input"
+
+        c = chrono.calendar.USCalendar
+
+        self.assertEqual(chrono.DateTime(None, None, c).calendar, c)
+
+    def test_calendar_default(self):
+        "DateTime.__init__() defaults to ISOCalendar"
+
+        self.assertEqual(chrono.DateTime().calendar, chrono.calendar.ISOCalendar)
+
     def test_date(self):
         "DateTime.__init__() with Date parameter copies attributes"
 
             chrono.error.NoDateTimeError, chrono.DateTime(None).get
         )
 
+    def test_parser(self):
+        "DateTime.__init__() takes parser as input"
+
+        p = chrono.parser.ISOParser
+        d = chrono.DateTime("2009-07-23 16:27:43", p)
+
+        self.assertEqual(d.get(), (2009, 7, 23, 16, 27, 43))
+        self.assertEqual(d.parser, p)
+
+    def test_parser_default(self):
+        "DateTime.__init__() defaults to CommonParser"
+
+        d = chrono.DateTime("2009-07-23 16:27:43")
+
+        self.assertEqual(d.get(), (2009, 7, 23, 16, 27, 43))
+        self.assertEqual(d.parser, chrono.parser.CommonParser)
+
     def test_string(self):
         "DateTime.__init__() parses strings"