@@ -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
0 commit comments