Enable flake8-datetimez linting rules (#267)

See https://docs.astral.sh/ruff/rules/#flake8-datetimez-dtz.
This commit is contained in:
Marc Wrobel
2023-12-30 11:20:51 +01:00
parent 54e7091fd2
commit 798c298c59
10 changed files with 34 additions and 26 deletions

View File

@@ -44,10 +44,20 @@ def parse_datetime(text: str, formats: list[str] = frozenset([
text = text.strip().replace(", ", " ").replace(". ", " ").replace("(", "").replace(")", "")
for fmt in formats:
try:
date = datetime.strptime(text, fmt)
date = datetime.strptime(text, fmt) # NOQA: DTZ007, timezone is handled below
date = date.astimezone(timezone.utc) if to_utc else date
return date
except ValueError:
pass
raise ValueError(f"'{text}' could not be parsed as a date with any of the formats: {str(formats)}")
def date(year: int, month: int, day: int) -> datetime:
"""Create a datetime object with the given year, month and day, at midnight."""
return datetime(year, month, day, tzinfo=timezone.utc)
def today() -> datetime:
"""Create a datetime object with today's date, at midnight."""
return datetime.now(tz=timezone.utc).replace(hour=0, minute=0, second=0, microsecond=0)

View File

@@ -2,7 +2,7 @@ import json
import logging
import os
import re
from datetime import datetime
from datetime import datetime, timezone
from glob import glob
import frontmatter
@@ -84,7 +84,8 @@ class Product:
if not os.path.isfile(product.versions_path):
with open(product.versions_path) as f:
for version, date in json.load(f).items():
product.versions[version] = datetime.strptime(date, "%Y-%m-%d")
date_obj = datetime.strptime(date, "%Y-%m-%d").replace(tzinfo=timezone.utc)
product.versions[version] = date_obj
logging.info(f"loaded versions data for {product.name} from {product.versions_path}")
else:
logging.warning(f"no versions data found for {product.name} at {product.versions_path}")