1
0
mirror of https://github.com/l1ving/youtube-dl synced 2024-11-25 10:12:53 +08:00

Improve parse_duration

This commit is contained in:
Sergey M. 2014-02-17 03:46:26 +07:00
parent 3f32c0ba4c
commit 2db806b4aa
2 changed files with 10 additions and 1 deletions

View File

@ -202,7 +202,16 @@ class TestUtil(unittest.TestCase):
self.assertEqual(parse_duration('1'), 1) self.assertEqual(parse_duration('1'), 1)
self.assertEqual(parse_duration('1337:12'), 80232) self.assertEqual(parse_duration('1337:12'), 80232)
self.assertEqual(parse_duration('9:12:43'), 33163) 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('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): def test_fix_xml_ampersands(self):
self.assertEqual( self.assertEqual(

View File

@ -1144,7 +1144,7 @@ def parse_duration(s):
return None return None
m = re.match( 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: if not m:
return None return None
res = int(m.group('secs')) res = int(m.group('secs'))