Skip to content

Commit

Permalink
add ParseTime
Browse files Browse the repository at this point in the history
  • Loading branch information
rhino1998 committed Sep 24, 2024
1 parent 3111bb1 commit 3c5a1ac
Showing 1 changed file with 27 additions and 20 deletions.
47 changes: 27 additions & 20 deletions configtype/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,30 @@ func NewRelativeTime(d time.Duration) Time {
}
}

func ParseTime(s string) (Time, error) {
var t Time
var err error
switch {
case timeRFC3339Regexp.MatchString(s):
t.time, err = time.Parse(time.RFC3339, s)
if t.time.IsZero() {
t.typ = timeTypeZero
} else {
t.typ = timeTypeFixed
}
case dateRegexp.MatchString(s):
t.typ = timeTypeFixed
t.time, err = time.Parse(time.DateOnly, s)
case durationRegexp.MatchString(s):
t.typ = timeTypeRelative
t.duration, err = time.ParseDuration(s)
default:
return t, fmt.Errorf("invalid time format: %s", s)
}

return t, err
}

var (
timeRFC3339Pattern = `^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(.(\d{1,9}))?(Z|((-|\+)\d{2}:\d{2}))$`
timeRFC3339Regexp = regexp.MustCompile(timeRFC3339Pattern)
Expand All @@ -60,29 +84,12 @@ func (Time) JSONSchema() *jsonschema.Schema {

func (t *Time) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
err := json.Unmarshal(b, &s)
if err != nil {
return err
}

var err error
switch {
case timeRFC3339Regexp.MatchString(s):
t.time, err = time.Parse(time.RFC3339, s)
if t.time.IsZero() {
t.typ = timeTypeZero
} else {
t.typ = timeTypeFixed
}
case dateRegexp.MatchString(s):
t.typ = timeTypeFixed
t.time, err = time.Parse(time.DateOnly, s)
case durationRegexp.MatchString(s):
t.typ = timeTypeRelative
t.duration, err = time.ParseDuration(s)
default:
return fmt.Errorf("invalid time format: %s", s)
}

*t, err = ParseTime(s)
if err != nil {
return err
}
Expand Down

0 comments on commit 3c5a1ac

Please sign in to comment.