Merge branch '8784-dir-listings'
[arvados.git] / sdk / go / stats / duration.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: Apache-2.0
4
5 package stats
6
7 import (
8         "fmt"
9         "strconv"
10         "time"
11 )
12
13 // Duration is a duration that is displayed as a number of seconds in
14 // fixed-point notation.
15 type Duration time.Duration
16
17 // MarshalJSON implements json.Marshaler.
18 func (d Duration) MarshalJSON() ([]byte, error) {
19         return []byte(d.String()), nil
20 }
21
22 // String implements fmt.Stringer.
23 func (d Duration) String() string {
24         return fmt.Sprintf("%.6f", time.Duration(d).Seconds())
25 }
26
27 // UnmarshalJSON implements json.Unmarshaler
28 func (d *Duration) UnmarshalJSON(data []byte) error {
29         return d.Set(string(data))
30 }
31
32 // Value implements flag.Value
33 func (d *Duration) Set(s string) error {
34         sec, err := strconv.ParseFloat(s, 64)
35         if err == nil {
36                 *d = Duration(sec * float64(time.Second))
37         }
38         return err
39 }