package xlsx import ( "airlines/pkg/model" "errors" "strconv" "strings" "time" "airlines/pkg/airports" ) type Ticket struct { Sheet string Passenger string Title string FlightNumber string FromCity string FromCountry string FromAirport string FromCoords model.LatLong ToCity string ToCountry string ToAirport string ToCoords model.LatLong FlightDate string // (raw, expected YYYY-MM-DD; Excel text may start with ') FlightTime string // (raw, expected HH-MM or HH:MM; Excel text may start with ') PNR string Card string TicketNumber string // (may have a leading ' in Excel) } func (t Ticket) DateTime() (time.Time, *time.Location, error) { loc := t.inferLocationFromAirports() date := strings.TrimLeft(strings.TrimSpace(t.FlightDate), "'") hm := strings.TrimLeft(strings.TrimSpace(t.FlightTime), "'") hm = strings.ReplaceAll(hm, "-", ":") if date == "" || hm == "" { return time.Time{}, loc, errors.New("missing FlightDate or FlightTime") } ts, err := time.ParseInLocation("2006-01-02 15:04", date+" "+hm, loc) return ts, loc, err } func (t Ticket) inferLocationFromAirports() *time.Location { if loc := iataToLocation(t.FromAirport); loc != nil { return loc } if loc := iataToLocation(t.ToAirport); loc != nil { return loc } return time.Local } func iataToLocation(code string) *time.Location { iata := strings.ToUpper(strings.TrimSpace(code)) if len(iata) != 3 { return nil } ap, err := airports.LookupIATA(iata) if err != nil { return nil } // Prefer IANA tz name if tz := strings.TrimSpace(ap.Tz); tz != "" && tz != `\N` { if loc, err := time.LoadLocation(tz); err == nil { return loc } } // Fallback: fixed offset (no DST) if ap.Timezone != 0 { sec := int(ap.Timezone * 3600.0) return time.FixedZone("UTC"+offsetLabel(sec), sec) } return nil } func offsetLabel(sec int) string { sign := "+" if sec < 0 { sign = "-" sec = -sec } h := sec / 3600 m := (sec % 3600) / 60 return sign + two(h) + ":" + two(m) } func two(x int) string { if x < 10 { return "0" + strconv.Itoa(x) } return strconv.Itoa(x) }