model

package
v1.15.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 12, 2026 License: MIT Imports: 5 Imported by: 2 modules, 2 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package model defines the data types decoded from the search.ch timetable API.

Index

Constants

This section is empty.

Variables

View Source
var SwissLocation = func() *time.Location {
	loc, err := time.LoadLocation("Europe/Zurich")
	if err != nil {
		loc = time.FixedZone("CET", 1*60*60)
	}
	return loc
}()

SwissLocation is the Europe/Zurich time zone, with a fixed-offset fallback when the tzdata lookup fails. The fallback does not handle the CET-CEST transition; main.go embeds time/tzdata to keep that path off the hot road.

Functions

This section is empty.

Types

type Connection

type Connection struct {
	From      string    `json:"from"`
	To        string    `json:"to"`
	Departure Timestamp `json:"departure"`
	Arrival   Timestamp `json:"arrival"`
	Duration  int       `json:"duration"` // seconds
	DepDelay  Delay     `json:"dep_delay"`
	ArrDelay  Delay     `json:"arr_delay"`
	Occupancy Occupancy `json:"occupancy"`
	Legs      []Leg     `json:"legs"`

	// Disruptions is kept raw: the API returns an object keyed by
	// disruption ID, but historically also an empty array.
	Disruptions json.RawMessage `json:"disruptions"`
}

Connection is one full route returned by the API.

func (Connection) DisruptionList added in v1.15.0

func (c Connection) DisruptionList() []Disruption

DisruptionList decodes the raw disruptions payload into a stable-ordered slice. The API returns an object keyed by disruption ID (historically an empty array), so unknown shapes simply yield nil.

func (Connection) FirstVehicleLeg added in v1.15.0

func (c Connection) FirstVehicleLeg() int

FirstVehicleLeg returns the index of the first vehicle leg, or -1.

func (Connection) LastVehicleLeg added in v1.15.0

func (c Connection) LastVehicleLeg() int

LastVehicleLeg returns the index of the last vehicle leg, or -1.

type Delay added in v1.15.0

type Delay struct {
	Minutes   int
	Cancelled bool
}

Delay is a delay decoded from the API's string format: "+N" for N minutes of delay, or "X" when the stop/leg is cancelled.

func (*Delay) UnmarshalJSON added in v1.15.0

func (d *Delay) UnmarshalJSON(b []byte) error

UnmarshalJSON tolerantly parses "+0", "+5", "X", "" and null.

type Disruption added in v1.15.0

type Disruption struct {
	ID    string                     `json:"id"`
	Texts map[string]DisruptionTexts `json:"texts"`
}

Disruption is one service alert attached to a connection.

func (Disruption) Text added in v1.15.0

func (d Disruption) Text() DisruptionTexts

Text returns the shortest text variant available (S, then M, then L).

type DisruptionTexts added in v1.15.0

type DisruptionTexts struct {
	Summary     string `json:"summary"`
	Duration    string `json:"duration"`
	Description string `json:"description"`
	Consequence string `json:"consequence"`
}

DisruptionTexts are the alert texts of a disruption in one verbosity level.

type Exit added in v1.15.0

type Exit struct {
	Name      string    `json:"name"`
	SBBName   string    `json:"sbb_name"`
	StopID    string    `json:"stopid"`
	Arrival   Timestamp `json:"arrival"`
	ArrDelay  Delay     `json:"arr_delay"`
	Track     string    `json:"track"`
	WaitTime  int       `json:"waittime"` // seconds until the next departure
	IsAddress bool      `json:"isaddress"`
	Lat       float64   `json:"lat"`
	Lon       float64   `json:"lon"`
}

Exit describes where (and when) a leg is left.

type Leg added in v1.15.0

