Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/flightforms/fillers/_datetime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""Date/time conversion shared by the form fillers.

Requests always carry UTC. A form that prints the airport's wall clock
("Heure Locale") declares ``time_reference: "local"`` and a ``time_zone``
in its mapping.
"""

from datetime import datetime
from zoneinfo import ZoneInfo


def utc_to_local(date_str: str, time_str: str, tz_name: str) -> tuple[str, str]:
"""Convert a YYYY-MM-DD / HH:MM pair from UTC to *tz_name*.

Date and time must convert together: a UTC evening can land on the next
day locally (23:50Z on 1 June is 01:50 on 2 June in Paris), so printing a
converted time against the original UTC date would misdate the flight by
a day on a customs pre-notification.

The date comes back as YYYY-MM-DD, for the caller to format.
"""
dt = datetime.strptime(f"{date_str} {time_str}", "%Y-%m-%d %H:%M")
local = dt.replace(tzinfo=ZoneInfo("UTC")).astimezone(ZoneInfo(tz_name))
return local.strftime("%Y-%m-%d"), local.strftime("%H:%M")
29 changes: 13 additions & 16 deletions src/flightforms/fillers/french_customs_filler.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,19 @@
from datetime import datetime
from io import BytesIO
from pathlib import Path
from zoneinfo import ZoneInfo

from pypdf import PdfReader, PdfWriter

from ..api.models import GenerateRequest
from ..registry import FormMapping
from ._datetime import utc_to_local


def _parse_date(date_str: str, fmt: str) -> str:
dt = datetime.strptime(date_str, "%Y-%m-%d")
return dt.strftime(fmt)


def _utc_to_local(time_str: str, date_str: str, tz_name: str) -> str:
"""Convert HH:MM UTC to local time in the given timezone."""
dt = datetime.strptime(f"{date_str} {time_str}", "%Y-%m-%d %H:%M")
dt_utc = dt.replace(tzinfo=ZoneInfo("UTC"))
dt_local = dt_utc.astimezone(ZoneInfo(tz_name))
return dt_local.strftime("%H:%M")


def _suffix(index: int) -> str:
"""Row 0 = no suffix, row 1 = '_2', row 2 = '_3', etc."""
if index == 0:
Expand All @@ -51,15 +43,20 @@ def fill_french_customs(

observations = request.observations or mapping.default_observations or ""

# Use local time conversion only when time_reference is "local"
# Use local time conversion only when time_reference is "local". The date
# converts with the time: a late-evening UTC slot falls on the next day
# locally, and the form would otherwise print that local time against the
# UTC date.
if mapping.time_reference == "local" and mapping.time_zone:
dep_time = _utc_to_local(
request.flight.departure_time_utc, request.flight.departure_date, mapping.time_zone
dep_date, dep_time = utc_to_local(
request.flight.departure_date, request.flight.departure_time_utc, mapping.time_zone
)
arr_time = _utc_to_local(
request.flight.arrival_time_utc, request.flight.arrival_date, mapping.time_zone
arr_date, arr_time = utc_to_local(
request.flight.arrival_date, request.flight.arrival_time_utc, mapping.time_zone
)
else:
dep_date = request.flight.departure_date
arr_date = request.flight.arrival_date
dep_time = request.flight.departure_time_utc
arr_time = request.flight.arrival_time_utc

Expand All @@ -78,8 +75,8 @@ def fill_french_customs(
"destination": request.flight.destination,
"registration": request.aircraft.registration,
"aircraft_type": request.aircraft.type,
"departure_date": _parse_date(request.flight.departure_date, mapping.date_format),
"arrival_date": _parse_date(request.flight.arrival_date, mapping.date_format),
"departure_date": _parse_date(dep_date, mapping.date_format),
"arrival_date": _parse_date(arr_date, mapping.date_format),
"departure_time": dep_time,
"arrival_time": arr_time,
"contact": contact,
Expand Down
Loading
Loading