1. Brandon Nielsen
  2. aniso8601

Commits

Brandon Nielsen  committed a72b0bf

Added ability to parse a complete date time.

  • Participants
  • Parent commits 0f9612d
  • Branches master

Comments (0)

Files changed (2)

File aniso8601/__init__.py

View file
  • Ignore whitespace
     else:
         return parse_time_naive(timestr).replace(tzinfo=parse_timezone(tzstr))
 
+def parse_datetime(isodatetimestr, delimiter='T'):
+    #Given a string in ISO8601 date time format, return a datetime.datetime
+    #object that corresponds to the given date time.
+    #By default, the ISO8601 specified T delimiter is used to split the
+    #date and time (<date>T<time>). Fixed offset tzdata will be included
+    #if UTC offset is given in the input string.
+
+    isodatestr, isotimestr = isodatetimestr.split(delimiter)
+
+    datepart = parse_date(isodatestr)
+    timepart = parse_time(isotimestr)
+
+    return datetime.datetime.combine(datepart, timepart)
+
 def parse_year(yearstr):
     #yearstr is of the format Y[YYY]
     #

File aniso8601/test_aniso8601.py

View file
  • Ignore whitespace
         self.assertEqual(tzinfoobject.utcoffset(None), datetime.timedelta(hours=0))
         self.assertEqual(tzinfoobject.tzname(None), 'UTC')
 
+    def test_parse_datetime(self):
+        resultdatetime = aniso8601.parse_datetime('1981-04-05T23:21:28.512400Z')
+        self.assertEqual(resultdatetime.year, 1981)
+        self.assertEqual(resultdatetime.month, 4)
+        self.assertEqual(resultdatetime.day, 5)
+        self.assertEqual(resultdatetime.hour, 23)
+        self.assertEqual(resultdatetime.minute, 21)
+        self.assertEqual(resultdatetime.second, 28)
+        self.assertEqual(resultdatetime.microsecond, 512400)
+        tzinfoobject = resultdatetime.tzinfo
+        self.assertEqual(tzinfoobject.utcoffset(None), datetime.timedelta(hours=0))
+        self.assertEqual(tzinfoobject.tzname(None), 'UTC')
+
+        resultdatetime = aniso8601.parse_datetime('1981095T23:21:28.512400-12:34')
+        self.assertEqual(resultdatetime.year, 1981)
+        self.assertEqual(resultdatetime.month, 4)
+        self.assertEqual(resultdatetime.day, 5)
+        self.assertEqual(resultdatetime.hour, 23)
+        self.assertEqual(resultdatetime.minute, 21)
+        self.assertEqual(resultdatetime.second, 28)
+        self.assertEqual(resultdatetime.microsecond, 512400)
+        tzinfoobject = resultdatetime.tzinfo
+        self.assertEqual(tzinfoobject.utcoffset(None), -datetime.timedelta(hours=12, minutes=34))
+        self.assertEqual(tzinfoobject.tzname(None), '-12:34')
+
+        resultdatetime = aniso8601.parse_datetime('19810405T23:21:28+00')
+        self.assertEqual(resultdatetime.year, 1981)
+        self.assertEqual(resultdatetime.month, 4)
+        self.assertEqual(resultdatetime.day, 5)
+        self.assertEqual(resultdatetime.hour, 23)
+        self.assertEqual(resultdatetime.minute, 21)
+        self.assertEqual(resultdatetime.second, 28)
+        tzinfoobject = resultdatetime.tzinfo
+        self.assertEqual(tzinfoobject.utcoffset(None), datetime.timedelta(hours=0))
+        self.assertEqual(tzinfoobject.tzname(None), '+00')
+
+        resultdatetime = aniso8601.parse_datetime('19810405T23:21:28+00:00')
+        self.assertEqual(resultdatetime.year, 1981)
+        self.assertEqual(resultdatetime.month, 4)
+        self.assertEqual(resultdatetime.day, 5)
+        self.assertEqual(resultdatetime.hour, 23)
+        self.assertEqual(resultdatetime.minute, 21)
+        self.assertEqual(resultdatetime.second, 28)
+        tzinfoobject = resultdatetime.tzinfo
+        self.assertEqual(tzinfoobject.utcoffset(None), datetime.timedelta(hours=0))
+        self.assertEqual(tzinfoobject.tzname(None), '+00:00')
+
     def test_parse_year(self):
         date = aniso8601.parse_year('2013')
         self.assertEqual(date.year, 2013)