-
Notifications
You must be signed in to change notification settings - Fork 170
/
duration.go
55 lines (45 loc) · 1.1 KB
/
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
55
// Copyright (C) 2017 ScyllaDB
package duration
import (
"time"
"github.com/gocql/gocql"
)
// Duration adds marshalling to time.Duration.
type Duration time.Duration
// MarshalCQL implements gocql.Marshaler.
func (d Duration) MarshalCQL(info gocql.TypeInfo) ([]byte, error) {
return gocql.Marshal(info, int(time.Duration(d).Seconds()))
}
// UnmarshalCQL implements gocql.Unmarshaler.
func (d *Duration) UnmarshalCQL(info gocql.TypeInfo, data []byte) error {
var t time.Duration
if err := gocql.Unmarshal(info, data, &t); err != nil {
return err
}
*d = Duration(t * time.Second)
return nil
}
// MarshalText implements text.Marshaller.
func (d Duration) MarshalText() ([]byte, error) {
if d == 0 {
return []byte{}, nil
}
return []byte(d.String()), nil
}
// UnmarshalText implements text.Marshaller.
func (d *Duration) UnmarshalText(b []byte) error {
if len(b) == 0 {
*d = 0
return nil
}
t, err := ParseDuration(string(b))
if err != nil {
return err
}
*d = t
return nil
}
// Duration returns this duration as time.Duration.
func (d Duration) Duration() time.Duration {
return time.Duration(d)
}