From bb833561aa74f02970aee13cdc75973b29716491 Mon Sep 17 00:00:00 2001 From: leshe4ka46 Date: Mon, 27 Oct 2025 20:36:28 +0300 Subject: # This is a combination of 2 commits. # This is the 1st commit message: unmarshal all formats, merge them in the single table, users are truly unique # This is the commit message #2: i --- pkg/model/types.go | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 pkg/model/types.go (limited to 'pkg/model/types.go') diff --git a/pkg/model/types.go b/pkg/model/types.go new file mode 100644 index 0000000..fd65d46 --- /dev/null +++ b/pkg/model/types.go @@ -0,0 +1,74 @@ +package model + +import ( + "encoding/json" + "math" + "strconv" + "strings" + "time" + + "airlines/pkg/airports" +) + +type Sex uint8 + +const ( + SexUnknown Sex = 0 + SexMale Sex = 1 + SexFemale Sex = 2 +) + +func (s *Sex) UnmarshalJSON(b []byte) error { + var raw string + if err := json.Unmarshal(b, &raw); err == nil { + switch strings.ToLower(strings.TrimSpace(raw)) { + case "male": + *s = SexMale + return nil + case "female": + *s = SexFemale + return nil + case "", "unknown", "null": + *s = SexUnknown + return nil + } + } + // also accept numbers in JSON + var n int + if err := json.Unmarshal(b, &n); err == nil { + *s = Sex(n) + return nil + } + *s = SexUnknown + return nil +} + +func TzFromAirportRecord(a airports.Airport) *time.Location { + return TzFromOffsetHours(a.Timezone) +} + +func TzFromOffsetHours(hours float64) *time.Location { + minTotal := int(math.Round(hours * 60.0)) + sec := minTotal * 60 + + name := fixedZoneNameFromMinutes(minTotal) + return time.FixedZone(name, sec) +} + +func fixedZoneNameFromMinutes(minTotal int) string { + sign := "+" + if minTotal < 0 { + sign = "-" + minTotal = -minTotal + } + h := minTotal / 60 + m := minTotal % 60 + return "UTC" + sign + two(h) + ":" + two(m) +} + +func two(x int) string { + if x < 10 { + return "0" + strconv.Itoa(x) + } + return strconv.Itoa(x) +} -- cgit v1.2.3