diff --git a/model/time.go b/model/time.go index 7b0064fd..490a0240 100644 --- a/model/time.go +++ b/model/time.go @@ -186,6 +186,10 @@ var durationRE = regexp.MustCompile("^([0-9]+)(y|w|d|h|m|s|ms)$") // ParseDuration parses a string into a time.Duration, assuming that a year // always has 365d, a week always has 7d, and a day always has 24h. func ParseDuration(durationStr string) (Duration, error) { + // Allow 0 without a unit. + if durationStr == "0" { + return 0, nil + } matches := durationRE.FindStringSubmatch(durationStr) if len(matches) != 3 { return 0, fmt.Errorf("not a valid duration string: %q", durationStr) diff --git a/model/time_test.go b/model/time_test.go index 315bc4e5..330cc6e4 100644 --- a/model/time_test.go +++ b/model/time_test.go @@ -90,8 +90,14 @@ func TestParseDuration(t *testing.T) { var cases = []struct { in string out time.Duration + + expectedString string }{ { + in: "0", + out: 0, + expectedString: "0s", + }, { in: "0s", out: 0, }, { @@ -126,7 +132,11 @@ func TestParseDuration(t *testing.T) { if time.Duration(d) != c.out { t.Errorf("Expected %v but got %v", c.out, d) } - if d.String() != c.in { + expectedString := c.expectedString + if c.expectedString == "" { + expectedString = c.in + } + if d.String() != expectedString { t.Errorf("Expected duration string %q but got %q", c.in, d.String()) } }