forked from hooklift/gowsdl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xsdDateTime.go
357 lines (321 loc) · 9.07 KB
/
xsdDateTime.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package soap
import (
"encoding/xml"
"strings"
"time"
)
const (
dateLayout = "2006-01-02Z07:00"
timeLayout = "15:04:05.999999999Z07:00"
)
//
// DateTime struct
//
// XSDDateTime is a type for representing xsd:datetime in Golang
type XSDDateTime struct {
innerTime time.Time
hasTz bool
}
// StripTz removes TZ information from the datetime
func (xdt *XSDDateTime) StripTz() {
xdt.hasTz = false
}
// ToGoTime converts the time to time.Time by checking if a TZ is specified.
// If there is a TZ, that TZ is used, otherwise local TZ is used
func (xdt *XSDDateTime) ToGoTime() time.Time {
if xdt.hasTz {
return xdt.innerTime
}
return time.Date(xdt.innerTime.Year(), xdt.innerTime.Month(), xdt.innerTime.Day(),
xdt.innerTime.Hour(), xdt.innerTime.Minute(), xdt.innerTime.Second(),
xdt.innerTime.Nanosecond(), time.Local)
}
// MarshalXML implements xml.MarshalerAttr on XSDDateTime
func (xdt XSDDateTime) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
xdtString := xdt.string()
if xdtString != "" {
return e.EncodeElement(xdtString, start)
}
return nil
}
// MarshalXMLAttr implements xml.MarshalerAttr on XSDDateTime
func (xdt XSDDateTime) MarshalXMLAttr(name xml.Name) (xml.Attr, error) {
xdtString := xdt.string()
attr := xml.Attr{}
if xdtString != "" {
attr.Name = name
attr.Value = xdtString
}
return attr, nil
}
// returns string representation and skips "zero" time values. It also checks if nanoseconds and TZ exist.
func (xdt XSDDateTime) string() string {
if !xdt.innerTime.IsZero() {
dateTimeLayout := time.RFC3339Nano
if xdt.innerTime.Nanosecond() == 0 {
dateTimeLayout = time.RFC3339
}
dtString := xdt.innerTime.Format(dateTimeLayout)
if !xdt.hasTz {
// split off time portion
dateAndTime := strings.SplitN(dtString, "T", 2)
toks := strings.SplitN(dateAndTime[1], "Z", 2)
toks = strings.SplitN(toks[0], "+", 2)
toks = strings.SplitN(toks[0], "-", 2)
dtString = dateAndTime[0] + "T" + toks[0]
}
return dtString
}
return ""
}
// UnmarshalXML implements xml.Unmarshaler on XSDDateTime to use time.RFC3339Nano
func (xdt *XSDDateTime) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var content string
err := d.DecodeElement(&content, &start)
if err != nil {
return err
}
xdt.innerTime, xdt.hasTz, err = fromString(content, time.RFC3339Nano)
return err
}
// UnmarshalXMLAttr implements xml.UnmarshalerAttr on XSDDateTime to use time.RFC3339Nano
func (xdt *XSDDateTime) UnmarshalXMLAttr(attr xml.Attr) error {
var err error
xdt.innerTime, xdt.hasTz, err = fromString(attr.Value, time.RFC3339Nano)
return err
}
func fromString(content string, format string) (time.Time, bool, error) {
var t time.Time
if content == "" {
return t, true, nil
}
hasTz := false
if strings.Contains(content, "T") { // check if we have a time portion
// split into date and time portion
dateAndTime := strings.SplitN(content, "T", 2)
if len(dateAndTime) > 1 {
if strings.Contains(dateAndTime[1], "Z") ||
strings.Contains(dateAndTime[1], "+") ||
strings.Contains(dateAndTime[1], "-") {
hasTz = true
}
}
if !hasTz {
content += "Z"
}
if content == "0001-01-01T00:00:00Z" {
return t, true, nil
}
} else {
// we don't see to have a time portion, check timezone
if strings.Contains(content, "Z") ||
strings.Contains(content, ":") {
hasTz = true
}
if !hasTz {
content += "Z"
}
}
t, err := time.Parse(format, content)
return t, hasTz, err
}
// CreateXsdDateTime creates an object represent xsd:datetime object in Golang
func CreateXsdDateTime(dt time.Time, hasTz bool) XSDDateTime {
return XSDDateTime{
innerTime: dt,
hasTz: hasTz,
}
}
// XSDDate is a type for representing xsd:date in Golang
type XSDDate struct {
innerDate time.Time
hasTz bool
}
// StripTz removes the TZ information from the date
func (xd *XSDDate) StripTz() {
xd.hasTz = false
}
// ToGoTime converts the date to Golang time.Time by checking if a TZ is specified.
// If there is a TZ, that TZ is used, otherwise local TZ is used
func (xd *XSDDate) ToGoTime() time.Time {
if xd.hasTz {
return xd.innerDate
}
return time.Date(xd.innerDate.Year(), xd.innerDate.Month(), xd.innerDate.Day(),
0, 0, 0, 0, time.Local)
}
// MarshalXML implementation on XSDDate
func (xd XSDDate) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
xdtString := xd.string()
if xdtString != "" {
return e.EncodeElement(xdtString, start)
}
return nil
}
// MarshalXMLAttr implementation on XSDDate
func (xd XSDDate) MarshalXMLAttr(name xml.Name) (xml.Attr, error) {
xdString := xd.string()
attr := xml.Attr{}
if xdString != "" {
attr.Name = name
attr.Value = xdString
}
return attr, nil
}
// returns string representation and skips "zero" time values
func (xd XSDDate) string() string {
if !xd.innerDate.IsZero() {
dateString := xd.innerDate.Format(dateLayout) // serialize with TZ
if !xd.hasTz {
if strings.Contains(dateString, "Z") {
// UTC Tz
toks := strings.SplitN(dateString, "Z", 2)
dateString = toks[0]
} else {
// [+-]00:00 Tz, remove last 6 chars
if len(dateString) > 5 { // this should always be true
start := len(dateString) - 6 // locate at "-"
dateString = dateString[0:start]
}
}
}
return dateString
}
return ""
}
// UnmarshalXML implements xml.Unmarshaler on XSDDate to use dateLayout
func (xd *XSDDate) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var content string
err := d.DecodeElement(&content, &start)
if err != nil {
return err
}
xd.innerDate, xd.hasTz, err = fromString(content, dateLayout)
return err
}
// UnmarshalXMLAttr implements xml.UnmarshalerAttr on XSDDate to use dateLayout
func (xd *XSDDate) UnmarshalXMLAttr(attr xml.Attr) error {
var err error
xd.innerDate, xd.hasTz, err = fromString(attr.Value, dateLayout)
return err
}
// CreateXsdDate creates an object represent xsd:datetime object in Golang
func CreateXsdDate(date time.Time, hasTz bool) XSDDate {
return XSDDate{
innerDate: date,
hasTz: hasTz,
}
}
// XSDTime is a type for representing xsd:time
type XSDTime struct {
innerTime time.Time
hasTz bool
}
// MarshalXML implements xml.Marshaler on XSDTime
func (xt XSDTime) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
xdtString := xt.string()
if xdtString != "" {
return e.EncodeElement(xdtString, start)
}
return nil
}
// MarshalXMLAttr implements xml.MarshalerAttr on XSDTime
func (xt XSDTime) MarshalXMLAttr(name xml.Name) (xml.Attr, error) {
xdString := xt.string()
attr := xml.Attr{}
if xdString != "" {
attr.Name = name
attr.Value = xdString
}
return attr, nil
}
// returns string representation and skips "zero" time values
func (xt XSDTime) string() string {
if !xt.innerTime.IsZero() {
dateTimeLayout := time.RFC3339Nano
if xt.innerTime.Nanosecond() == 0 {
dateTimeLayout = time.RFC3339
}
// split off date portion
dateAndTime := strings.SplitN(xt.innerTime.Format(dateTimeLayout), "T", 2)
timeString := dateAndTime[1]
if !xt.hasTz {
toks := strings.SplitN(timeString, "Z", 2)
toks = strings.SplitN(toks[0], "+", 2)
toks = strings.SplitN(toks[0], "-", 2)
timeString = toks[0]
}
return timeString
}
return ""
}
// UnmarshalXML implements xml.Unmarshaler on XSDTime to use dateTimeLayout
func (xt *XSDTime) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var err error
var content string
err = d.DecodeElement(&content, &start)
if err != nil {
return err
}
return xt.fromString(content)
}
// UnmarshalXMLAttr implements xml.UnmarshalerAttr on XSDTime to use dateTimeLayout
func (xt *XSDTime) UnmarshalXMLAttr(attr xml.Attr) error {
return xt.fromString(attr.Value)
}
func (xt *XSDTime) fromString(content string) error {
var t time.Time
var err error
if content == "" {
xt.innerTime = t
return nil
}
xt.hasTz = false
if strings.Contains(content, "Z") ||
strings.Contains(content, "+") ||
strings.Contains(content, "-") {
xt.hasTz = true
}
if !xt.hasTz {
content += "Z"
}
xt.innerTime, err = time.Parse(timeLayout, content)
return err
}
// Hour returns hour of the xsd:time
func (xt XSDTime) Hour() int {
return xt.innerTime.Hour()
}
// Minute returns minutes of the xsd:time
func (xt XSDTime) Minute() int {
return xt.innerTime.Minute()
}
// Second returns seconds of the xsd:time
func (xt XSDTime) Second() int {
return xt.innerTime.Second()
}
// Nanosecond returns nanosecond of the xsd:time
func (xt XSDTime) Nanosecond() int {
return xt.innerTime.Nanosecond()
}
// Location returns the TZ information of the xsd:time
func (xt XSDTime) Location() *time.Location {
if xt.hasTz {
return xt.innerTime.Location()
}
return nil
}
// CreateXsdTime creates an object representing xsd:time in Golang
func CreateXsdTime(hour int, min int, sec int, nsec int, loc *time.Location) XSDTime {
realLoc := loc
if loc == nil {
realLoc = time.Local
}
return XSDTime{
innerTime: time.Date(1951, 10, 22, hour, min, sec, nsec, realLoc),
hasTz: loc != nil,
}
}