-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.go
86 lines (76 loc) · 1.52 KB
/
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package gpelements
import (
"encoding/xml"
"fmt"
"strings"
"time"
)
type Time time.Time
func NewTime(t time.Time) *Time {
if t.IsZero() {
t = time.Now().UTC()
}
then := Time(t)
return &then
}
func (t *Time) String() string {
if t == nil {
return ""
}
return time.Time(*t).String()
}
func ParseTime(s, layout string) (*Time, error) {
t, err := time.Parse(s, layout)
if err != nil {
return nil, err
}
t0 := Time(t)
return &t0, err
}
func (c *Time) Format(layout string) string {
return time.Time(*c).Format(layout)
}
func (c *Time) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
tokens := []xml.Token{start}
s := "null"
if c != nil {
s = time.Time(*c).Format(KVNTimeFormat)
}
tokens = append(tokens, xml.CharData(s))
tokens = append(tokens, xml.EndElement{start.Name})
for _, t := range tokens {
if err := e.EncodeToken(t); err != nil {
return err
}
}
return nil
}
func (c *Time) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var v string
d.DecodeElement(&v, &start)
if len(v) == 0 {
return nil
}
parse, err := time.Parse(KVNTimeFormat, v)
if err != nil {
return err
}
*c = Time(parse)
return nil
}
func (c *Time) UnmarshalJSON(data []byte) error {
s := strings.Trim(string(data), `"`)
t, err := time.Parse(KVNTimeFormat, s)
if err != nil {
return err
}
*c = Time(t)
return nil
}
func (c *Time) MarshalJSON() ([]byte, error) {
if c == nil {
return []byte(`"null"`), nil
}
s := fmt.Sprintf(`"%s"`, time.Time(*c).Format(KVNTimeFormat))
return []byte(s), nil
}