1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
15 // Duration is time.Duration but looks like "12s" in JSON, rather than
16 // a number of nanoseconds.
17 type Duration time.Duration
19 // UnmarshalJSON implements json.Unmarshaler.
20 func (d *Duration) UnmarshalJSON(data []byte) error {
21 if bytes.Equal(data, []byte(`"0"`)) || bytes.Equal(data, []byte(`0`)) {
22 // Unitless 0 is not accepted by ParseDuration, but we
23 // accept it as a reasonable spelling of 0
29 return d.Set(string(data[1 : len(data)-1]))
31 // Mimic error message returned by ParseDuration for a number
33 return fmt.Errorf("missing unit in duration %q", data)
36 // MarshalJSON implements json.Marshaler.
37 func (d Duration) MarshalJSON() ([]byte, error) {
38 return json.Marshal(d.String())
41 // String returns a format similar to (time.Duration)String() but with
42 // "0m" and "0s" removed: e.g., "1h" instead of "1h0m0s".
43 func (d Duration) String() string {
44 s := time.Duration(d).String()
45 s = strings.Replace(s, "m0s", "m", 1)
46 s = strings.Replace(s, "h0m", "h", 1)
50 // Duration returns a time.Duration.
51 func (d Duration) Duration() time.Duration {
52 return time.Duration(d)
55 // Set implements the flag.Value interface and sets the duration value by using time.ParseDuration to parse the string.
56 func (d *Duration) Set(s string) error {
57 dur, err := time.ParseDuration(s)