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 implements fmt.Stringer
32 func (d Duration) String() string {
33 return time.Duration(d).String()
36 // Duration returns a time.Duration
37 func (d Duration) Duration() time.Duration {
38 return time.Duration(d)
41 // Set sets the current duration by parsing the string using time.ParseDuration
42 func (d *Duration) Set(s string) error {
43 dur, err := time.ParseDuration(s)
48 // DurationMapStructureDecodeHook can be used to create a decoder for arvados.duration when using mapstructure
49 func DurationMapStructureDecodeHook() interface{} {
50 return func(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error) {
52 if f.Kind() != reflect.String || t != reflect.TypeOf(duration) {
56 duration.Set(data.(string))