From 5007ccb28772e1b637af83024e036debf03b3c6b Mon Sep 17 00:00:00 2001 From: Jasper Bok Date: Mon, 15 Apr 2024 19:43:32 +0200 Subject: [PATCH] Store realtime times from API response --- bus.go | 43 +++++++++++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/bus.go b/bus.go index a95801e..7f13c8c 100644 --- a/bus.go +++ b/bus.go @@ -6,25 +6,30 @@ import ( "io" "net/http" "net/url" + "strconv" "time" ) type Departure struct { - TripID int - RealtimeTripId string - StopHeadsign string - ArrivalTime time.Time - DepartureTime time.Time - RouteShortName string + TripID int + RealtimeTripId string + StopHeadsign string + ArrivalTime time.Time + DepartureTime time.Time + RealtimeArrivalTime time.Time + RealtimeDepartureTime time.Time + RouteShortName string } type ResponseDeparture struct { - TripID int `json:"trip_id"` - RealtimeTripId string `json:"realtime_trip_id"` - StopHeadsign string `json:"stop_headsign"` - ArrivalTime string `json:"arrival_time"` - DepartureTime string `json:"departure_time"` - Tripdata struct { + TripID int `json:"trip_id"` + RealtimeTripId string `json:"realtime_trip_id"` + StopHeadsign string `json:"stop_headsign"` + ArrivalTime string `json:"arrival_time"` + DepartureTime string `json:"departure_time"` + RealtimeArrivalTime int `json:"realtime_arrival_time"` + RealtimeDepartureTime int `json:"realtime_departure_time"` + Tripdata struct { Route struct { RouteShortName string `json:"route_short_name"` } `json:"route"` @@ -52,6 +57,20 @@ func (rd ResponseDeparture) ToDeparture() (Departure, error) { } departure.DepartureTime = _time + if rd.RealtimeArrivalTime != 0 { + _time, err = parseUnixTimestamp(strconv.Itoa(rd.RealtimeArrivalTime)) + if err == nil { + departure.RealtimeArrivalTime = _time + } + } + + if rd.RealtimeDepartureTime != 0 { + _time, err = parseUnixTimestamp(strconv.Itoa(rd.RealtimeDepartureTime)) + if err == nil { + departure.RealtimeDepartureTime = _time + } + } + return departure, nil }