-
Notifications
You must be signed in to change notification settings - Fork 0
/
xml_types.go
94 lines (75 loc) · 1.48 KB
/
xml_types.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 meldeschein
import (
"encoding/json"
"encoding/xml"
"time"
"github.com/pkg/errors"
)
type Date struct {
time.Time
}
func (d Date) MarshalSchema() string {
return d.Time.Format("2006-01-02")
}
func (d Date) IsEmpty() bool {
if d.Time.IsZero() {
return true
}
s := d.Time.Format("2006-01-02")
if s == "1899-12-30" {
return true
}
return false
}
func (d Date) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
if d.IsEmpty() {
return e.EncodeElement("", start)
}
return e.EncodeElement(d.Time.Format("2006-01-02"), start)
}
func (d *Date) UnmarshalXML(dec *xml.Decoder, start xml.StartElement) error {
var value string
err := dec.DecodeElement(&value, &start)
if err != nil {
return err
}
if value == "" {
return nil
}
// first try standard date
layout := time.RFC3339
d.Time, err = time.Parse(layout, value)
if err == nil {
return nil
}
layout = "2006-01-02"
d.Time, err = time.Parse(layout, value)
if err == nil {
return errors.WithStack(err)
}
return nil
}
func (d Date) MarshalJSON() ([]byte, error) {
// if d.Time.IsZero() {
// return []byte(`""`), nil
// }
return json.Marshal(d.Format("2006-01-02"))
}
func (d *Date) UnmarshalJSON(text []byte) (err error) {
var value string
err = json.Unmarshal(text, &value)
if err != nil {
return err
}
if value == "" {
return nil
}
// 28-1-2008
layout := "2006-01-02"
time, err := time.Parse(layout, value)
d.Time = time
return err
}
type DateTime struct {
time.Time
}