-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtime.go
73 lines (64 loc) · 1.85 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
package elementalconductor
import (
"encoding/xml"
"time"
)
const (
dateTimeLayout = "2006-01-02 15:04:05 -0700"
errorDateTimeLayout = "2006-01-02T15:04:05-07:00"
)
// DateTime is a custom struct for representing time within ElementalConductor.
// It customizes marshalling, and always store the underlying time in UTC.
type DateTime struct {
time.Time
}
// MarshalXML implementation on DateTimeg to skip "zero" time values
func (jdt DateTime) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
if !jdt.IsZero() {
e.EncodeElement(jdt.Time.Format(dateTimeLayout), start)
}
return nil
}
// UnmarshalXML implementation on DateTimeg to use dateTimeLayout
func (jdt *DateTime) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var err error
jdt.Time, err = unmarshalTime(d, start, dateTimeLayout)
return err
}
// JobErrorDateTime is a custom time struct to be used on Media items
type JobErrorDateTime struct {
time.Time
}
// MarshalXML implementation on JobErrorDateTime to skip "zero" time values
func (jdt JobErrorDateTime) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
if !jdt.IsZero() {
e.EncodeElement(jdt.Time.Format(errorDateTimeLayout), start)
}
return nil
}
// UnmarshalXML implementation on JobErrorDateTime to use errorDateTimeLayout
func (jdt *JobErrorDateTime) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var err error
jdt.Time, err = unmarshalTime(d, start, errorDateTimeLayout)
return err
}
func unmarshalTime(d *xml.Decoder, start xml.StartElement, format string) (time.Time, error) {
var t time.Time
var content string
err := d.DecodeElement(&content, &start)
if err != nil {
return t, err
}
if content == "" {
return t, nil
}
if content == "0001-01-01T00:00:00Z" {
return t, nil
}
t, err = time.Parse(format, content)
if err != nil {
return t, err
}
t = t.UTC()
return t, nil
}