-
Notifications
You must be signed in to change notification settings - Fork 36
/
date_time.go
94 lines (74 loc) · 1.64 KB
/
date_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
87
88
89
90
91
92
93
94
package stationxml
/*
* WARNING: CODE GENERATED AUTOMATICALLY.
*
* Use "go generate" to update these files.
*
* THIS FILE SHOULD NOT BE EDITED BY HAND.
*
*/
import (
"encoding/xml"
"time"
)
// only include down to seconds
const DateTimeFormat = "2006-01-02T15:04:05"
type DateTime struct {
time.Time `xml:",chardata"`
}
func Now() DateTime {
return DateTime{Time: time.Now()}
}
func NewDateTime(t time.Time) DateTime {
return DateTime{
Time: t,
}
}
func ParseDateTime(s string) (DateTime, error) {
x, err := time.Parse(DateTimeFormat, s)
return DateTime{x}, err
}
func MustParseDateTime(s string) DateTime {
x, err := time.Parse(DateTimeFormat, s)
if err != nil {
panic(err)
}
return DateTime{x}
}
func MustParseDateTimePtr(s string) *DateTime {
x := MustParseDateTime(s)
return &x
}
func (t DateTime) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
if t.Time.Year() < 1880 {
return e.EncodeElement(nil, start)
}
return e.EncodeElement(t.Time.Format(DateTimeFormat), start)
}
func (t *DateTime) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var s string
err := d.DecodeElement(&s, &start)
if err != nil {
return err
}
x, err := time.Parse(DateTimeFormat, s)
if err != nil {
return nil
}
*t = DateTime{x}
return nil
}
func (t DateTime) MarshalXMLAttr(name xml.Name) (xml.Attr, error) {
if t.Time.Year() < 1880 {
return xml.Attr{}, nil
}
return xml.Attr{Name: name, Value: t.Time.Format(DateTimeFormat)}, nil
}
func (t *DateTime) UnmarshalXMLAttr(attr xml.Attr) error {
x, err := time.Parse(DateTimeFormat, attr.Value)
if err != nil {
return nil
}
*t = DateTime{x}
return nil
}