aboutsummaryrefslogtreecommitdiff
path: root/pkg/model/types.go
diff options
context:
space:
mode:
authorleshe4ka46 <alex9102naid1@ya.ru>2025-10-27 20:36:28 +0300
committerleshe4ka46 <alex9102naid1@ya.ru>2025-10-28 13:42:21 +0300
commitbb833561aa74f02970aee13cdc75973b29716491 (patch)
tree0914668e11dbf825979f7419ce1bc78294cd3f7f /pkg/model/types.go
parente17a425dfb3382310fb5863f516dacdca9f44956 (diff)
# 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
Diffstat (limited to 'pkg/model/types.go')
-rw-r--r--pkg/model/types.go74
1 files changed, 74 insertions, 0 deletions
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)
+}