Commits

Brandon Nielsen committed b5caf1a

Finish duration parsing support.

Comments (0)

Files changed (2)

aniso8601/__init__.py

 
     return datetime.datetime.combine(datepart, timepart)
 
+def parse_duration(durationstr):
+    #Given a string representing an ISO 8601 duration, return a
+    #datetime.timedelta that matches the given duration. Valid formts are:
+    #
+    #PnYnMnDTnHnMnS (or any reduced precision equivalent)
+    #P<date>T<time>
+
+    if durationstr[0] != 'P':
+        raise ValueError('String is not a valid ISO8601 duration.')
+
+    #If Y, M, D, H, or S are in the string, assume it is a specified duration
+    if durationstr.find('Y') != -1 or durationstr.find('M') != -1 or durationstr.find('D') != -1 or durationstr.find('H') != -1 or durationstr.find('S') != -1:
+        return parse_duration_prescribed(durationstr)
+    else:
+        return parse_duration_combined(durationstr)
+
 def parse_year(yearstr):
     #yearstr is of the format Y[YYY]
     #

aniso8601/test_aniso8601.py

         self.assertEqual(tzinfoobject.utcoffset(None), -datetime.timedelta(hours=12, minutes=34))
         self.assertEqual(tzinfoobject.tzname(None), '-12:34')
 
+    def test_parse_duration(self):
+        resultduration = aniso8601.parse_duration('P1Y2M3DT4H54M6S')
+        self.assertEqual(resultduration.days, 428)
+        self.assertEqual(resultduration.seconds, 17646)
+
+        resultduration = aniso8601.parse_duration('P1Y2M3DT4H54M6.5S')
+        self.assertEqual(resultduration.days, 428)
+        self.assertEqual(resultduration.seconds, 17646)
+        self.assertEqual(resultduration.microseconds, 500000)
+
+        resultduration = aniso8601.parse_duration('P1Y2M3D')
+        self.assertEqual(resultduration.days, 428)
+
+        resultduration = aniso8601.parse_duration('PT4H54M6.5S')
+        self.assertEqual(resultduration.days, 0)
+        self.assertEqual(resultduration.seconds, 17646)
+        self.assertEqual(resultduration.microseconds, 500000)
+
+        resultduration = aniso8601.parse_duration('P1Y')
+        self.assertEqual(resultduration.days, 365)
+
+        resultduration = aniso8601.parse_duration('P1M')
+        self.assertEqual(resultduration.days, 30)
+
+        resultduration = aniso8601.parse_duration('P0003-06-04T12:30:05')
+        self.assertEqual(resultduration.days, 1279)
+        self.assertEqual(resultduration.seconds, 45005)
+        self.assertEqual(resultduration.microseconds, 0)
+
+        resultduration = aniso8601.parse_duration('P0003-06-04T12:30:05.5')
+        self.assertEqual(resultduration.days, 1279)
+        self.assertEqual(resultduration.seconds, 45005)
+        self.assertEqual(resultduration.microseconds, 500000)
+
     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.