-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtime.go
60 lines (47 loc) · 1.02 KB
/
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package podio
import (
"fmt"
"strconv"
"strings"
"time"
)
// Marshals and Unmarshals time in the common podio format.
// Podio time is always UTC.
type Time struct {
time.Time
}
const podioLayout = "2006-01-02 15:04:05"
func (t *Time) UnmarshalJSON(buf []byte) error {
// apparently we need to trim "
raw := strings.Trim(string(buf), "\"")
if raw == "null" {
// on null value we set the time to the time.Time zero value
t.Time = time.Time{}
return nil
}
tm, err := time.ParseInLocation(podioLayout, raw, time.UTC)
if err == nil {
t.Time = tm
}
return err
}
func (t *Time) MarshalJSON() ([]byte, error) {
s := t.Format(podioLayout)
return []byte(fmt.Sprintf(`"%s"`, s)), nil
}
type Timestamp struct {
time.Time
}
func (t *Timestamp) MarshalJSON() ([]byte, error) {
ts := t.Time.Unix()
stamp := fmt.Sprint(ts)
return []byte(stamp), nil
}
func (t *Timestamp) UnmarshalJSON(b []byte) error {
ts, err := strconv.Atoi(string(b))
if err != nil {
return err
}
t.Time = time.Unix(int64(ts), 0)
return nil
}