1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
14 // Duration is time.Duration but looks like "12s" in JSON, rather than
15 // a number of nanoseconds.
16 type Duration time.Duration
18 // UnmarshalJSON implements json.Unmarshaler.
19 func (d *Duration) UnmarshalJSON(data []byte) error {
21 return d.Set(string(data[1 : len(data)-1]))
23 return fmt.Errorf("duration must be given as a string like \"600s\" or \"1h30m\"")
26 // MarshalJSON implements json.Marshaler.
27 func (d Duration) MarshalJSON() ([]byte, error) {
28 return json.Marshal(d.String())
31 // String returns a format similar to (time.Duration)String() but with
32 // "0m" and "0s" removed: e.g., "1h" instead of "1h0m0s".
33 func (d Duration) String() string {
34 s := time.Duration(d).String()
35 s = strings.Replace(s, "m0s", "m", 1)
36 s = strings.Replace(s, "h0m", "h", 1)
40 // Duration returns a time.Duration.
41 func (d Duration) Duration() time.Duration {
42 return time.Duration(d)
45 // Set implements the flag.Value interface and sets the duration value by using time.ParseDuration to parse the string.
46 func (d *Duration) Set(s string) error {
47 dur, err := time.ParseDuration(s)