-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
duration.go
54 lines (46 loc) · 1016 Bytes
/
duration.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package jsonx
import (
"database/sql/driver"
"encoding/json"
"errors"
"math"
"time"
)
// Duration is a JSON representation of the standard Duration type, until Go version 2 supports it under the hoods.
type Duration time.Duration
func (d Duration) MarshalJSON() ([]byte, error) {
return json.Marshal(time.Duration(d).String())
}
func (d *Duration) UnmarshalJSON(b []byte) error {
var v interface{}
if err := json.Unmarshal(b, &v); err != nil {
return err
}
switch value := v.(type) {
case float64:
*d = Duration(value)
return nil
case string:
v, err := time.ParseDuration(value)
if err != nil {
return err
}
*d = Duration(v)
return nil
default:
return errors.New("invalid duration")
}
}
func (d Duration) ToDuration() time.Duration {
return time.Duration(d)
}
func (d Duration) Value() (driver.Value, error) {
return int64(d), nil
}
// Set sets the value of duration in nanoseconds.
func (d *Duration) Set(v float64) {
if math.IsNaN(v) {
return
}
*d = Duration(v)
}