Add timezone-aware station lookup and formatting

These changes ensure displayed times respect the station's timezone. Something I mistakenly overlooked when creating this.

Luckily, my screenshot with Ashland isn't affected by this change.
This commit is contained in:
Tony Bark 2026-08-24 01:00:47 -04:00
parent fc45303761
commit dd50d7cd4a
3 changed files with 42 additions and 19 deletions

View file

@ -7,6 +7,12 @@ enum AmtrakError: Error {
case stationNotFound(String)
}
struct TrainStationLookUp: Sendable {
var trainNumbers: [String]
var timeZone: TimeZone?
}
struct AmtrakClient {
var decoder = JSONDecoder()
@ -41,15 +47,21 @@ struct AmtrakClient {
.first
.map(String.init) ?? trainId
}
func fetchStationTrainNumbers(stationCode code: String) async throws -> [String] {
func trainStationLookup(stationCode code: String) async throws -> TrainStationLookUp {
let reponse = try await fetchStation(code: code)
guard let meta = reponse[code] else {
throw AmtrakError.stationNotFound(code)
}
return Array(Set(meta.trains.map(trainNumber(fromTrainId:))))
let trainNumbers = Array(Set(meta.trains.map(trainNumber(fromTrainId:))))
let timeZone = meta.tz.flatMap(TimeZone.init(identifier:))
return TrainStationLookUp(
trainNumbers: trainNumbers,
timeZone: timeZone
)
}
func get<T: Decodable>(_ urlString: String) async throws -> T {