Improve parse_duration
authorSergey M. <dstftw@gmail.com>
Sun, 16 Feb 2014 20:46:26 +0000 (03:46 +0700)
committerSergey M. <dstftw@gmail.com>
Sun, 16 Feb 2014 20:46:26 +0000 (03:46 +0700)
test/test_utils.py
youtube_dl/utils.py

index 1ca5f5af8d55228b42751a036306b5768805cf60..84553b94386236352e9ad856782c4b3428f3587b 100644 (file)
@@ -202,7 +202,16 @@ class TestUtil(unittest.TestCase):
         self.assertEqual(parse_duration('1'), 1)
         self.assertEqual(parse_duration('1337:12'), 80232)
         self.assertEqual(parse_duration('9:12:43'), 33163)
+        self.assertEqual(parse_duration('12:00'), 720)
+        self.assertEqual(parse_duration('00:01:01'), 61)
         self.assertEqual(parse_duration('x:y'), None)
+        self.assertEqual(parse_duration('3h11m53s'), 11513)
+        self.assertEqual(parse_duration('62m45s'), 3765)
+        self.assertEqual(parse_duration('6m59s'), 419)
+        self.assertEqual(parse_duration('49s'), 49)
+        self.assertEqual(parse_duration('0h0m0s'), 0)
+        self.assertEqual(parse_duration('0m0s'), 0)
+        self.assertEqual(parse_duration('0s'), 0)
 
     def test_fix_xml_ampersands(self):
         self.assertEqual(
index dd03f058ff5581497099523e35022412e373252b..a192a420ac5b8311d6a27298b6d8ecaf6e6398df 100644 (file)
@@ -1144,7 +1144,7 @@ def parse_duration(s):
         return None
 
     m = re.match(
-        r'(?:(?:(?P<hours>[0-9]+):)?(?P<mins>[0-9]+):)?(?P<secs>[0-9]+)$', s)
+        r'(?:(?:(?P<hours>[0-9]+)[:h])?(?P<mins>[0-9]+)[:m])?(?P<secs>[0-9]+)s?$', s)
     if not m:
         return None
     res = int(m.group('secs'))