Skip to content

Commit 9796e4e

Browse files
committed
last fixes on waypoint_route of Google Maps
1 parent 92be68d commit 9796e4e

2 files changed

Lines changed: 15 additions & 10 deletions

File tree

navigator/actions/google/maps.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -363,16 +363,19 @@ async def waypoint_route(
363363
# Google Maps API requires departure_time to be in seconds since the epoch
364364
# if the departure time is in the past, we need to set it to tomorrow
365365
now = datetime.datetime.now(tz=timezone.utc)
366+
tomorrow = now + datetime.timedelta(days=1)
366367
departure_time = self._compute_departure(payload.departure_time)
367368
# if the departure time is in the past, we need to set it to tomorrow
368369
if departure_time < now:
369370
# Traffic information is only available for future and current times
370-
tomorrow = now + datetime.timedelta(days=1)
371-
payload.departure_time = departure_time.replace(
372-
year=tomorrow.year,
373-
# month=tomorrow.month,
374-
# day=tomorrow.day
375-
)
371+
# Try setting the year to the current year
372+
adjusted_dt_utc = departure_time.replace(year=tomorrow.year)
373+
# If it's still in the past
374+
# (e.g., user_dt was Jan 1, 2024,
375+
# now is May 30, 2025 -> adjusted_dt becomes Jan 1, 2025, which is still past)
376+
if adjusted_dt_utc < now:
377+
adjusted_dt_utc = adjusted_dt_utc.replace(year=tomorrow.year + 1)
378+
departure_time = adjusted_dt_utc
376379
# we need to convert the departure time to a timestamp
377380
# in seconds since the epoch
378381
departure_time = departure_time.strftime('%Y-%m-%dT%H:%M:%SZ')
@@ -398,7 +401,7 @@ async def waypoint_route(
398401

399402
if departure_time is not None:
400403
data['departureTime'] = departure_time
401-
404+
402405
if payload.locations:
403406
data['intermediates'] = [loc.get_location() for loc in payload.locations]
404407
if payload.optimal is True:
@@ -480,12 +483,14 @@ async def waypoint_route(
480483
instruction = first_step['navigationInstruction'].get('instructions', '')
481484
# Get localized distance for this leg
482485
leg_distance = leg.get(
483-
'localizedValues', {}).get('distance', {}).get('text', f"{leg['distanceMeters']/1609.34:.1f} mi")
486+
'localizedValues', {}
487+
).get('distance', {}).get('text', f"{leg['distanceMeters']/1609.34:.1f} mi") or f"{distance_meters/1609.34:.1f} mi" # noqa: E501
484488
bestroute.append(f"Leg {i+1}: {instruction} for {leg_distance}")
485489
else:
486490
# Fallback if no navigation instruction
487491
leg_distance = leg.get(
488-
'localizedValues', {}).get('distance', {}).get('text', f"{leg['distanceMeters']/1609.34:.1f} mi")
492+
'localizedValues', {}
493+
).get('distance', {}).get('text', f"{leg['distanceMeters']/1609.34:.1f} mi") or f"{distance_meters/1609.34:.1f} mi" # noqa: E501
489494
bestroute.append(f"Leg {i+1}: Continue for {leg_distance}")
490495
# Convert duration to minutes
491496
total_duration_min = total_duration / 60 if total_duration > 0 else 0

navigator/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
__description__ = (
55
"Navigator Web Framework based on aiohttp, " "with batteries included."
66
)
7-
__version__ = "2.12.25"
7+
__version__ = "2.12.26"
88
__copyright__ = "Copyright (c) 2020-2024 Jesus Lara"
99
__author__ = "Jesus Lara"
1010
__author_email__ = "jesuslarag@gmail.com"

0 commit comments

Comments
 (0)