init push
This commit is contained in:
35
internal/shared/pkg/timezone/timezone.go
Normal file
35
internal/shared/pkg/timezone/timezone.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package timezone
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const Default = "UTC"
|
||||
|
||||
var (
|
||||
ErrEmpty = errors.New("timezone is required")
|
||||
ErrInvalid = errors.New("timezone must be a valid IANA timezone")
|
||||
)
|
||||
|
||||
func Normalize(name string) (string, error) {
|
||||
tz := strings.TrimSpace(name)
|
||||
if tz == "" {
|
||||
return "", ErrEmpty
|
||||
}
|
||||
|
||||
loc, err := time.LoadLocation(tz)
|
||||
if err != nil {
|
||||
return "", ErrInvalid
|
||||
}
|
||||
return loc.String(), nil
|
||||
}
|
||||
|
||||
func WithDefault(name string) string {
|
||||
tz := strings.TrimSpace(name)
|
||||
if tz == "" {
|
||||
return Default
|
||||
}
|
||||
return tz
|
||||
}
|
||||
Reference in New Issue
Block a user