-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatetime_test.go
108 lines (92 loc) · 3.56 KB
/
datetime_test.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package xsd_test
import (
"encoding/xml"
"fmt"
"testing"
"time"
"github.com/gosoap/xsd"
"github.com/stretchr/testify/assert"
)
type xsdDateTime struct {
XMLName xml.Name `xml:"TS"`
D xsd.DateTime `xml:"d,attr"`
}
func ExampleDateTime() {
type person struct {
XMLName xml.Name `xml:"person"`
Born xsd.DateTime `xml:"born,attr"`
Name string `xml:",chardata"`
}
ts := person{Name: "John Doe", Born: *xsd.NewDateTime(2020, time.June, 22, 12, 0, 0, time.UTC)}
b, _ := xml.Marshal(ts)
fmt.Println(string(b))
// Output:
// <person born="2020-06-22T12:00:00Z">John Doe</person>
}
func TestDateTime_Marshal(t *testing.T) {
ts := xsdDateTime{D: *xsd.NewDateTime(2020, time.June, 22, 12, 0, 0, time.UTC)}
b, err := xml.Marshal(ts)
assert.Nil(t, err)
assert.Equal(t, `<TS d="2020-06-22T12:00:00Z"></TS>`, fmt.Sprintf("%s", b))
ts = xsdDateTime{D: *xsd.NewDateTime(-2020, time.June, 22, 12, 0, 0, time.UTC)}
b, err = xml.Marshal(ts)
assert.Nil(t, err)
assert.Equal(t, `<TS d="-2020-06-22T12:00:00Z"></TS>`, fmt.Sprintf("%s", b))
}
func TestDateTime_Unmarshal(t *testing.T) {
ts := new(xsdDateTime)
err := xml.Unmarshal([]byte(`<TS d="2001-10-26T21:32:52"></TS>`), ts)
assert.Nil(t, err)
assert.Equal(t, 2001, ts.D.Time().Year())
assert.Equal(t, time.October, ts.D.Time().Month())
assert.Equal(t, 26, ts.D.Time().Day())
assert.Equal(t, 21, ts.D.Time().Hour())
assert.Equal(t, 32, ts.D.Time().Minute())
assert.Equal(t, 52, ts.D.Time().Second())
assert.Equal(t, "UTC", ts.D.Time().Location().String())
err = xml.Unmarshal([]byte(`<TS d="2001-10-26T21:32:52+02:00"></TS>`), ts)
assert.Nil(t, err)
assert.Equal(t, 2001, ts.D.Time().Year())
assert.Equal(t, time.October, ts.D.Time().Month())
assert.Equal(t, 26, ts.D.Time().Day())
assert.Equal(t, 21, ts.D.Time().Hour())
assert.Equal(t, 32, ts.D.Time().Minute())
assert.Equal(t, 52, ts.D.Time().Second())
assert.NotEqual(t, "UTC", ts.D.Time().Location().String())
err = xml.Unmarshal([]byte(`<TS d="2001-10-26T19:32:52Z"></TS>`), ts)
assert.Nil(t, err)
assert.Equal(t, 2001, ts.D.Time().Year())
assert.Equal(t, time.October, ts.D.Time().Month())
assert.Equal(t, 26, ts.D.Time().Day())
assert.Equal(t, 19, ts.D.Time().Hour())
assert.Equal(t, 32, ts.D.Time().Minute())
assert.Equal(t, 52, ts.D.Time().Second())
assert.Equal(t, "UTC", ts.D.Time().Location().String())
err = xml.Unmarshal([]byte(`<TS d="2001-10-26T19:32:52+00:00"></TS>`), ts)
assert.Nil(t, err)
assert.Equal(t, 2001, ts.D.Time().Year())
assert.Equal(t, time.October, ts.D.Time().Month())
assert.Equal(t, 26, ts.D.Time().Day())
assert.Equal(t, 19, ts.D.Time().Hour())
assert.Equal(t, 32, ts.D.Time().Minute())
assert.Equal(t, 52, ts.D.Time().Second())
assert.NotEqual(t, "UTC", ts.D.Time().Location().String())
err = xml.Unmarshal([]byte(`<TS d="2001-10-26T21:32:52.12679"></TS>`), ts)
assert.Nil(t, err)
assert.Equal(t, 2001, ts.D.Time().Year())
assert.Equal(t, time.October, ts.D.Time().Month())
assert.Equal(t, 26, ts.D.Time().Day())
assert.Equal(t, 21, ts.D.Time().Hour())
assert.Equal(t, 32, ts.D.Time().Minute())
assert.Equal(t, 52, ts.D.Time().Second())
assert.Equal(t, 126790000, ts.D.Time().Nanosecond())
assert.Equal(t, "UTC", ts.D.Time().Location().String())
err = xml.Unmarshal([]byte(`<TS d="2001-10-26"></TS>`), ts)
assert.NotNil(t, err)
err = xml.Unmarshal([]byte(`<TS d="2001-10-32T21:32"></TS>`), ts)
assert.NotNil(t, err)
err = xml.Unmarshal([]byte(`<TS d="2001-10-26T25:32:52+02:00"></TS>`), ts)
assert.NotNil(t, err)
err = xml.Unmarshal([]byte(`<TS d="01-10-26T21:32"></TS>`), ts)
assert.NotNil(t, err)
}