-
Notifications
You must be signed in to change notification settings - Fork 0
/
timezones.go
65 lines (53 loc) · 1.44 KB
/
timezones.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Package timezones provides an slice of timezones as well as a simple
// API for accessing and searching timezones.
package timezones
import (
"errors"
)
// TimeZone represents an international date/time zone.
type TimeZone struct {
Value string `json:"value"`
Abbr string `json:"abbr"`
Offset float32 `json:"offset"`
Isdst bool `json:"isdst"`
Text string `json:"text"`
Utc []string `json:"utc"`
}
var (
TimeZoneNotFound = errors.New("time zone not found")
NoTimeZonesFound = errors.New("no time zones were found")
)
// GetTimeZones returns a slice of TimeZone.
func GetTimeZones() []TimeZone {
return goData
}
// GetTimeZoneByValue returns a TimeZone by the Value field.
func GetTimeZoneByValue(value string) (*TimeZone, error) {
var timeZone *TimeZone
timeZones := GetTimeZones()
for i := range timeZones {
if timeZones[i].Value == value {
timeZone = &timeZones[i]
break
}
}
if timeZone == nil {
return nil, TimeZoneNotFound
}
return timeZone, nil
}
// GetTimeZoneByOffset returns all TimeZones where the Offset field equals
// the offset argument.
func GetTimeZoneByOffset(offset float32) ([]*TimeZone, error) {
var matchingTimeZones []*TimeZone
timeZones := GetTimeZones()
for i := range timeZones {
if timeZones[i].Offset == offset {
matchingTimeZones = append(matchingTimeZones, &timeZones[i])
}
}
if len(timeZones) == 0 {
return nil, TimeZoneNotFound
}
return matchingTimeZones, nil
}