def cli_date(date_string: str) -> datetime: '''Parse a CLI date or number of days into a TZ-aware datetime.''' try: days = float(date_string) if days <= 0: raise click.BadParameter('must be a positive number or date') return datetime.now().astimezone() - timedelta(days=days) except ValueError: pass try: value = parse_datetime(date_string) except Exception: raise click.BadParameter(f'"{date_string}" is not a date') if value >= datetime.now().astimezone(): raise click.BadParameter('must be a date in the past.') return value
def test_iso8601_dates(self) -> None: result = parse_datetime('2021-09-10T00:03:18Z') assert result == datetime(2021, 9, 10, 0, 3, 18, tzinfo=timezone.utc)
def test_does_not_corrects_century_based_on_args(self) -> None: with pytest.raises(ValueError): parse_datetime('1921-09-10T00:00:00Z', correct_century=False)
def test_corrects_century(self) -> None: result = parse_datetime('1921-09-10T00:00:00Z') assert result == datetime(2021, 9, 10, tzinfo=timezone.utc)
def test_raises_for_unlikely_dates(self) -> None: with pytest.raises(ValueError): parse_datetime('2010-09-10T00:00:00Z') with pytest.raises(ValueError): parse_datetime('2030-09-10T00:00:00Z')
def test_default_timezone_is_ignored_if_date_specifies_one(self) -> None: default_tz = dateutil.tz.gettz('America/Chicago') result = parse_datetime('2021-09-10T00:03:18Z', timezone=default_tz) result_tz = result.tzinfo assert result_tz is not None assert result_tz.utcoffset(result) == timezone.utc.utcoffset(result)
def test_default_timezone_is_used(self) -> None: default_tz = dateutil.tz.gettz('America/Chicago') result = parse_datetime('2021-09-10T00:03:18', timezone=default_tz) assert result.tzinfo is default_tz
def test_always_sets_a_timezone(self) -> None: result = parse_datetime('2021-09-10T00:03:18') assert result.tzinfo is not None
def most_recent_case_time(data: Dict[str, Any]) -> datetime: most_recent_cases = cast(Dict[str, str], dig(data, ['series', 'cases', -1])) return parse_datetime(most_recent_cases['date'])