-
Notifications
You must be signed in to change notification settings - Fork 19
/
environment.go
114 lines (95 loc) · 3.14 KB
/
environment.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package utils
import (
"encoding/json"
"time"
)
// Environment defines the Environment that the Excellent function is running in, this includes
// the timezone the user is in as well as the preferred date and time formats.
type Environment interface {
DateFormat() DateFormat
SetDateFormat(DateFormat)
TimeFormat() TimeFormat
SetTimeFormat(TimeFormat)
Timezone() *time.Location
SetTimezone(*time.Location)
Languages() LanguageList
Locations() (*LocationHierarchy, error)
}
// NewDefaultEnvironment creates a new Environment with our usual defaults in the UTC timezone
func NewDefaultEnvironment() Environment {
return &environment{
dateFormat: DateFormat_yyyy_MM_dd,
timeFormat: TimeFormat_HH_mm,
timezone: time.UTC,
languages: LanguageList{},
}
}
// NewEnvironment creates a new Environment with the passed in date and time formats and timezone
func NewEnvironment(dateFormat DateFormat, timeFormat TimeFormat, timezone *time.Location, languages LanguageList) Environment {
if timezone == nil {
timezone = time.UTC
}
return &environment{
dateFormat: dateFormat,
timeFormat: timeFormat,
timezone: timezone,
languages: languages,
}
}
type environment struct {
dateFormat DateFormat
timeFormat TimeFormat
timezone *time.Location
languages LanguageList
}
func (e *environment) DateFormat() DateFormat { return e.dateFormat }
func (e *environment) SetDateFormat(dateFormat DateFormat) { e.dateFormat = dateFormat }
func (e *environment) TimeFormat() TimeFormat { return e.timeFormat }
func (e *environment) SetTimeFormat(timeFormat TimeFormat) { e.timeFormat = timeFormat }
func (e *environment) Timezone() *time.Location { return e.timezone }
func (e *environment) SetTimezone(timezone *time.Location) {
if timezone == nil {
e.timezone = time.UTC
} else {
e.timezone = timezone
}
}
func (e *environment) Languages() LanguageList { return e.languages }
func (e *environment) Locations() (*LocationHierarchy, error) { return nil, nil }
//------------------------------------------------------------------------------------------
// JSON Encoding / Decoding
//------------------------------------------------------------------------------------------
type envEnvelope struct {
DateFormat DateFormat `json:"date_format"`
TimeFormat TimeFormat `json:"time_format"`
Timezone string `json:"timezone"`
Languages LanguageList `json:"languages"`
}
// ReadEnvironment reads an environment from the given JSON
func ReadEnvironment(data json.RawMessage) (*environment, error) {
env := NewDefaultEnvironment().(*environment)
var envelope envEnvelope
var err error
err = json.Unmarshal(data, &envelope)
if err != nil {
return nil, err
}
env.dateFormat = envelope.DateFormat
env.timeFormat = envelope.TimeFormat
tz, err := time.LoadLocation(envelope.Timezone)
if err != nil {
return nil, err
}
env.timezone = tz
env.languages = envelope.Languages
return env, nil
}
func (e *environment) MarshalJSON() ([]byte, error) {
ee := envEnvelope{
DateFormat: e.dateFormat,
TimeFormat: e.timeFormat,
Timezone: e.timezone.String(),
Languages: e.languages,
}
return json.Marshal(ee)
}