-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatetime.go
79 lines (68 loc) · 1.37 KB
/
datetime.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package freee
import (
"encoding/json"
"time"
)
type DateTime time.Time
type Date time.Time
type Time time.Time
func NewDateTime(year int, month time.Month, day int, hour int, min int, sec int) *DateTime {
d := DateTime(time.Date(year, month, day, hour, min, sec, 0, time.UTC))
return &d
}
func (d *DateTime) MarshalJSON() ([]byte, error) {
s := ""
if d != nil {
t := time.Time(*d)
s = t.Format("2006-01-02 15:04:05")
}
return json.Marshal(s)
}
func (d *DateTime) String() string {
s := ""
if d != nil {
t := time.Time(*d)
s = t.Format("2006-01-02 15:04:05")
}
return s
}
func NewDate(year int, month time.Month, day int) *Date {
d := Date(time.Date(year, month, day, 0, 0, 0, 0, time.UTC))
return &d
}
func (d *Date) MarshalJSON() ([]byte, error) {
s := ""
if d != nil {
t := time.Time(*d)
s = t.Format("2006-01-02")
}
return json.Marshal(s)
}
func (d *Date) String() string {
s := ""
if d != nil {
t := time.Time(*d)
s = t.Format("2006-01-02")
}
return s
}
func NewTime(hour int, min int, sec int) *DateTime {
d := DateTime(time.Date(0, 0, 0, hour, min, sec, 0, time.UTC))
return &d
}
func (t *Time) MarshalJSON() ([]byte, error) {
s := ""
if t != nil {
u := time.Time(*t)
s = u.Format("15:04:05")
}
return json.Marshal(s)
}
func (d *Time) String() string {
s := ""
if d != nil {
t := time.Time(*d)
s = t.Format("15:04:05")
}
return s
}