From ca0003edfdea2b5636d4e455190699e4ec3bc263 Mon Sep 17 00:00:00 2001 From: Nahuel Lofeudo Date: Sat, 2 May 2026 08:16:13 +0100 Subject: [PATCH] Moved GTFS struct back to structs, refresh works now. --- src/gtfs/loader.rs | 2 +- src/gtfs/mod.rs | 37 +++++++++++-------------------------- src/gtfs/structs.rs | 28 +++++++++++++++++++++++++--- src/main.rs | 2 +- 4 files changed, 38 insertions(+), 31 deletions(-) diff --git a/src/gtfs/loader.rs b/src/gtfs/loader.rs index eb1efa0..7435973 100644 --- a/src/gtfs/loader.rs +++ b/src/gtfs/loader.rs @@ -9,7 +9,7 @@ use std::{ use zip::ZipArchive; use crate::gtfs::{ - Gtfs, + structs::Gtfs, utils::{route_ids_from_numbers, stop_ids_from_codes}, }; diff --git a/src/gtfs/mod.rs b/src/gtfs/mod.rs index 59c66d5..5dda86c 100644 --- a/src/gtfs/mod.rs +++ b/src/gtfs/mod.rs @@ -1,35 +1,15 @@ mod loader; mod utils; pub mod structs; -use chrono::{DateTime, Local}; -use log::{debug, info, warn}; -use sdl3::libc::stpncpy; +use chrono::{DateTime, Local, NaiveTime, Timelike}; +use log::{debug}; use std::{ collections::{HashMap, HashSet}, fs::File, }; -use gtfs_structures::{Agency, Calendar, CalendarDate, Exception, RawStopTime, RawTrip, Route, Stop}; +use gtfs_structures::{Exception, RawTrip}; -use crate::gtfs::{loader::load_gtfs, structs::{Arrival, Preferences}}; - -// The main GTFS struct. This is similar to (but not exactly) gtfs-structures::Gtfs because we don't need everything -#[derive(Debug)] -pub struct Gtfs { - /// All agencies. They can not be read by `agency_id`, as it is not a required field - pub agencies: Vec, - /// All Calendar by `service_id` - pub calendar: HashMap, - /// All calendar dates grouped by service_id - pub calendar_dates: HashMap>, - /// All routes by `route_id` - pub routes: HashMap, - /// All stop by `stop_id`. - pub stops: HashMap, - /// All trips by trip_id - pub trips: HashMap, - /// Stop times for the chosen stops and the chosen routes - pub stop_times: HashMap<(String, u32), RawStopTime>, -} +use crate::gtfs::{loader::load_gtfs, structs::{Arrival, Gtfs, Preferences}}; impl Gtfs { @@ -77,7 +57,12 @@ impl Gtfs { // Finally, find the arrivals for the active trips on the chosen stops let mut arrivals: Vec = Vec::new(); - let current_timestamp = target_datetime.timestamp(); + + // Stop times are parsed as the number of seconds since midnight on the current day + let current_timestamp = target_datetime.time().hour() * 3600 + + target_datetime.time().minute() * 60 + + target_datetime.time().second(); + for (_id, stop_time) in self.stop_times.iter() { if trips.contains_key(&stop_time.trip_id) { let stop_timestamp = stop_time.departure_time.or(stop_time.arrival_time).unwrap(); @@ -86,7 +71,7 @@ impl Gtfs { let arrival: Arrival = Arrival { route: self.routes.get(&self.trips.get(&stop_time.trip_id).unwrap().route_id).unwrap(), stop: self.stops.get(&stop_time.stop_id).unwrap(), - departure_time: stop_timestamp + departure_time: NaiveTime::from_num_seconds_from_midnight_opt(stop_timestamp, 0).unwrap() }; arrivals.push(arrival); } diff --git a/src/gtfs/structs.rs b/src/gtfs/structs.rs index 60e69a8..51f7cd4 100644 --- a/src/gtfs/structs.rs +++ b/src/gtfs/structs.rs @@ -1,6 +1,7 @@ -use std::collections::{HashSet}; +use std::collections::{HashMap, HashSet}; -use gtfs_structures::{Route, Stop}; +use chrono::NaiveTime; +use gtfs_structures::{Agency, Calendar, CalendarDate, RawStopTime, RawTrip, Route, Stop}; // This is to store the preferences for the GTFS(-R) side of the code. pub struct Preferences { @@ -8,10 +9,31 @@ pub struct Preferences { pub stop_codes: HashSet, } + +// The main GTFS struct. This is similar to (but not exactly) gtfs-structures::Gtfs because we don't need everything +#[derive(Debug)] +pub struct Gtfs { + /// All agencies. They can not be read by `agency_id`, as it is not a required field + pub agencies: Vec, + /// All Calendar by `service_id` + pub calendar: HashMap, + /// All calendar dates grouped by service_id + pub calendar_dates: HashMap>, + /// All routes by `route_id` + pub routes: HashMap, + /// All stop by `stop_id`. + pub stops: HashMap, + /// All trips by trip_id + pub trips: HashMap, + /// Stop times for the chosen stops and the chosen routes + pub stop_times: HashMap<(String, u32), RawStopTime>, +} + + #[derive(Debug)] pub struct Arrival<'a> { pub route: &'a Route, pub stop: &'a Stop, - pub departure_time: u32, + pub departure_time: NaiveTime, } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index bc0f56c..e687df0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,7 @@ use chrono::{DateTime, Local}; use sdl3::{event::Event, libc::statx_timestamp}; use log::{Level, Metadata, Record, debug, error, info, warn}; -use crate::{gtfs::Gtfs, renderer::Screen}; +use crate::{gtfs::structs::Gtfs, renderer::Screen}; const SRC_FILE: &str = "/home/nahuel/Downloads/GTFS_Realtime.zip";