From 9b3bccd1f15584639b128b3b942bdfe990eee95a Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Mon, 29 May 2023 13:59:30 +0300 Subject: [PATCH] util/time: ParseDuration fix Signed-off-by: Vasiliy Tolstov --- util/time/duration.go | 3 +-- util/time/duration_test.go | 10 +++++++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/util/time/duration.go b/util/time/duration.go index 456c6bad..36ba97a1 100644 --- a/util/time/duration.go +++ b/util/time/duration.go @@ -21,7 +21,6 @@ loop: for i, r := range s { switch r { case 's', 'm': - p = i break loop case 'h': d, err := strconv.Atoi(s[p:i]) @@ -68,7 +67,7 @@ func (d *Duration) UnmarshalJSON(b []byte) error { *d = Duration(time.Duration(value)) return nil case string: - dv, err := time.ParseDuration(value) + dv, err := ParseDuration(value) if err != nil { return err } diff --git a/util/time/duration_test.go b/util/time/duration_test.go index d538b449..80b7289c 100644 --- a/util/time/duration_test.go +++ b/util/time/duration_test.go @@ -23,13 +23,21 @@ func TestUnmarshalJSON(t *testing.T) { TTL Duration `json:"ttl"` } v := &str{} + var err error - err := json.Unmarshal([]byte(`{"ttl":"10ms"}`), v) + err = json.Unmarshal([]byte(`{"ttl":"10ms"}`), v) if err != nil { t.Fatal(err) } else if v.TTL != 10000000 { 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) { -- 2.45.2