Skip to content
This repository was archived by the owner on Feb 12, 2025. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions autorest/date/time.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package date

import (
"strings"
"time"
)

// Azure reports time in UTC but it doesn't include the 'Z' time zone suffix in some cases.
const (
rfc3339JSON = `"` + time.RFC3339Nano + `"`
rfc3339 = time.RFC3339Nano
azureUtcFormatJSON = `"2006-01-02T15:04:05.999999999"`
azureUtcFormat = "2006-01-02T15:04:05.999999999"
rfc3339JSON = `"` + time.RFC3339Nano + `"`
rfc3339 = time.RFC3339Nano
)

// Time defines a type similar to time.Time but assumes a layout of RFC3339 date-time (i.e.,
Expand Down Expand Up @@ -36,7 +40,12 @@ func (t Time) MarshalJSON() (json []byte, err error) {
// UnmarshalJSON reconstitutes the Time from a JSON string conforming to RFC3339 date-time
// (i.e., 2006-01-02T15:04:05Z).
func (t *Time) UnmarshalJSON(data []byte) (err error) {
t.Time, err = ParseTime(rfc3339JSON, string(data))
stringData := string(data)
timeFormat := azureUtcFormatJSON
if strings.IndexAny(stringData, "Zz") > -1 {
timeFormat = rfc3339JSON
}
t.Time, err = ParseTime(timeFormat, stringData)
return err
}

Expand All @@ -49,7 +58,12 @@ func (t Time) MarshalText() (text []byte, err error) {
// UnmarshalText reconstitutes a Time saved as a byte array conforming to RFC3339 date-time
// (i.e., 2006-01-02T15:04:05Z).
func (t *Time) UnmarshalText(data []byte) (err error) {
t.Time, err = ParseTime(rfc3339, string(data))
stringData := string(data)
timeFormat := azureUtcFormat
if strings.IndexAny(stringData, "Zz") > -1 {
timeFormat = rfc3339
}
t.Time, err = ParseTime(timeFormat, stringData)
return err
}

Expand Down