[utils] Add support for zero years and months in parse_duration
authorSergey M․ <dstftw@gmail.com>
Sun, 29 Oct 2017 00:04:48 +0000 (07:04 +0700)
committerSergey M․ <dstftw@gmail.com>
Sun, 29 Oct 2017 00:04:48 +0000 (07:04 +0700)
test/test_utils.py
youtube_dl/utils.py

index efa73d0f45e17b76647a8e7895a5bd3c62f404b3..cc13f795c338d816b442d9d338262eb9323f4f64 100644 (file)
@@ -540,6 +540,7 @@ class TestUtil(unittest.TestCase):
         self.assertEqual(parse_duration('87 Min.'), 5220)
         self.assertEqual(parse_duration('PT1H0.040S'), 3600.04)
         self.assertEqual(parse_duration('PT00H03M30SZ'), 210)
+        self.assertEqual(parse_duration('P0Y0M0DT0H4M20.880S'), 260.88)
 
     def test_fix_xml_ampersands(self):
         self.assertEqual(
index 59fb3343582e6dfa2aab2c6a8311300bd7458141..34866a54b6efc122f4d0edb22712c503fa448ec0 100644 (file)
@@ -1835,10 +1835,20 @@ def parse_duration(s):
         days, hours, mins, secs, ms = m.groups()
     else:
         m = re.match(
-            r'''(?ix)(?:P?T)?
+            r'''(?ix)(?:P?
+                (?:
+                    [0-9]+\s*y(?:ears?)?\s*
+                )?
+                (?:
+                    [0-9]+\s*m(?:onths?)?\s*
+                )?
+                (?:
+                    [0-9]+\s*w(?:eeks?)?\s*
+                )?
                 (?:
                     (?P<days>[0-9]+)\s*d(?:ays?)?\s*
                 )?
+                T)?
                 (?:
                     (?P<hours>[0-9]+)\s*h(?:ours?)?\s*
                 )?