forked from bitbandi/go-southxchange
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timestamp.go
39 lines (32 loc) · 813 Bytes
/
timestamp.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
package southxchange
import (
"time"
"strconv"
)
type Timestamp time.Time
func (t Timestamp) String() string {
return t.Format("2006-01-02T15:04:05.999999999")
}
func (t Timestamp) Format(layout string) string {
return time.Time(t).Format(layout)
}
func (t *Timestamp) UnmarshalJSON(body []byte) (err error) {
s, err := strconv.Unquote(string(body))
if err != nil {
return err
}
if *(*time.Time)(t), err = time.Parse("2006-01-02T15:04:05.999999999", s); err != nil {
return err
}
return nil
}
func (t Timestamp) MarshalJSON() ([]byte, error) {
return []byte(strconv.Quote(time.Time(t).Format("2006-01-02T15:04:05.999999999"))), nil
}
func (t Timestamp) Diff(u Timestamp) time.Duration {
diff := time.Time(t).Sub(time.Time(u))
if diff.Nanoseconds() >= 0 {
return diff
}
return -diff;
}