-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmltime.go
90 lines (78 loc) · 2.08 KB
/
xmltime.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
/*
Package xmltime provides struct XMLTime. XMLTime wraps `time.Time` type. XMLTime
can be used to convert date-time in RFC3339 format in XML element values / attributes
when unmarshalling.
*/
package xmltime
import (
"encoding/xml"
"time"
)
var raiseParseErrOnEmpty = true
func mustAddZ(v string) bool {
_, err := time.Parse("2006-01-02T15:04:05", v)
if err != nil {
return false
}
return true
}
/*
AllowEmptyDateTime sets up the unmarshalling engine to convert empty dates in XML
to: 0001-01-01 00:00:00 +0000 UTC. In the absence of this flag being set, the parser
will terminate parsing and return error. This is a irreversible call, and a global one.
*/
func AllowEmptyDateTime() {
raiseParseErrOnEmpty = false
}
// XMLTime wraps time.Time.
type XMLTime struct {
time.Time
}
func (t *XMLTime) unmarshall(v string) error {
if mustAddZ(v) {
v += "Z"
}
if (!raiseParseErrOnEmpty) && v == "" {
*t = XMLTime{time.Time{}}
return nil
}
// RFC3339: 2006-01-02T15:04:05Z07:00
parse, err := time.Parse(time.RFC3339, v)
if err != nil {
return err
}
*t = XMLTime{parse}
return nil
}
// UnmarshalXML implements xml.Unmarshaler interface.
func (t *XMLTime) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var v string
err := d.DecodeElement(&v, &start)
if err != nil {
return err
}
return t.unmarshall(v)
}
// UnmarshalXMLAttr implements xml.UnmarshalerAttr interface.
func (t *XMLTime) UnmarshalXMLAttr(attr xml.Attr) error {
v := attr.Value
return t.unmarshall(v)
}
// MarshalXML implements xml.Marshaler interface.
func (t XMLTime) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
e.EncodeElement(t.Format(time.RFC3339), start)
return nil
}
// MarshalXMLAttr implements xml.MarshalerAttr interface.
func (t XMLTime) MarshalXMLAttr(name xml.Name) (xml.Attr, error) {
attr := xml.Attr{Name: name, Value: t.Format(time.RFC3339)}
return attr, nil
}
var beginningTime = time.Time{}
// IsBeginning returns true if date-time is set to: 0001-01-01 00:00:00 +0000 UTC.
func (t *XMLTime) IsBeginning() bool {
if t.Equal(beginningTime) {
return true
}
return false
}