Python航班夜间时段判定功能:现有代码问题排查
Fixing the Night Flight Detection Logic in Python
Hey there! Let's work through fixing your night flight detection code. First, let's recap the core requirement clearly:
A flight is considered a night flight if its departure-to-arrival window overlaps at all with the 00:00–02:59 time slot (on any day).
Your test cases are perfect for validating this logic:
- Test 1:
2018-01-16 18:30 → 2018-01-16 20:25→ Not a night flight (no overlap with 00:00–02:59) - Test 2:
2018-01-16 21:35 → 2018-01-17 01:15→ Night flight (overlaps with 2018-01-17's 00:00–01:15) - Test 3:
2018-01-17 23:30 → 2018-01-18 03:25→ Night flight (overlaps with 2018-01-18's 00:00–02:59)
Common Mistakes in Broken Code
Most bugs here stem from not accounting for cross-day flights or treating departure/arrival times in isolation instead of as a continuous window:
- Mistake 1: Only checking if the arrival time falls between 00:00–02:59 (ignores flights that start before midnight and overlap into the night slot)
- Mistake 2: Failing to handle date boundaries (e.g., treating 23:30 on day 1 and 01:15 on day 2 as unrelated times)
- Mistake 3: Comparing raw time strings instead of using
datetimeobjects (leads to messy, error-prone string parsing)
Correct Implementation
Here's a robust solution that handles all edge cases, including same-day and cross-day flights:
from datetime import datetime, timedelta def is_night_flight(departure_str, arrival_str): # Convert input strings to datetime objects for easy time comparison departure = datetime.strptime(departure_str, "%Y-%m-%d %H:%M") arrival = datetime.strptime(arrival_str, "%Y-%m-%d %H:%M") # Define the night window as a timedelta (00:00 to 03:00, left-closed right-open) night_start = timedelta(hours=0) night_end = timedelta(hours=3) # Case 1: Flight operates within a single day if departure.date() == arrival.date(): # Create datetime objects for the night window on that specific day day_night_start = datetime.combine(departure.date(), datetime.min.time()) + night_start day_night_end = datetime.combine(departure.date(), datetime.min.time()) + night_end # Check for overlap: flight ends after night starts, and flight starts before night ends return not (arrival <= day_night_start or departure >= day_night_end) # Case 2: Flight crosses midnight (spans two consecutive days) else: # Check overlap with the night window on the departure day dep_night_start = datetime.combine(departure.date(), datetime.min.time()) + night_start dep_night_end = datetime.combine(departure.date(), datetime.min.time()) + night_end overlap_dep_day = not (departure >= dep_night_end or arrival <= dep_night_start) # Check overlap with the night window on the arrival day arr_night_start = datetime.combine(arrival.date(), datetime.min.time()) + night_start arr_night_end = datetime.combine(arrival.date(), datetime.min.time()) + night_end overlap_arr_day = not (departure >= arr_night_end or arrival <= arr_night_start) # If either day has an overlap, it qualifies as a night flight return overlap_dep_day or overlap_arr_day
Validate with Your Test Cases
Let's run the code against your test data to confirm it works:
# Test 1: Non-night flight print(is_night_flight("2018-01-16 18:30", "2018-01-16 20:25")) # Output: False # Test 2: Cross-day night flight print(is_night_flight("2018-01-16 21:35", "2018-01-17 01:15")) # Output: True # Test 3: Cross-day flight overlapping the full night window print(is_night_flight("2018-01-17 23:30", "2018-01-18 03:25")) # Output: True
How This Logic Works
- We use
datetimeobjects to handle date and time as a single, comparable value (no messy string parsing) - For same-day flights, we check if the flight window overlaps with the day's 00:00–03:00 slot
- For cross-day flights, we check both the departure day's night slot and the arrival day's night slot for any overlap
- The overlap check follows a simple rule: two time ranges overlap if neither ends before the other starts
内容的提问来源于stack exchange,提问作者Ward
相关产品推荐
相关产品推荐

