...
1
16
17
18
19
20
21
22 package time
23
24 import (
25 "bytes"
26 "time"
27 )
28
29
30 var emptyString = `""`
31
32
33
34 type Time struct {
35 time.Time
36 }
37
38
39 func Now() Time {
40 return Time{time.Now()}
41 }
42
43 func (t Time) MarshalJSON() ([]byte, error) {
44 if t.Time.IsZero() {
45 return []byte(emptyString), nil
46 }
47
48 return t.Time.MarshalJSON()
49 }
50
51 func (t *Time) UnmarshalJSON(b []byte) error {
52 if bytes.Equal(b, []byte("null")) {
53 return nil
54 }
55
56
57 if bytes.Equal([]byte(emptyString), b) {
58 return nil
59 }
60
61 return t.Time.UnmarshalJSON(b)
62 }
63
64 func Parse(layout, value string) (Time, error) {
65 t, err := time.Parse(layout, value)
66 return Time{Time: t}, err
67 }
68 func ParseInLocation(layout, value string, loc *time.Location) (Time, error) {
69 t, err := time.ParseInLocation(layout, value, loc)
70 return Time{Time: t}, err
71 }
72
73 func Date(year int, month time.Month, day, hour, min, sec, nsec int, loc *time.Location) Time {
74 return Time{Time: time.Date(year, month, day, hour, min, sec, nsec, loc)}
75 }
76
77 func Unix(sec int64, nsec int64) Time { return Time{Time: time.Unix(sec, nsec)} }
78
79 func (t Time) Add(d time.Duration) Time { return Time{Time: t.Time.Add(d)} }
80 func (t Time) AddDate(years int, months int, days int) Time {
81 return Time{Time: t.Time.AddDate(years, months, days)}
82 }
83 func (t Time) After(u Time) bool { return t.Time.After(u.Time) }
84 func (t Time) Before(u Time) bool { return t.Time.Before(u.Time) }
85 func (t Time) Equal(u Time) bool { return t.Time.Equal(u.Time) }
86 func (t Time) In(loc *time.Location) Time { return Time{Time: t.Time.In(loc)} }
87 func (t Time) Local() Time { return Time{Time: t.Time.Local()} }
88 func (t Time) Round(d time.Duration) Time { return Time{Time: t.Time.Round(d)} }
89 func (t Time) Sub(u Time) time.Duration { return t.Time.Sub(u.Time) }
90 func (t Time) Truncate(d time.Duration) Time { return Time{Time: t.Time.Truncate(d)} }
91 func (t Time) UTC() Time { return Time{Time: t.Time.UTC()} }
92
View as plain text