-
Notifications
You must be signed in to change notification settings - Fork 1
/
time.go
42 lines (37 loc) · 923 Bytes
/
time.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
package common
import (
"strings"
"time"
)
type CustomTime struct {
time.Time
}
var locCST = time.FixedZone("CST", 8*3600)
func (ct *CustomTime) UnmarshalJSON(data []byte) error {
s := strings.Trim(string(data), `"`)
// Ignore null, like in the main JSON package.
if s == "null" {
return nil
}
// Fractional seconds are handled implicitly by Parse.
var err error
ct.Time, err = time.Parse(time.RFC3339, s)
if err != nil {
ct.Time, err = time.ParseInLocation(`2006-01-02T15:04:05`, s, locCST)
}
return err
}
func (ct *CustomTime) UnmarshalText(data []byte) error {
s := strings.Trim(string(data), `"`)
// Ignore null, like in the main JSON package.
if s == "" {
return nil
}
// Fractional seconds are handled implicitly by Parse.
var err error
ct.Time, err = time.Parse(time.RFC3339, s)
if err != nil {
ct.Time, err = time.ParseInLocation(`2006-01-02T15:04:05`, s, locCST)
}
return err
}