Brandon Nielsen avatar Brandon Nielsen committed a72b0bf

Added ability to parse a complete date time.

Comments (0)

Files changed (2)

aniso8601/__init__.py

     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]
     #

aniso8601/test_aniso8601.py

         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)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.