Merge pull request 'util/time: ParseDuration fix' (#222) from timefix into v3

Reviewed-on: #222
This commit is contained in:
Василий Толстов 2023-05-29 14:04:41 +03:00
commit 75173560e3
2 changed files with 10 additions and 3 deletions

View File

@ -21,7 +21,6 @@ loop:
for i, r := range s { for i, r := range s {
switch r { switch r {
case 's', 'm': case 's', 'm':
p = i
break loop break loop
case 'h': case 'h':
d, err := strconv.Atoi(s[p:i]) d, err := strconv.Atoi(s[p:i])
@ -68,7 +67,7 @@ func (d *Duration) UnmarshalJSON(b []byte) error {
*d = Duration(time.Duration(value)) *d = Duration(time.Duration(value))
return nil return nil
case string: case string:
dv, err := time.ParseDuration(value) dv, err := ParseDuration(value)
if err != nil { if err != nil {
return err return err
} }

View File

@ -23,13 +23,21 @@ func TestUnmarshalJSON(t *testing.T) {
TTL Duration `json:"ttl"` TTL Duration `json:"ttl"`
} }
v := &str{} v := &str{}
var err error
err := json.Unmarshal([]byte(`{"ttl":"10ms"}`), v) err = json.Unmarshal([]byte(`{"ttl":"10ms"}`), v)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} else if v.TTL != 10000000 { } else if v.TTL != 10000000 {
t.Fatalf("invalid duration %v != 10000000", v.TTL) t.Fatalf("invalid duration %v != 10000000", v.TTL)
} }
err = json.Unmarshal([]byte(`{"ttl":"1y"}`), v)
if err != nil {
t.Fatal(err)
} else if v.TTL != 31536000000000000 {
t.Fatalf("invalid duration %v != 31536000000000000", v.TTL)
}
} }
func TestParseDuration(t *testing.T) { func TestParseDuration(t *testing.T) {