-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtime.go
57 lines (44 loc) · 1004 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package easy
import (
"time"
)
type Time time.Time
func NewTime(t time.Time) Time {
return Time(t.Local())
}
func (t *Time) UnmarshalJSON(data []byte) error {
if string(data) == "null" {
return nil
}
tm, err := time.ParseInLocation(`"`+time.DateTime+`"`, ByteToString(data), time.Local)
if err != nil {
return err
}
*t = NewTime(tm)
return nil
}
func (t Time) MarshalJSON() ([]byte, error) {
format := t.Time().Format(`"` + time.DateTime + `"`)
return StringToByte(format), nil
}
func (t Time) MarshalText() ([]byte, error) {
format := t.Time().Format(time.DateTime)
return StringToByte(format), nil
}
func (t *Time) UnmarshalText(data []byte) error {
tm, err := time.ParseInLocation(time.DateTime, ByteToString(data), time.Local)
if err != nil {
return err
}
*t = NewTime(tm)
return nil
}
func (t Time) Time() time.Time {
return time.Time(t)
}
func (t Time) String() string {
return t.Time().Format(time.DateTime)
}
func Now() Time {
return NewTime(time.Now())
}