A feature-rich command-line audio/video downloader
[cleanup] Standardize `import datetime as dt` (#8978)
pukkandanc305a25c1b16
diff --git a/devscripts/tomlparse.py b/devscripts/tomlparse.pyindex 85ac4eef78..ac9ea31707 100755--- a/devscripts/tomlparse.py+++ b/devscripts/tomlparse.py@@ -11,7 +11,7 @@ IMPORTANT: INVALID FILES OR MULTILINE STRINGS ARE NOT SUPPORTED!from __future__ import annotations-import datetime+import datetime as dtimport jsonimport re@@ -115,9 +115,9 @@ def parse_value(data: str, index: int):for func in [int,float,- datetime.time.fromisoformat,- datetime.date.fromisoformat,- datetime.datetime.fromisoformat,+ dt.time.fromisoformat,+ dt.date.fromisoformat,+ dt.datetime.fromisoformat,{'true': True, 'false': False}.get,]:try:@@ -179,7 +179,7 @@ def main():data = file.read()def default(obj):- if isinstance(obj, (datetime.date, datetime.time, datetime.datetime)):+ if isinstance(obj, (dt.date, dt.time, dt.datetime)):return obj.isoformat()print(json.dumps(parse_toml(data), default=default))diff --git a/devscripts/update-version.py b/devscripts/update-version.pyindex da54a6a258..07a0717458 100644--- a/devscripts/update-version.py+++ b/devscripts/update-version.py@@ -9,15 +9,15 @@ sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))import argparseimport contextlib+import datetime as dtimport sys-from datetime import datetime, timezonefrom devscripts.utils import read_version, run_process, write_filedef get_new_version(version, revision):if not version:- version = datetime.now(timezone.utc).strftime('%Y.%m.%d')+ version = dt.datetime.now(dt.timezone.utc).strftime('%Y.%m.%d')if revision:assert revision.isdecimal(), 'Revision must be a number'diff --git a/test/test_cookies.py b/test/test_cookies.pyindex 5282ef6215..bd61f30a66 100644--- a/test/test_cookies.py+++ b/test/test_cookies.py@@ -1,5 +1,5 @@+import datetime as dtimport unittest-from datetime import datetime, timezonefrom yt_dlp import cookiesfrom yt_dlp.cookies import (@@ -138,7 +138,7 @@ class TestCookies(unittest.TestCase):self.assertEqual(cookie.name, 'foo')self.assertEqual(cookie.value, 'test%20%3Bcookie')self.assertFalse(cookie.secure)- expected_expiration = datetime(2021, 6, 18, 21, 39, 19, tzinfo=timezone.utc)+ expected_expiration = dt.datetime(2021, 6, 18, 21, 39, 19, tzinfo=dt.timezone.utc)self.assertEqual(cookie.expires, int(expected_expiration.timestamp()))def test_pbkdf2_sha1(self):diff --git a/yt_dlp/YoutubeDL.py b/yt_dlp/YoutubeDL.pyindex e83108619e..291fc8d00c 100644--- a/yt_dlp/YoutubeDL.py+++ b/yt_dlp/YoutubeDL.py@@ -1,7 +1,7 @@import collectionsimport contextlibimport copy-import datetime+import datetime as dtimport errnoimport fileinputimport http.cookiejar@@ -2629,7 +2629,7 @@ class YoutubeDL:# Working around out-of-range timestamp values (e.g. negative ones on Windows,# see http://bugs.python.org/issue1646728)with contextlib.suppress(ValueError, OverflowError, OSError):- upload_date = datetime.datetime.fromtimestamp(info_dict[ts_key], datetime.timezone.utc)+ upload_date = dt.datetime.fromtimestamp(info_dict[ts_key], dt.timezone.utc)info_dict[date_key] = upload_date.strftime('%Y%m%d')if not info_dict.get('release_year'):@@ -2783,7 +2783,7 @@ class YoutubeDL:get_from_start = not info_dict.get('is_live') or bool(self.params.get('live_from_start'))if not get_from_start:- info_dict['title'] += ' ' + datetime.datetime.now().strftime('%Y-%m-%d %H:%M')+ info_dict['title'] += ' ' + dt.datetime.now().strftime('%Y-%m-%d %H:%M')if info_dict.get('is_live') and formats:formats = [f for f in formats if bool(f.get('is_from_start')) == get_from_start]if get_from_start and not formats:diff --git a/yt_dlp/cookies.py b/yt_dlp/cookies.pyindex 28d174a09f..85d6dd1823 100644--- a/yt_dlp/cookies.py+++ b/yt_dlp/cookies.py@@ -1,6 +1,7 @@import base64import collectionsimport contextlib+import datetime as dtimport globimport http.cookiejarimport http.cookies@@ -15,7 +16,6 @@ import sysimport tempfileimport timeimport urllib.request-from datetime import datetime, timedelta, timezonefrom enum import Enum, autofrom hashlib import pbkdf2_hmac@@ -594,7 +594,7 @@ class DataParser:def _mac_absolute_time_to_posix(timestamp):- return int((datetime(2001, 1, 1, 0, 0, tzinfo=timezone.utc) + timedelta(seconds=timestamp)).timestamp())+ return int((dt.datetime(2001, 1, 1, 0, 0, tzinfo=dt.timezone.utc) + dt.timedelta(seconds=timestamp)).timestamp())def _parse_safari_cookies_header(data, logger):diff --git a/yt_dlp/extractor/atvat.py b/yt_dlp/extractor/atvat.pyindex d6ed9e4958..d60feba315 100644--- a/yt_dlp/extractor/atvat.py+++ b/yt_dlp/extractor/atvat.py@@ -1,4 +1,4 @@-import datetime+import datetime as dtfrom .common import InfoExtractorfrom ..utils import (@@ -71,9 +71,9 @@ class ATVAtIE(InfoExtractor):content_ids = [{'id': id, 'subclip_start': content['start'], 'subclip_end': content['end']}for id, content in enumerate(contentResource)]- time_of_request = datetime.datetime.now()- not_before = time_of_request - datetime.timedelta(minutes=5)- expire = time_of_request + datetime.timedelta(minutes=5)+ time_of_request = dt.datetime.now()+ not_before = time_of_request - dt.timedelta(minutes=5)+ expire = time_of_request + dt.timedelta(minutes=5)payload = {'content_ids': {content_id: content_ids,diff --git a/yt_dlp/extractor/aws.py b/yt_dlp/extractor/aws.pyindex c4741a6a11..4ebef92957 100644--- a/yt_dlp/extractor/aws.py+++ b/yt_dlp/extractor/aws.py@@ -1,4 +1,4 @@-import datetime+import datetime as dtimport hashlibimport hmac@@ -12,7 +12,7 @@ class AWSIE(InfoExtractor): # XXX: Conventionally, base classes should end withdef _aws_execute_api(self, aws_dict, video_id, query=None):query = query or {}- amz_date = datetime.datetime.now(datetime.timezone.utc).strftime('%Y%m%dT%H%M%SZ')+ amz_date = dt.datetime.now(dt.timezone.utc).strftime('%Y%m%dT%H%M%SZ')date = amz_date[:8]headers = {'Accept': 'application/json',diff --git a/yt_dlp/extractor/cda.py b/yt_dlp/extractor/cda.pyindex 1157114b2a..90b4d082e2 100644--- a/yt_dlp/extractor/cda.py+++ b/yt_dlp/extractor/cda.py@@ -1,6 +1,6 @@import base64import codecs-import datetime+import datetime as dtimport hashlibimport hmacimport json@@ -134,7 +134,7 @@ class CDAIE(InfoExtractor):self._API_HEADERS['User-Agent'] = f'pl.cda 1.0 (version {app_version}; Android {android_version}; {phone_model})'cached_bearer = self.cache.load(self._BEARER_CACHE, username) or {}- if cached_bearer.get('valid_until', 0) > datetime.datetime.now().timestamp() + 5:+ if cached_bearer.get('valid_until', 0) > dt.datetime.now().timestamp() + 5:self._API_HEADERS['Authorization'] = f'Bearer {cached_bearer["token"]}'return@@ -154,7 +154,7 @@ class CDAIE(InfoExtractor):})self.cache.store(self._BEARER_CACHE, username, {'token': token_res['access_token'],- 'valid_until': token_res['expires_in'] + datetime.datetime.now().timestamp(),+ 'valid_until': token_res['expires_in'] + dt.datetime.now().timestamp(),})self._API_HEADERS['Authorization'] = f'Bearer {token_res["access_token"]}'diff --git a/yt_dlp/extractor/goplay.py b/yt_dlp/extractor/goplay.pyindex 74aad11927..7a98e0f31c 100644--- a/yt_dlp/extractor/goplay.py+++ b/yt_dlp/extractor/goplay.py@@ -1,6 +1,6 @@import base64import binascii-import datetime+import datetime as dtimport hashlibimport hmacimport json@@ -422,7 +422,7 @@ class AwsIdp:months = [None, 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']- time_now = datetime.datetime.now(datetime.timezone.utc)+ time_now = dt.datetime.now(dt.timezone.utc)format_string = "{} {} {} %H:%M:%S UTC %Y".format(days[time_now.weekday()], months[time_now.month], time_now.day)time_string = time_now.strftime(format_string)return time_stringdiff --git a/yt_dlp/extractor/joqrag.py b/yt_dlp/extractor/joqrag.pyindex 3bb28af94e..c68ad8cb5f 100644--- a/yt_dlp/extractor/joqrag.py+++ b/yt_dlp/extractor/joqrag.py@@ -1,4 +1,4 @@-import datetime+import datetime as dtimport urllib.parsefrom .common import InfoExtractor@@ -50,8 +50,8 @@ class JoqrAgIE(InfoExtractor):def _extract_start_timestamp(self, video_id, is_live):def extract_start_time_from(date_str):- dt = datetime_from_str(date_str) + datetime.timedelta(hours=9)- date = dt.strftime('%Y%m%d')+ dt_ = datetime_from_str(date_str) + dt.timedelta(hours=9)+ date = dt_.strftime('%Y%m%d')start_time = self._search_regex(r'<h3[^>]+\bclass="dailyProgram-itemHeaderTime"[^>]*>[\s\d:]+–\s*(\d{1,2}:\d{1,2})',self._download_webpage(@@ -60,7 +60,7 @@ class JoqrAgIE(InfoExtractor):errnote=f'Failed to download program list of {date}') or '','start time', default=None)if start_time:- return unified_timestamp(f'{dt.strftime("%Y/%m/%d")} {start_time} +09:00')+ return unified_timestamp(f'{dt_.strftime("%Y/%m/%d")} {start_time} +09:00')return Nonestart_timestamp = extract_start_time_from('today')@@ -87,7 +87,7 @@ class JoqrAgIE(InfoExtractor):msg = 'This stream is not currently live'if release_timestamp:msg += (' and will start at '- + datetime.datetime.fromtimestamp(release_timestamp).strftime('%Y-%m-%d %H:%M:%S'))+ + dt.datetime.fromtimestamp(release_timestamp).strftime('%Y-%m-%d %H:%M:%S'))self.raise_no_formats(msg, expected=True)else:m3u8_path = self._search_regex(diff --git a/yt_dlp/extractor/leeco.py b/yt_dlp/extractor/leeco.pyindex 85033b8f8b..5d61a607f7 100644--- a/yt_dlp/extractor/leeco.py+++ b/yt_dlp/extractor/leeco.py@@ -1,4 +1,4 @@-import datetime+import datetime as dtimport hashlibimport reimport time@@ -185,7 +185,7 @@ class LeIE(InfoExtractor):publish_time = parse_iso8601(self._html_search_regex(r'发布时间 ([^<>]+) ', page, 'publish time', default=None),- delimiter=' ', timezone=datetime.timedelta(hours=8))+ delimiter=' ', timezone=dt.timedelta(hours=8))description = self._html_search_meta('description', page, fatal=False)return {diff --git a/yt_dlp/extractor/motherless.py b/yt_dlp/extractor/motherless.pyindex 160150a7b6..b6c18fe5bf 100644--- a/yt_dlp/extractor/motherless.py+++ b/yt_dlp/extractor/motherless.py@@ -1,4 +1,4 @@-import datetime+import datetime as dtimport reimport urllib.parse@@ -151,7 +151,7 @@ class MotherlessIE(InfoExtractor):'d': 'days',}kwargs = {_AGO_UNITS.get(uploaded_ago[-1]): delta}- upload_date = (datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(**kwargs)).strftime('%Y%m%d')+ upload_date = (dt.datetime.now(dt.timezone.utc) - dt.timedelta(**kwargs)).strftime('%Y%m%d')comment_count = len(re.findall(r'''class\s*=\s*['"]media-comment-contents\b''', webpage))uploader_id = self._html_search_regex(diff --git a/yt_dlp/extractor/niconico.py b/yt_dlp/extractor/niconico.pyindex 5da728fa16..b04ce96154 100644--- a/yt_dlp/extractor/niconico.py+++ b/yt_dlp/extractor/niconico.py@@ -1,4 +1,4 @@-import datetime+import datetime as dtimport functoolsimport itertoolsimport json@@ -819,12 +819,12 @@ class NicovideoSearchDateIE(NicovideoSearchBaseIE, SearchInfoExtractor):'playlist_mincount': 1610,}]- _START_DATE = datetime.date(2007, 1, 1)+ _START_DATE = dt.date(2007, 1, 1)_RESULTS_PER_PAGE = 32_MAX_PAGES = 50def _entries(self, url, item_id, start_date=None, end_date=None):- start_date, end_date = start_date or self._START_DATE, end_date or datetime.datetime.now().date()+ start_date, end_date = start_date or self._START_DATE, end_date or dt.datetime.now().date()# If the last page has a full page of videos, we need to break down the query interval furtherlast_page_len = len(list(self._get_entries_for_date(diff --git a/yt_dlp/extractor/panopto.py b/yt_dlp/extractor/panopto.pyindex 52e703e044..63c5fd68f1 100644--- a/yt_dlp/extractor/panopto.py+++ b/yt_dlp/extractor/panopto.py@@ -1,5 +1,5 @@import calendar-import datetime+import datetime as dtimport functoolsimport jsonimport random@@ -243,7 +243,7 @@ class PanoptoIE(PanoptoBaseIE):invocation_id = delivery_info.get('InvocationId')stream_id = traverse_obj(delivery_info, ('Delivery', 'Streams', ..., 'PublicID'), get_all=False, expected_type=str)if invocation_id and stream_id and duration:- timestamp_str = f'/Date({calendar.timegm(datetime.datetime.now(datetime.timezone.utc).timetuple())}000)/'+ timestamp_str = f'/Date({calendar.timegm(dt.datetime.now(dt.timezone.utc).timetuple())}000)/'data = {'streamRequests': [{diff --git a/yt_dlp/extractor/pr0gramm.py b/yt_dlp/extractor/pr0gramm.pyindex 6b2f57186f..3e0ccba174 100644--- a/yt_dlp/extractor/pr0gramm.py+++ b/yt_dlp/extractor/pr0gramm.py@@ -1,4 +1,4 @@-import datetime+import datetime as dtimport jsonimport urllib.parse@@ -197,7 +197,7 @@ class Pr0grammIE(InfoExtractor):'like_count': ('up', {int}),'dislike_count': ('down', {int}),'timestamp': ('created', {int}),- 'upload_date': ('created', {int}, {datetime.date.fromtimestamp}, {lambda x: x.strftime('%Y%m%d')}),+ 'upload_date': ('created', {int}, {dt.date.fromtimestamp}, {lambda x: x.strftime('%Y%m%d')}),'thumbnail': ('thumb', {lambda x: urljoin('https://thumb.pr0gramm.com', x)})}),}diff --git a/yt_dlp/extractor/rokfin.py b/yt_dlp/extractor/rokfin.pyindex 56bbccde40..3bc5f3cab2 100644--- a/yt_dlp/extractor/rokfin.py+++ b/yt_dlp/extractor/rokfin.py@@ -1,4 +1,4 @@-import datetime+import datetime as dtimport itertoolsimport jsonimport re@@ -156,7 +156,7 @@ class RokfinIE(InfoExtractor):self.raise_login_required('This video is only available to premium users', True, method='cookies')elif scheduled:self.raise_no_formats(- f'Stream is offline; scheduled for {datetime.datetime.fromtimestamp(scheduled).strftime("%Y-%m-%d %H:%M:%S")}',+ f'Stream is offline; scheduled for {dt.datetime.fromtimestamp(scheduled).strftime("%Y-%m-%d %H:%M:%S")}',video_id=video_id, expected=True)uploader = traverse_obj(metadata, ('createdBy', 'username'), ('creator', 'username'))diff --git a/yt_dlp/extractor/sejmpl.py b/yt_dlp/extractor/sejmpl.pyindex 29cb0152a2..eb433d2ac3 100644--- a/yt_dlp/extractor/sejmpl.py+++ b/yt_dlp/extractor/sejmpl.py@@ -1,4 +1,4 @@-import datetime+import datetime as dtfrom .common import InfoExtractorfrom .redge import RedCDNLivxIE@@ -13,16 +13,16 @@ from ..utils.traversal import traverse_objdef is_dst(date):- last_march = datetime.datetime(date.year, 3, 31)- last_october = datetime.datetime(date.year, 10, 31)- last_sunday_march = last_march - datetime.timedelta(days=last_march.isoweekday() % 7)- last_sunday_october = last_october - datetime.timedelta(days=last_october.isoweekday() % 7)+ last_march = dt.datetime(date.year, 3, 31)+ last_october = dt.datetime(date.year, 10, 31)+ last_sunday_march = last_march - dt.timedelta(days=last_march.isoweekday() % 7)+ last_sunday_october = last_october - dt.timedelta(days=last_october.isoweekday() % 7)return last_sunday_march.replace(hour=2) <= date <= last_sunday_october.replace(hour=3)def rfc3339_to_atende(date):- date = datetime.datetime.fromisoformat(date)- date = date + datetime.timedelta(hours=1 if is_dst(date) else 0)+ date = dt.datetime.fromisoformat(date)+ date = date + dt.timedelta(hours=1 if is_dst(date) else 0)return int((date.timestamp() - 978307200) * 1000)diff --git a/yt_dlp/extractor/sonyliv.py b/yt_dlp/extractor/sonyliv.pyindex a6da445250..7c914acbed 100644--- a/yt_dlp/extractor/sonyliv.py+++ b/yt_dlp/extractor/sonyliv.py@@ -1,4 +1,4 @@-import datetime+import datetime as dtimport itertoolsimport jsonimport math@@ -94,7 +94,7 @@ class SonyLIVIE(InfoExtractor):'mobileNumber': username,'channelPartnerID': 'MSMIND','country': 'IN',- 'timestamp': datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%MZ'),+ 'timestamp': dt.datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%MZ'),'otpSize': 6,'loginType': 'REGISTERORSIGNIN','isMobileMandatory': True,@@ -111,7 +111,7 @@ class SonyLIVIE(InfoExtractor):'otp': self._get_tfa_info('OTP'),'dmaId': 'IN','ageConfirmation': True,- 'timestamp': datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%MZ'),+ 'timestamp': dt.datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%MZ'),'isMobileMandatory': True,}).encode())if otp_verify_json['resultCode'] == 'KO':diff --git a/yt_dlp/extractor/tenplay.py b/yt_dlp/extractor/tenplay.pyindex ea4041976a..11cc5705e9 100644--- a/yt_dlp/extractor/tenplay.py+++ b/yt_dlp/extractor/tenplay.py@@ -1,5 +1,5 @@import base64-import datetime+import datetime as dtimport functoolsimport itertools@@ -70,7 +70,7 @@ class TenPlayIE(InfoExtractor):username, password = self._get_login_info()if username is None or password is None:self.raise_login_required('Your 10play account\'s details must be provided with --username and --password.')- _timestamp = datetime.datetime.now().strftime('%Y%m%d000000')+ _timestamp = dt.datetime.now().strftime('%Y%m%d000000')_auth_header = base64.b64encode(_timestamp.encode('ascii')).decode('ascii')data = self._download_json('https://10play.com.au/api/user/auth', video_id, 'Getting bearer token', headers={'X-Network-Ten-Auth': _auth_header,diff --git a/yt_dlp/extractor/youtube.py b/yt_dlp/extractor/youtube.pyindex 1f1db1ad31..e553fff9f1 100644--- a/yt_dlp/extractor/youtube.py+++ b/yt_dlp/extractor/youtube.py@@ -2,7 +2,7 @@ import base64import calendarimport collectionsimport copy-import datetime+import datetime as dtimport enumimport hashlibimport itertools@@ -924,10 +924,10 @@ class YoutubeBaseInfoExtractor(InfoExtractor):def _parse_time_text(self, text):if not text:return- dt = self.extract_relative_time(text)+ dt_ = self.extract_relative_time(text)timestamp = None- if isinstance(dt, datetime.datetime):- timestamp = calendar.timegm(dt.timetuple())+ if isinstance(dt_, dt.datetime):+ timestamp = calendar.timegm(dt_.timetuple())if timestamp is None:timestamp = (@@ -4568,7 +4568,7 @@ class YoutubeIE(YoutubeBaseInfoExtractor):if upload_date and live_status not in ('is_live', 'post_live', 'is_upcoming'):# Newly uploaded videos' HLS formats are potentially problematic and need to be checked- upload_datetime = datetime_from_str(upload_date).replace(tzinfo=datetime.timezone.utc)+ upload_datetime = datetime_from_str(upload_date).replace(tzinfo=dt.timezone.utc)if upload_datetime >= datetime_from_str('today-2days'):for fmt in info['formats']:if fmt.get('protocol') == 'm3u8_native':diff --git a/yt_dlp/utils/_utils.py b/yt_dlp/utils/_utils.pyindex 648cf0abd5..dec514674f 100644--- a/yt_dlp/utils/_utils.py+++ b/yt_dlp/utils/_utils.py@@ -5,7 +5,7 @@ import codecsimport collectionsimport collections.abcimport contextlib-import datetime+import datetime as dtimport email.headerimport email.utilsimport errno@@ -1150,14 +1150,14 @@ def extract_timezone(date_str):timezone = TIMEZONE_NAMES.get(m and m.group('tz').strip())if timezone is not None:date_str = date_str[:-len(m.group('tz'))]- timezone = datetime.timedelta(hours=timezone or 0)+ timezone = dt.timedelta(hours=timezone or 0)else:date_str = date_str[:-len(m.group('tz'))]if not m.group('sign'):- timezone = datetime.timedelta()+ timezone = dt.timedelta()else:sign = 1 if m.group('sign') == '+' else -1- timezone = datetime.timedelta(+ timezone = dt.timedelta(hours=sign * int(m.group('hours')),minutes=sign * int(m.group('minutes')))return timezone, date_str@@ -1176,8 +1176,8 @@ def parse_iso8601(date_str, delimiter='T', timezone=None):with contextlib.suppress(ValueError):date_format = f'%Y-%m-%d{delimiter}%H:%M:%S'- dt = datetime.datetime.strptime(date_str, date_format) - timezone- return calendar.timegm(dt.timetuple())+ dt_ = dt.datetime.strptime(date_str, date_format) - timezone+ return calendar.timegm(dt_.timetuple())def date_formats(day_first=True):@@ -1198,12 +1198,12 @@ def unified_strdate(date_str, day_first=True):for expression in date_formats(day_first):with contextlib.suppress(ValueError):- upload_date = datetime.datetime.strptime(date_str, expression).strftime('%Y%m%d')+ upload_date = dt.datetime.strptime(date_str, expression).strftime('%Y%m%d')if upload_date is None:timetuple = email.utils.parsedate_tz(date_str)if timetuple:with contextlib.suppress(ValueError):- upload_date = datetime.datetime(*timetuple[:6]).strftime('%Y%m%d')+ upload_date = dt.datetime(*timetuple[:6]).strftime('%Y%m%d')if upload_date is not None:return str(upload_date)@@ -1233,8 +1233,8 @@ 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())+ dt_ = dt.datetime.strptime(date_str, expression) - timezone + dt.timedelta(hours=pm_delta)+ return calendar.timegm(dt_.timetuple())timetuple = email.utils.parsedate_tz(date_str)if timetuple:@@ -1272,11 +1272,11 @@ def datetime_from_str(date_str, precision='auto', format='%Y%m%d'):if precision == 'auto':auto_precision = Trueprecision = 'microsecond'- today = datetime_round(datetime.datetime.now(datetime.timezone.utc), precision)+ today = datetime_round(dt.datetime.now(dt.timezone.utc), precision)if date_str in ('now', 'today'):return todayif date_str == 'yesterday':- return today - datetime.timedelta(days=1)+ return today - dt.timedelta(days=1)match = re.match(r'(?P<start>.+)(?P<sign>[+-])(?P<time>\d+)(?P<unit>microsecond|second|minute|hour|day|week|month|year)s?',date_str)@@ -1291,13 +1291,13 @@ def datetime_from_str(date_str, precision='auto', format='%Y%m%d'):if unit == 'week':unit = 'day'time *= 7- delta = datetime.timedelta(**{unit + 's': time})+ delta = dt.timedelta(**{unit + 's': time})new_date = start_time + deltaif auto_precision:return datetime_round(new_date, unit)return new_date- return datetime_round(datetime.datetime.strptime(date_str, format), precision)+ return datetime_round(dt.datetime.strptime(date_str, format), precision)def date_from_str(date_str, format='%Y%m%d', strict=False):@@ -1312,21 +1312,21 @@ def date_from_str(date_str, format='%Y%m%d', strict=False):return datetime_from_str(date_str, precision='microsecond', format=format).date()-def datetime_add_months(dt, months):+def datetime_add_months(dt_, months):"""Increment/Decrement a datetime object by months."""- month = dt.month + months - 1- year = dt.year + month // 12+ month = dt_.month + months - 1+ year = dt_.year + month // 12month = month % 12 + 1- day = min(dt.day, calendar.monthrange(year, month)[1])- return dt.replace(year, month, day)+ day = min(dt_.day, calendar.monthrange(year, month)[1])+ return dt_.replace(year, month, day)-def datetime_round(dt, precision='day'):+def datetime_round(dt_, precision='day'):"""Round a datetime object's time to a specific precision"""if precision == 'microsecond':- return dt+ return dt_unit_seconds = {'day': 86400,@@ -1335,8 +1335,8 @@ def datetime_round(dt, precision='day'):'second': 1,}roundto = lambda x, n: ((x + n / 2) // n) * n- timestamp = roundto(calendar.timegm(dt.timetuple()), unit_seconds[precision])- return datetime.datetime.fromtimestamp(timestamp, datetime.timezone.utc)+ timestamp = roundto(calendar.timegm(dt_.timetuple()), unit_seconds[precision])+ return dt.datetime.fromtimestamp(timestamp, dt.timezone.utc)def hyphenate_date(date_str):@@ -1357,11 +1357,11 @@ class DateRange:if start is not None:self.start = date_from_str(start, strict=True)else:- self.start = datetime.datetime.min.date()+ self.start = dt.datetime.min.date()if end is not None:self.end = date_from_str(end, strict=True)else:- self.end = datetime.datetime.max.date()+ self.end = dt.datetime.max.date()if self.start > self.end:raise ValueError('Date range: "%s" , the start date must be before the end date' % self)@@ -1372,7 +1372,7 @@ class DateRange:def __contains__(self, date):"""Check if the date is in the range"""- if not isinstance(date, datetime.date):+ if not isinstance(date, dt.date):date = date_from_str(date)return self.start <= date <= self.end@@ -1996,12 +1996,12 @@ def strftime_or_none(timestamp, date_format='%Y%m%d', 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- # Also, datetime.datetime.fromtimestamp breaks for negative timestamps+ # Also, dt.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))+ datetime_object = (dt.datetime.fromtimestamp(0, dt.timezone.utc)+ + dt.timedelta(seconds=timestamp))elif isinstance(timestamp, str): # assume YYYYMMDD- datetime_object = datetime.datetime.strptime(timestamp, '%Y%m%d')+ datetime_object = dt.datetime.strptime(timestamp, '%Y%m%d')date_format = re.sub( # Support %s on windowsr'(?<!%)(%%)*%s', rf'\g<1>{int(datetime_object.timestamp())}', date_format)return datetime_object.strftime(date_format)@@ -4490,10 +4490,10 @@ def write_xattr(path, key, value):def random_birthday(year_field, month_field, day_field):- start_date = datetime.date(1950, 1, 1)- end_date = datetime.date(1995, 12, 31)+ start_date = dt.date(1950, 1, 1)+ end_date = dt.date(1995, 12, 31)offset = random.randint(0, (end_date - start_date).days)- random_date = start_date + datetime.timedelta(offset)+ random_date = start_date + dt.timedelta(offset)return {year_field: str(random_date.year),month_field: str(random_date.month),@@ -4672,7 +4672,7 @@ def time_seconds(**kwargs):"""Returns TZ-aware time in seconds since the epoch (1970-01-01T00:00:00Z)"""- return time.time() + datetime.timedelta(**kwargs).total_seconds()+ return time.time() + dt.timedelta(**kwargs).total_seconds()# create a JSON Web Signature (jws) with HS256 algorithm