1. Brandon Nielsen
  2. aniso8601

Commits

Brandon Nielsen  committed b5caf1a

Finish duration parsing support.

  • Participants
  • Parent commits 21320ae
  • Branches master

Comments (0)

Files changed (2)

File aniso8601/__init__.py

View file
  • Ignore whitespace
 
     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]
     #

File aniso8601/test_aniso8601.py

View file
  • Ignore whitespace
         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)