Skip to content

Commit

Permalink
Merge pull request #102 from Kunde21/parse_location
Browse files Browse the repository at this point in the history
configure default location for time parsing
  • Loading branch information
casualjim committed Mar 16, 2023
2 parents a1049a1 + 7113cf9 commit a70f44a
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
6 changes: 3 additions & 3 deletions date.go
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
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
Expand Up @@ -92,6 +92,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 @@ -101,7 +104,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 a70f44a

Please sign in to comment.