[utils] Allow using local timezone for 'now' timestamps

This commit is contained in:
Elyse 2023-03-09 17:18:44 -06:00
parent 367429e238
commit 1799a6ae36

View file

@ -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:
@ -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':
@ -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':