]> jfr.im git - yt-dlp.git/commitdiff
[utils] `strftime_or_none`: Handle negative timestamps
authorpukkandan <redacted>
Tue, 20 Jun 2023 23:48:03 +0000 (05:18 +0530)
committerpukkandan <redacted>
Wed, 21 Jun 2023 00:40:39 +0000 (06:10 +0530)
Closes #6706
Authored by pukkandan, dirkf

yt_dlp/utils/_utils.py

index 1fd6f44af498c8ff3a12c6dcf1138b1b3a058396..256e2db5a99869dc9f0d5afd4ad567c12b1fff6e 100644 (file)
@@ -2452,7 +2452,10 @@ def strftime_or_none(timestamp, date_format, default=None):
         if isinstance(timestamp, (int, float)):  # unix timestamp
             # Using naive datetime here can break timestamp() in Windows
             # Ref: https://github.com/yt-dlp/yt-dlp/issues/5185, https://github.com/python/cpython/issues/94414
-            datetime_object = datetime.datetime.fromtimestamp(timestamp, datetime.timezone.utc)
+            # Also, datetime.datetime.fromtimestamp breaks for negative timestamps
+            # Ref: https://github.com/yt-dlp/yt-dlp/issues/6706#issuecomment-1496842642
+            datetime_object = (datetime.datetime.fromtimestamp(0, datetime.timezone.utc)
+                               + datetime.timedelta(seconds=timestamp))
         elif isinstance(timestamp, str):  # assume YYYYMMDD
             datetime_object = datetime.datetime.strptime(timestamp, '%Y%m%d')
         date_format = re.sub(  # Support %s on windows