-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathduration.go
155 lines (123 loc) · 3.33 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright (c) 2025 Tulir Asokan
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package jsontime
import (
"database/sql/driver"
"strconv"
"time"
)
func unmarshalDuration(into *time.Duration, jsonData []byte, unit time.Duration) error {
val, err := strconv.ParseInt(string(jsonData), 10, 64)
if err != nil {
return err
}
*into = time.Duration(val) * unit
return nil
}
func anyIntegerToDuration(src any, unit time.Duration, into *time.Duration) error {
i64, err := anyIntegerTo64(src)
if err != nil {
return err
}
*into = time.Duration(i64) * unit
return nil
}
type Seconds struct {
time.Duration
}
func S(dur time.Duration) Seconds {
return Seconds{Duration: dur}
}
func SInt(dur int) Seconds {
return Seconds{Duration: time.Duration(dur) * time.Second}
}
func (s Seconds) MarshalJSON() ([]byte, error) {
return []byte(strconv.FormatInt(int64(s.Seconds()), 10)), nil
}
func (s Seconds) Value() (driver.Value, error) {
return int64(s.Seconds()), nil
}
func (s *Seconds) UnmarshalJSON(data []byte) error {
return unmarshalDuration(&s.Duration, data, time.Second)
}
func (s *Seconds) Scan(src interface{}) error {
return anyIntegerToDuration(src, time.Second, &s.Duration)
}
func (s *Seconds) Get() time.Duration {
if s == nil {
return 0
}
return s.Duration
}
type Milliseconds struct {
time.Duration
}
func MS(dur time.Duration) Milliseconds {
return Milliseconds{Duration: dur}
}
func MSInt(dur int64) Milliseconds {
return Milliseconds{Duration: time.Duration(dur) * time.Millisecond}
}
func (s Milliseconds) MarshalJSON() ([]byte, error) {
return []byte(strconv.FormatInt(s.Milliseconds(), 10)), nil
}
func (s Milliseconds) Value() (driver.Value, error) {
return s.Milliseconds(), nil
}
func (s *Milliseconds) UnmarshalJSON(data []byte) error {
return unmarshalDuration(&s.Duration, data, time.Millisecond)
}
func (s *Milliseconds) Scan(src interface{}) error {
return anyIntegerToDuration(src, time.Millisecond, &s.Duration)
}
func (s *Milliseconds) Get() time.Duration {
if s == nil {
return 0
}
return s.Duration
}
type Microseconds struct {
time.Duration
}
func (s Microseconds) MarshalJSON() ([]byte, error) {
return []byte(strconv.FormatInt(s.Microseconds(), 10)), nil
}
func (s Microseconds) Value() (driver.Value, error) {
return s.Microseconds(), nil
}
func (s *Microseconds) UnmarshalJSON(data []byte) error {
return unmarshalDuration(&s.Duration, data, time.Microsecond)
}
func (s *Microseconds) Scan(src interface{}) error {
return anyIntegerToDuration(src, time.Microsecond, &s.Duration)
}
func (s *Microseconds) Get() time.Duration {
if s == nil {
return 0
}
return s.Duration
}
type Nanoseconds struct {
time.Duration
}
func (s Nanoseconds) MarshalJSON() ([]byte, error) {
return []byte(strconv.FormatInt(s.Nanoseconds(), 10)), nil
}
func (s Nanoseconds) Value() (driver.Value, error) {
return s.Nanoseconds(), nil
}
func (s *Nanoseconds) UnmarshalJSON(data []byte) error {
return unmarshalDuration(&s.Duration, data, time.Nanosecond)
}
func (s *Nanoseconds) Scan(src interface{}) error {
return anyIntegerToDuration(src, time.Nanosecond, &s.Duration)
}
func (s *Nanoseconds) Get() time.Duration {
if s == nil {
return 0
}
return s.Duration
}