-
Notifications
You must be signed in to change notification settings - Fork 9
/
timezone.go
66 lines (52 loc) · 1.33 KB
/
timezone.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
66
package timezone
import (
"database/sql/driver"
"encoding/json"
"fmt"
)
type Timezone string
//UnmarshalJSON unmarshall implementation for timezone
func (name *Timezone) UnmarshalJSON(data []byte) error {
var str string
if err := json.Unmarshal(data, &str); err != nil {
return err
}
if _, err := ByNameStrErr(str); err != nil {
return err
}
*name = Timezone(str)
return nil
}
//Value implementation of driver.Valuer
func (name Timezone) Value() (value driver.Value, err error) {
if name == "" {
return "", nil
}
if _, err = ByNameStrErr(string(name)); err != nil {
return nil, err
}
return name.String(), nil
}
//Validate implementation of ozzo-validation Validate interface
func (name Timezone) Validate() (err error) {
_, err = ByNameStrErr(string(name))
return
}
//String implementation of Stringer interface
func (name Timezone) String() string {
return string(name)
}
//ByNameStr lookup for timezone by name string
func ByNameStr(timezoneStr string) (result Timezone, ok bool) {
result, ok = timezonesByName[timezoneStr]
return
}
//ByNameStrErr lookup for timezone by name with error return type
func ByNameStrErr(timezoneStr string) (result Timezone, err error) {
var ok bool
result, ok = ByNameStr(timezoneStr)
if !ok {
err = fmt.Errorf("'%s' is not valid RFC-6557 Timezone", timezoneStr)
}
return
}