42Cummer commited on
Commit
e780e98
·
verified ·
1 Parent(s): adde0f1

Upload utils.py

Browse files
Files changed (1) hide show
  1. api/utils.py +42 -11
api/utils.py CHANGED
@@ -1,4 +1,4 @@
1
- from datetime import datetime, date, time, timedelta
2
 
3
  def hms_to_seconds(hms_str):
4
  """Converts GTFS 'HH:MM:SS' (e.g. '25:30:00') to total seconds from midnight."""
@@ -7,19 +7,50 @@ def hms_to_seconds(hms_str):
7
 
8
  def get_service_day_start_ts():
9
  """
10
- Returns the Unix timestamp for 00:00:00 of the CURRENT service day.
11
- TTC service day typically flips at 4:00 AM.
 
12
  """
13
- now = datetime.now()
14
- # If it's 2 AM, we are still technically on 'yesterday's' schedule
15
- if now.hour < 4:
16
- service_date = date.today() - timedelta(days=1)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  else:
18
- service_date = date.today()
 
 
 
 
 
 
 
 
19
 
20
- # Combine that date with 00:00:00 and get the timestamp
21
- service_start_dt = datetime.combine(service_date, time.min)
22
- return int(service_start_dt.timestamp())
23
 
24
  def translate_occupancy(status):
25
  """Maps GTFS occupancy enums to human readable strings."""
 
1
+ from datetime import datetime, time, timedelta
2
 
3
  def hms_to_seconds(hms_str):
4
  """Converts GTFS 'HH:MM:SS' (e.g. '25:30:00') to total seconds from midnight."""
 
7
 
8
  def get_service_day_start_ts():
9
  """
10
+ Returns the Unix timestamp (UTC) for 00:00:00 of the CURRENT service day.
11
+ TTC service day flips at 4:00 AM Eastern time (handles DST automatically).
12
+ All timestamps are in UTC to match GTFS Realtime timestamps.
13
  """
14
+ from datetime import timezone
15
+ try:
16
+ # Python 3.9+ has zoneinfo built-in
17
+ from zoneinfo import ZoneInfo
18
+ eastern_tz = ZoneInfo("America/Toronto")
19
+ except ImportError:
20
+ # Fallback for older Python versions - use pytz if available
21
+ try:
22
+ import pytz
23
+ eastern_tz = pytz.timezone("America/Toronto")
24
+ except ImportError:
25
+ # Last resort: hardcoded UTC-5 (EST), no DST handling
26
+ now_utc = datetime.now(timezone.utc)
27
+ if now_utc.hour < 9: # 4 AM Eastern = 9 AM UTC (EST)
28
+ service_date = now_utc.date() - timedelta(days=1)
29
+ else:
30
+ service_date = now_utc.date()
31
+ service_start_utc = datetime.combine(service_date, time.min, tzinfo=timezone.utc) + timedelta(hours=5)
32
+ return int(service_start_utc.timestamp())
33
+
34
+ # Get current UTC time and convert to Eastern
35
+ now_utc = datetime.now(timezone.utc)
36
+ now_eastern = now_utc.astimezone(eastern_tz)
37
+
38
+ # TTC service day flips at 4:00 AM Eastern
39
+ if now_eastern.hour < 4:
40
+ service_date = now_eastern.date() - timedelta(days=1)
41
  else:
42
+ service_date = now_eastern.date()
43
+
44
+ # Create datetime at 00:00:00 Eastern, convert to UTC
45
+ try:
46
+ # zoneinfo (Python 3.9+)
47
+ service_start_eastern = datetime.combine(service_date, time.min, tzinfo=eastern_tz)
48
+ except TypeError:
49
+ # pytz needs localize
50
+ service_start_eastern = eastern_tz.localize(datetime.combine(service_date, time.min))
51
 
52
+ service_start_utc = service_start_eastern.astimezone(timezone.utc)
53
+ return int(service_start_utc.timestamp())
 
54
 
55
  def translate_occupancy(status):
56
  """Maps GTFS occupancy enums to human readable strings."""