type Leg struct {
	Name        string    `json:"name"`
	SBBName     string    `json:"sbb_name"`
	StopID      string    `json:"stopid"`
	TripID      string    `json:"tripid"`
	Arrival     Timestamp `json:"arrival"`   // arrival at this node
	Departure   Timestamp `json:"departure"` // departure from this node
	DepDelay    Delay     `json:"dep_delay"`
	Type        string    `json:"type"`      // walk, bus, tram, strain, express_train, ship, ...
	TypeName    string    `json:"type_name"` // human-readable vehicle kind
	Line        string    `json:"line"`      // e.g. "S13", "IC 1", "17"
	Category    string    `json:"*G"`        // e.g. "IC"
	LineNumber  string    `json:"*L"`        // e.g. "1"
	Terminal    string    `json:"terminal"`  // direction the vehicle is headed
	Operator    string    `json:"operator"`
	FgColor     string    `json:"fgcolor"` // line color, hex without '#'
	BgColor     string    `json:"bgcolor"` // line color, hex without '#'
	Track       string    `json:"track"`   // "13"; a trailing '!' marks a platform change
	Occupancy   Occupancy `json:"occupancy"`
	RunningTime int       `json:"runningtime"` // seconds on board / walking
	WaitTime    int       `json:"waittime"`    // seconds waited before departure
	IsAddress   bool      `json:"isaddress"`
	Lat         float64   `json:"lat"`
	Lon         float64   `json:"lon"`
	Exit        *Exit     `json:"exit"`
	Stops       []Stop    `json:"stops"`
}

Leg is one node of a connection: the stop where you board (or start walking) plus the vehicle ridden and the Exit where you get off. The final leg of a connection is a bare arrival node: it has a Name and Arrival but no Exit, vehicle or walk attached.

func (Leg) DisplayLine added in v1.15.0

func (l Leg) DisplayLine() string

DisplayLine returns the line label the way SBB displays it. Trains already arrive combined ("S13", "IC 1") and stay as-is; for other modes the API's line is the bare number, so the category is prefixed: buses "B 33", trams "T 51", ships "BAT 3600". Modes without a line label (gondolas, funiculars, ...) fall back to category and number ("GB 2042"), then to the human-readable type name.

func (Leg) IsVehicle added in v1.15.0

func (l Leg) IsVehicle() bool

IsVehicle reports whether the leg is ridden on a transport vehicle. Some modes (gondolas, funiculars, ...) carry no line label, so the leg type is the discriminator: vehicle legs always have one, while the final arrival node of a connection has none.

func (Leg) IsWalk added in v1.15.0

func (l Leg) IsWalk() bool

IsWalk reports whether the leg is a walking transfer.

func (Leg) OperatorName added in v1.15.0

func (l Leg) OperatorName() string

OperatorName returns the operator with the API's stray internal whitespace collapsed (e.g. "VBZ F" -> "VBZ F").

type Occupancy added in v1.15.0

type Occupancy string

Occupancy is the API's two-digit occupancy code: the first digit is the first-class load, the second the second-class load (1=low, 2=medium, 3=high, 0/absent=unknown).

func (Occupancy) FirstClass added in v1.15.0

func (o Occupancy) FirstClass() int

FirstClass returns the first-class occupancy level (0=unknown, 1=low, 2=medium, 3=high).

func (Occupancy) SecondClass added in v1.15.0

func (o Occupancy) SecondClass() int

SecondClass returns the second-class occupancy level (0=unknown, 1=low, 2=medium, 3=high).

type Stop added in v1.15.0

type Stop struct {
	Name      string    `json:"name"`
	StopID    string    `json:"stopid"`
	Arrival   Timestamp `json:"arrival"`
	Departure Timestamp `json:"departure"`
	DepDelay  Delay     `json:"dep_delay"`
	Occupancy Occupancy `json:"occupancy"`
	Lat       float64   `json:"lat"`
	Lon       float64   `json:"lon"`
}

Stop is an intermediate halt within a Leg.

type Timestamp

type Timestamp struct {
	time.Time
}

Timestamp is a time.Time decoded from the API's "2006-01-02 15:04:05" format, which is expressed in Swiss local time.

func (Timestamp) Local added in v1.12.2

func (t Timestamp) Local() time.Time

Local returns the timestamp in Swiss time, overriding time.Time.Local.

func (Timestamp) Sub

func (t Timestamp) Sub(other Timestamp) time.Duration

Sub returns the duration between two Timestamps.

func (*Timestamp) UnmarshalJSON

func (t *Timestamp) UnmarshalJSON(b []byte) error

UnmarshalJSON parses the API's "2006-01-02 15:04:05" format in Swiss local time.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL