Skip to content

Commit

Permalink
configure default location for time parsing
Browse files Browse the repository at this point in the history
Interacting with servers that utilize the ISO-8601 Local Time variants in
request and response payloads can configure the request marshaling using
NormalizeTimeForMarshal setting, but don't have a ready method for setting the
local time zone when parsing the response fields.

Keep UTC as the default location, to maintain the previous functionality.

Signed-off-by: Chad Kunde <Kunde21@gmail.com>
  • Loading branch information
Kunde21 committed Jul 3, 2022
1 parent 8ad3739 commit 7113cf9
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
6 changes: 3 additions & 3 deletions date.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (d *Date) UnmarshalText(text []byte) error {
if len(text) == 0 {
return nil
}
dd, err := time.Parse(RFC3339FullDate, string(text))
dd, err := time.ParseInLocation(RFC3339FullDate, string(text), DefaultTimeLocation)
if err != nil {
return err
}
Expand Down Expand Up @@ -107,7 +107,7 @@ func (d *Date) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(data, &strdate); err != nil {
return err
}
tt, err := time.Parse(RFC3339FullDate, strdate)
tt, err := time.ParseInLocation(RFC3339FullDate, strdate, DefaultTimeLocation)
if err != nil {
return err
}
Expand All @@ -126,7 +126,7 @@ func (d *Date) UnmarshalBSON(data []byte) error {
}

if data, ok := m["data"].(string); ok {
rd, err := time.Parse(RFC3339FullDate, data)
rd, err := time.ParseInLocation(RFC3339FullDate, data, DefaultTimeLocation)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion format.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (f *defaultFormats) MapStructureHookFunc() mapstructure.DecodeHookFunc { //
if to == tpe {
switch v.Name {
case "date":
d, err := time.Parse(RFC3339FullDate, data)
d, err := time.ParseInLocation(RFC3339FullDate, data, DefaultTimeLocation)
if err != nil {
return nil, err
}
Expand Down
5 changes: 4 additions & 1 deletion time.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ var (
// NormalizeTimeForMarshal provides a normalization function on time befeore marshalling (e.g. time.UTC).
// By default, the time value is not changed.
NormalizeTimeForMarshal = func(t time.Time) time.Time { return t }

// DefaultTimeLocation provides a location for a time when the time zone is not encoded in the string (ex: ISO8601 Local variants).
DefaultTimeLocation = time.UTC
)

// ParseDateTime parses a string that represents an ISO8601 time or a unix epoch
Expand All @@ -95,7 +98,7 @@ func ParseDateTime(data string) (DateTime, error) {
}
var lastError error
for _, layout := range DateTimeFormats {
dd, err := time.Parse(layout, data)
dd, err := time.ParseInLocation(layout, data, DefaultTimeLocation)
if err != nil {
lastError = err
continue
Expand Down

0 comments on commit 7113cf9

Please sign in to comment.