-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtime.go
41 lines (35 loc) · 792 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
package expensify
import (
"bytes"
"fmt"
"time"
)
const (
layoutISO = "2006-01-02"
)
// Time which marshals to and unmarshals from "yyyy-mm-dd" format.
type Time struct {
time.Time
}
// NewTime returns a new Time from the given time.Time.
func NewTime(t time.Time) Time {
return Time{t}
}
// MarshalJSON implements json.Marshaler.
func (t Time) MarshalJSON() ([]byte, error) {
if t.IsZero() {
return []byte("null"), nil
}
stamp := fmt.Sprintf("%q", t.Format(layoutISO))
return []byte(stamp), nil
}
// UnmarshalJSON implements json.Unmarshaler.
func (t *Time) UnmarshalJSON(b []byte) (err error) {
b = bytes.Trim(b, "\"")
if bytes.Equal(b, []byte("null")) {
return nil
} else if t.Time, err = time.Parse(layoutISO, string(b)); err != nil {
return err
}
return nil
}