Skip to content

feat: add StringToTimeLocationHookFunc to convert strings to *time.Location #117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 15, 2025
Merged
Show file tree
Hide file tree
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
20 changes: 20 additions & 0 deletions decode_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,26 @@ func StringToTimeDurationHookFunc() DecodeHookFunc {
}
}

// StringToTimeLocationHookFunc returns a DecodeHookFunc that converts
// strings to *time.Location.
func StringToTimeLocationHookFunc() DecodeHookFunc {
return func(
f reflect.Type,
t reflect.Type,
data any,
) (any, error) {
if f.Kind() != reflect.String {
return data, nil
}
if t != reflect.TypeOf(time.Local) {
return data, nil
}
d, err := time.LoadLocation(data.(string))

return d, wrapTimeParseLocationError(err)
}
}

// StringToURLHookFunc returns a DecodeHookFunc that converts
// strings to *url.URL.
func StringToURLHookFunc() DecodeHookFunc {
Expand Down
29 changes: 29 additions & 0 deletions decode_hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,35 @@ func TestStringToTimeDurationHookFunc(t *testing.T) {
suite.Run(t)
}

func TestStringToTimeLocationHookFunc(t *testing.T) {
newYork, _ := time.LoadLocation("America/New_York")
london, _ := time.LoadLocation("Europe/London")
tehran, _ := time.LoadLocation("Asia/Tehran")
shanghai, _ := time.LoadLocation("Asia/Shanghai")

suite := decodeHookTestSuite[string, *time.Location]{
fn: StringToTimeLocationHookFunc(),
ok: []decodeHookTestCase[string, *time.Location]{
{"UTC", time.UTC},
{"Local", time.Local},
{"America/New_York", newYork},
{"Europe/London", london},
{"Asia/Tehran", tehran},
{"Asia/Shanghai", shanghai},
},
fail: []decodeHookFailureTestCase[string, *time.Location]{
{"UTC2"}, // Non-existent
{"5s"}, // Duration-like, not a zone
{"Europe\\London"}, // Invalid path separator
{"../etc/passwd"}, // Unsafe path
{"/etc/zoneinfo"}, // Absolute path (rejected by stdlib)
{"Asia\\Tehran"}, // Invalid Windows-style path
},
}

suite.Run(t)
}

func TestStringToURLHookFunc(t *testing.T) {
httpURL, _ := url.Parse("http://example.com")
httpsURL, _ := url.Parse("https://example.com")
Expand Down
12 changes: 12 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,15 @@ func wrapTimeParseDurationError(err error) error {

return err
}

func wrapTimeParseLocationError(err error) error {
if err == nil {
return nil
}
errMsg := err.Error()
if strings.Contains(errMsg, "unknown time zone") || strings.HasPrefix(errMsg, "time: unknown format") {
return fmt.Errorf("invalid time zone format: %w", err)
}

return err
}
Loading