Skip to content

Commit

Permalink
[utils] Allow using local timezone for 'now' timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
elyse0 committed Mar 9, 2023
1 parent 367429e commit 1799a6a
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions yt_dlp/utils.py
Expand Up @@ -1893,7 +1893,7 @@ def subtitles_filename(filename, sub_lang, sub_format, expected_real_ext=None):
return replace_extension(filename, sub_lang + '.' + sub_format, expected_real_ext)


def datetime_from_str(date_str, precision='auto', format='%Y%m%d'):
def datetime_from_str(date_str, precision='auto', format='%Y%m%d', use_utc=True):
R"""
Return a datetime object from a string.
Supported format:
Expand All @@ -1902,12 +1902,13 @@ def datetime_from_str(date_str, precision='auto', format='%Y%m%d'):
@param format strftime format of DATE
@param precision Round the datetime object: auto|microsecond|second|minute|hour|day
auto: round to the unit provided in date_str (if applicable).
@param use_utc Use UTC instead of local timezone for 'now' timestamps.
"""
auto_precision = False
if precision == 'auto':
auto_precision = True
precision = 'microsecond'
today = datetime_round(datetime.datetime.utcnow(), precision)
today = datetime_round(datetime.datetime.utcnow() if use_utc else datetime.datetime.now(), precision)
if date_str in ('now', 'today'):
return today
if date_str == 'yesterday':
Expand All @@ -1916,7 +1917,7 @@ def datetime_from_str(date_str, precision='auto', format='%Y%m%d'):
r'(?P<start>.+)(?P<sign>[+-])(?P<time>\d+)(?P<unit>microsecond|second|minute|hour|day|week|month|year)s?',
date_str)
if match is not None:
start_time = datetime_from_str(match.group('start'), precision, format)
start_time = datetime_from_str(match.group('start'), precision, format, use_utc)
time = int(match.group('time')) * (-1 if match.group('sign') == '-' else 1)
unit = match.group('unit')
if unit == 'month' or unit == 'year':
Expand Down

0 comments on commit 1799a6a

Please sign in to comment.