-
Notifications
You must be signed in to change notification settings - Fork 19
/
environment.go
87 lines (71 loc) · 2.43 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
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)
}
// NewDefaultEnvironment creates a new Environment with our usual defaults in the UTC timezone
func NewDefaultEnvironment() Environment {
return &environment{DateFormat_yyyy_MM_dd, TimeFormat_HH_mm, time.UTC}
}
// NewEnvironment creates a new Environment with the passed in date and time formats and timezone
func NewEnvironment(dateFormat DateFormat, timeFormat TimeFormat, timezone *time.Location) Environment {
if timezone == nil {
timezone = time.UTC
}
return &environment{dateFormat, timeFormat, timezone}
}
type environment struct {
dateFormat DateFormat
timeFormat TimeFormat
timezone *time.Location
}
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
}
}
//------------------------------------------------------------------------------------------
// JSON Encoding / Decoding
//------------------------------------------------------------------------------------------
type envEnvelope struct {
DateFormat DateFormat `json:"date_format"`
TimeFormat TimeFormat `json:"time_format"`
Timezone string `json:"timezone"`
}
func (e *environment) UnmarshalJSON(data []byte) error {
var envelope envEnvelope
var err error
err = json.Unmarshal(data, &envelope)
if err != nil {
return err
}
e.dateFormat = envelope.DateFormat
e.timeFormat = envelope.TimeFormat
tz, err := time.LoadLocation(envelope.Timezone)
if err != nil {
return err
}
e.timezone = tz
return nil
}
func (e *environment) MarshalJSON() ([]byte, error) {
ee := envEnvelope{e.dateFormat, e.timeFormat, e.timezone.String()}
return json.Marshal(ee)
}