Files
go-gtfs/pkg/types/stoptime.go
2025-11-15 00:23:48 +01:00

66 lines
1.7 KiB
Go

package types
import (
"fmt"
"iter"
"strconv"
"strings"
"time"
)
type SecondsAfterMidnight int
func AsTime(s SecondsAfterMidnight) string {
h := int(s) / 3600
m := (int(s) % 3600) / 60
sec := int(s) % 60
return fmt.Sprintf("%02d:%02d:%02d", h, m, sec)
}
type StopTime struct {
Stop *Stop `json:"stop"`
TripId string `json:"trip_id" csv:"trip_id"`
ArrivalTime SecondsAfterMidnight `json:"arrival_time" csv:"arrival_time"`
DepartureTime SecondsAfterMidnight `json:"departure_time" csv:"departure_time"`
StopId string `json:"stop_id" csv:"stop_id"`
StopSequence int `json:"stop_sequence" csv:"stop_sequence"`
PickupType int `json:"pickup_type" csv:"pickup_type"`
DropOffType int `json:"drop_off_type" csv:"drop_off_type"`
}
func ParseTimeString(s string) SecondsAfterMidnight {
parts := strings.Split(s, ":")
if len(parts) != 3 {
return 0
}
h, _ := strconv.Atoi(parts[0])
m, _ := strconv.Atoi(parts[1])
sec, _ := strconv.Atoi(parts[2])
return SecondsAfterMidnight(h*3600 + m*60 + sec)
}
func AsSecondsAfterMidnight(when time.Time) SecondsAfterMidnight {
return SecondsAfterMidnight(when.Hour()*3600 + when.Minute()*60 + when.Second())
}
func (st *StopTime) DepartsAfter(when time.Time) bool {
return st.DepartureTime >= AsSecondsAfterMidnight(when)
}
func (st *StopTime) GetPossibleTrips() iter.Seq[*Trip] {
return func(yield func(*Trip) bool) {
for _, trip := range st.Stop.Trips {
if trip.TripId != st.TripId {
if !yield(trip) {
return
}
}
}
}
}
func (st *StopTime) SetStop(stop *Stop) {
st.Stop = stop
}