From 439be2b4a443bb7edd89f9037d509adb8067f954 Mon Sep 17 00:00:00 2001 From: Sophire <115919609+sophie0x@users.noreply.github.com> Date: Thu, 20 Oct 2022 19:50:58 -0500 Subject: [PATCH] [utils] Add microseconds to unified_timestamp --- test/test_utils.py | 2 ++ yt_dlp/utils.py | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/test/test_utils.py b/test/test_utils.py index 3045b6d7e..fceca03ec 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -415,10 +415,12 @@ def test_unified_timestamps(self): self.assertEqual(unified_timestamp('Sep 11, 2013 | 5:49 AM'), 1378878540) self.assertEqual(unified_timestamp('December 15, 2017 at 7:49 am'), 1513324140) self.assertEqual(unified_timestamp('2018-03-14T08:32:43.1493874+00:00'), 1521016363) + self.assertEqual(unified_timestamp('2022-10-13T02:37:47.831Z'), 1665628667) self.assertEqual(unified_timestamp('December 31 1969 20:00:01 EDT'), 1) self.assertEqual(unified_timestamp('Wednesday 31 December 1969 18:01:26 MDT'), 86) self.assertEqual(unified_timestamp('12/31/1969 20:01:18 EDT', False), 78) + self.assertEqual(unified_timestamp('2023-03-09T18:01:33.646Z', with_milliseconds=True), 1678384893.646) def test_determine_ext(self): self.assertEqual(determine_ext('http://example.com/foo/bar.mp4/?download'), 'mp4') diff --git a/yt_dlp/utils.py b/yt_dlp/utils.py index 8c2c5593c..a2e2c8fc6 100644 --- a/yt_dlp/utils.py +++ b/yt_dlp/utils.py @@ -1843,7 +1843,7 @@ def unified_strdate(date_str, day_first=True): return str(upload_date) -def unified_timestamp(date_str, day_first=True): +def unified_timestamp(date_str, day_first=True, with_milliseconds=False): if date_str is None: return None @@ -1869,7 +1869,7 @@ def unified_timestamp(date_str, day_first=True): for expression in date_formats(day_first): with contextlib.suppress(ValueError): dt = datetime.datetime.strptime(date_str, expression) - timezone + datetime.timedelta(hours=pm_delta) - return calendar.timegm(dt.timetuple()) + return calendar.timegm(dt.timetuple()) + (dt.microsecond/1e6 if with_milliseconds else 0) timetuple = email.utils.parsedate_tz(date_str) if timetuple: