diff options
| author | Ariel Costas Guerrero <ariel@costas.dev> | 2025-12-29 00:41:52 +0100 |
|---|---|---|
| committer | Ariel Costas Guerrero <ariel@costas.dev> | 2025-12-29 00:41:52 +0100 |
| commit | a304c24b32c0327436bbd8c2853e60668e161b42 (patch) | |
| tree | 08f65c05daca134cf4d2e4f779bd15d98fd66370 /src/Enmarcha.Backend/Extensions/StopScheduleExtensions.cs | |
| parent | 120a3c6bddd0fb8d9fa05df4763596956554c025 (diff) | |
Rename a lot of stuff, add Santiago real time
Diffstat (limited to 'src/Enmarcha.Backend/Extensions/StopScheduleExtensions.cs')
| -rw-r--r-- | src/Enmarcha.Backend/Extensions/StopScheduleExtensions.cs | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/src/Enmarcha.Backend/Extensions/StopScheduleExtensions.cs b/src/Enmarcha.Backend/Extensions/StopScheduleExtensions.cs new file mode 100644 index 0000000..bbf0e4b --- /dev/null +++ b/src/Enmarcha.Backend/Extensions/StopScheduleExtensions.cs @@ -0,0 +1,59 @@ +using Enmarcha.Backend.Types; +using static Enmarcha.Backend.Types.StopArrivals.Types; + +namespace Enmarcha.Backend.Extensions; + +public static class StopScheduleExtensions +{ + public static DateTime? StartingDateTime(this StopArrivals.Types.ScheduledArrival stop, DateTime baseDate) + { + return ParseGtfsTime(stop.StartingTime, baseDate); + } + + public static DateTime? CallingDateTime(this StopArrivals.Types.ScheduledArrival stop, DateTime baseDate) + { + return ParseGtfsTime(stop.CallingTime, baseDate); + } + + /// <summary> + /// Parse GTFS time format (HH:MM:SS) which can have hours >= 24 for services past midnight + /// </summary> + private static DateTime? ParseGtfsTime(string timeStr, DateTime baseDate) + { + if (string.IsNullOrWhiteSpace(timeStr)) + { + return null; + } + + var parts = timeStr.Split(':'); + if (parts.Length != 3) + { + return null; + } + + if (!int.TryParse(parts[0], out var hours) || + !int.TryParse(parts[1], out var minutes) || + !int.TryParse(parts[2], out var seconds)) + { + return null; + } + + // Handle GTFS times that exceed 24 hours (e.g., 25:30:00 for 1:30 AM next day) + var days = hours / 24; + var normalizedHours = hours % 24; + + try + { + var dt = baseDate + .AddDays(days) + .AddHours(normalizedHours) + .AddMinutes(minutes) + .AddSeconds(seconds); + return dt.AddSeconds(60 - dt.Second); + } + catch + { + return null; + } + } +} |
