Fix --date today

Closes #3704
This commit is contained in:
pukkandan 2022-05-11 05:52:31 +05:30
parent 3a408f9d19
commit 3d38b2d6d0
No known key found for this signature in database
GPG key ID: 7EEE9E1E817D0A39
3 changed files with 18 additions and 20 deletions

View file

@ -427,7 +427,8 @@ ## Video Selection:
(e.g. 50k or 44.6m) (e.g. 50k or 44.6m)
--date DATE Download only videos uploaded on this date. --date DATE Download only videos uploaded on this date.
The date can be "YYYYMMDD" or in the format The date can be "YYYYMMDD" or in the format
"(now|today)[+-][0-9](day|week|month|year)(s)?" [now|today|yesterday][-N[day|week|month|year]].
Eg: --date today-2weeks
--datebefore DATE Download only videos uploaded on or before --datebefore DATE Download only videos uploaded on or before
this date. The date formats accepted is the this date. The date formats accepted is the
same as --date same as --date

View file

@ -435,9 +435,8 @@ def _dict_from_options_callback(
'--date', '--date',
metavar='DATE', dest='date', default=None, metavar='DATE', dest='date', default=None,
help=( help=(
'Download only videos uploaded on this date. ' 'Download only videos uploaded on this date. The date can be "YYYYMMDD" or in the format '
'The date can be "YYYYMMDD" or in the format ' '[now|today|yesterday][-N[day|week|month|year]]. Eg: --date today-2weeks'))
'"(now|today)[+-][0-9](day|week|month|year)(s)?"'))
selection.add_option( selection.add_option(
'--datebefore', '--datebefore',
metavar='DATE', dest='datebefore', default=None, metavar='DATE', dest='datebefore', default=None,

View file

@ -1756,13 +1756,13 @@ def subtitles_filename(filename, sub_lang, sub_format, expected_real_ext=None):
def datetime_from_str(date_str, precision='auto', format='%Y%m%d'): def datetime_from_str(date_str, precision='auto', format='%Y%m%d'):
""" R"""
Return a datetime object from a string in the format YYYYMMDD or Return a datetime object from a string.
(now|today|yesterday|date)[+-][0-9](microsecond|second|minute|hour|day|week|month|year)(s)? Supported format:
(now|today|yesterday|DATE)([+-]\d+(microsecond|second|minute|hour|day|week|month|year)s?)?
format: string date format used to return datetime object from @param format strftime format of DATE
precision: round the time portion of a datetime object. @param precision Round the datetime object: auto|microsecond|second|minute|hour|day
auto|microsecond|second|minute|hour|day.
auto: round to the unit provided in date_str (if applicable). auto: round to the unit provided in date_str (if applicable).
""" """
auto_precision = False auto_precision = False
@ -1775,7 +1775,7 @@ def datetime_from_str(date_str, precision='auto', format='%Y%m%d'):
if date_str == 'yesterday': if date_str == 'yesterday':
return today - datetime.timedelta(days=1) return today - datetime.timedelta(days=1)
match = re.match( match = re.match(
r'(?P<start>.+)(?P<sign>[+-])(?P<time>\d+)(?P<unit>microsecond|second|minute|hour|day|week|month|year)(s)?', r'(?P<start>.+)(?P<sign>[+-])(?P<time>\d+)(?P<unit>microsecond|second|minute|hour|day|week|month|year)s?',
date_str) date_str)
if match is not None: 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)
@ -1798,16 +1798,14 @@ def datetime_from_str(date_str, precision='auto', format='%Y%m%d'):
def date_from_str(date_str, format='%Y%m%d', strict=False): def date_from_str(date_str, format='%Y%m%d', strict=False):
""" R"""
Return a datetime object from a string in the format YYYYMMDD or Return a date object from a string using datetime_from_str
(now|today|yesterday|date)[+-][0-9](microsecond|second|minute|hour|day|week|month|year)(s)?
If "strict", only (now|today)[+-][0-9](day|week|month|year)(s)? is allowed @param strict Restrict allowed patterns to "YYYYMMDD" and
(now|today|yesterday)(-\d+(day|week|month|year)s?)?
format: string date format used to return datetime object from
""" """
if strict and not re.fullmatch(r'\d{8}|(now|today)[+-]\d+(day|week|month|year)(s)?', date_str): if strict and not re.fullmatch(r'\d{8}|(now|today|yesterday)(-\d+(day|week|month|year)s?)?', date_str):
raise ValueError(f'Invalid date format {date_str}') raise ValueError(f'Invalid date format "{date_str}"')
return datetime_from_str(date_str, precision='microsecond', format=format).date() return datetime_from_str(date_str, precision='microsecond', format=format).date()