some custom stuff
This commit is contained in:
@@ -1,24 +1,34 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"iter"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type StopTime struct {
|
||||
departureTime int
|
||||
Stop *Stop `json:"stop"`
|
||||
TripID string `json:"trip_id" csv:"trip_id"`
|
||||
ArrivalTime string `json:"arrival_time" csv:"arrival_time"`
|
||||
DepartureTime string `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"`
|
||||
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)
|
||||
}
|
||||
|
||||
func parseTime(s string) int {
|
||||
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
|
||||
@@ -26,37 +36,29 @@ func parseTime(s string) int {
|
||||
h, _ := strconv.Atoi(parts[0])
|
||||
m, _ := strconv.Atoi(parts[1])
|
||||
sec, _ := strconv.Atoi(parts[2])
|
||||
return h*3600 + m*60 + sec
|
||||
return SecondsAfterMidnight(h*3600 + m*60 + sec)
|
||||
}
|
||||
|
||||
func (st *StopTime) DepartTimeAsSeconds() int {
|
||||
if st.departureTime > 0 {
|
||||
return st.departureTime
|
||||
}
|
||||
if st.DepartureTime == "" {
|
||||
return 0
|
||||
}
|
||||
|
||||
st.departureTime = parseTime(st.DepartureTime)
|
||||
return st.departureTime
|
||||
func AsSecondsAfterMidnight(when time.Time) SecondsAfterMidnight {
|
||||
return SecondsAfterMidnight(when.Hour()*3600 + when.Minute()*60 + when.Second())
|
||||
}
|
||||
|
||||
func (st *StopTime) DepartsAfter(when time.Time) bool {
|
||||
secondsAfterMidnight := st.DepartTimeAsSeconds()
|
||||
return secondsAfterMidnight >= when.Hour()*3600+when.Minute()*60+when.Second()
|
||||
|
||||
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) 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
|
||||
|
||||
Reference in New Issue
Block a user