Skip to content

Commit

Permalink
Fix timezone shenanigans
Browse files Browse the repository at this point in the history
Passing %Z to strptime does not result in a datetime with a timezone
set. This doesn't make sense at all but the random email function does
the right thing.
  • Loading branch information
jbruechert committed Mar 1, 2024
1 parent 6b32a7e commit c31b4d6
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

from metadata import *
from pathlib import Path
from datetime import datetime
from datetime import datetime, timezone
import email.utils

import requests
import transitland
Expand Down Expand Up @@ -39,11 +40,12 @@ def fetch_source(self, name: str, source: Source) -> Optional[Path]:
dest_path = Path(f"downloads/{name}.gtfs.zip")
if dest_path.exists():
mtime = dest_path.stat().st_mtime
last_modified = datetime.fromtimestamp(mtime)
last_modified = datetime.fromtimestamp(mtime,
tz=timezone.utc)

# Check if the last download was longer than the interval ago
if source.options.fetch_interval_days and last_modified \
and (datetime.now() - last_modified).days \
and (datetime.now(timezone.utc) - last_modified).days \
< source.options.fetch_interval_days:
return None

Expand All @@ -54,8 +56,8 @@ def fetch_source(self, name: str, source: Source) -> Optional[Path]:
# If server version is older, return
last_modified_server = None
if "last-modified" in server_headers:
last_modified_server = datetime.strptime(
server_headers["last-modified"], "%a, %d %b %Y %X %Z")
last_modified_server = email.utils.parsedate_to_datetime(
server_headers["last-modified"])

if last_modified and last_modified_server <= last_modified:
return None
Expand Down Expand Up @@ -92,8 +94,8 @@ def fetch_source(self, name: str, source: Source) -> Optional[Path]:
# but the actual target does.
server_headers = response.headers
if "last-modified" in server_headers:
last_modified_server = datetime.strptime(
server_headers["last-modified"], "%a, %d %b %Y %X %Z")
last_modified_server = email.utils.parsedate_to_datetime(
server_headers["last-modified"])

with open(dest_path, "wb") as dest:
dest.write(response.content)
Expand Down

0 comments on commit c31b4d6

Please sign in to comment.