-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
format.go
164 lines (143 loc) · 3.56 KB
/
format.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
156
157
158
159
160
161
162
163
164
package hdur
import (
"database/sql/driver"
"encoding/json"
"fmt"
"strings"
)
// Format returns a string representation of the duration using the given format
// Format specifiers:
// %y - years
// %M - months
// %d - days
// %h - hours
// %m - minutes
// %s - seconds
// %f - fractional seconds (without leading dot)
func (d Duration) Format(format string) string {
d.normalize()
// Handle fractional seconds specially to avoid double dots
format = strings.ReplaceAll(format, "%s.%f", fmt.Sprintf("%d.%09d", d.Seconds, d.Nanos))
replacer := strings.NewReplacer(
"%y", fmt.Sprintf("%d", d.Years),
"%M", fmt.Sprintf("%d", d.Months),
"%d", fmt.Sprintf("%d", d.Days),
"%h", fmt.Sprintf("%d", d.Hours),
"%m", fmt.Sprintf("%d", d.Minutes),
"%s", fmt.Sprintf("%d", d.Seconds),
"%f", fmt.Sprintf("%09d", d.Nanos),
)
return replacer.Replace(format)
}
// abs returns the absolute value of the Duration
func (d Duration) abs() Duration {
return Duration{
Years: abs(int(d.Years)),
Months: abs(int(d.Months)),
Days: abs(int(d.Days)),
Hours: abs(int(d.Hours)),
Minutes: abs(int(d.Minutes)),
Seconds: abs(int(d.Seconds)),
Nanos: abs(int(d.Nanos)),
}
}
// formatMainUnits formats years through seconds
func (d Duration) formatMainUnits() []string {
parts := []string{}
if d.Years > 0 {
parts = append(parts, fmt.Sprintf("%dy", d.Years))
}
if d.Months > 0 {
parts = append(parts, fmt.Sprintf("%dmo", d.Months))
}
if d.Days > 0 {
parts = append(parts, fmt.Sprintf("%dd", d.Days))
}
if d.Hours > 0 {
parts = append(parts, fmt.Sprintf("%dh", d.Hours))
}
if d.Minutes > 0 {
parts = append(parts, fmt.Sprintf("%dm", d.Minutes))
}
if d.Seconds > 0 || (d.Nanos > 0 && d.Nanos >= 1000000000) {
parts = append(parts, fmt.Sprintf("%ds", d.Seconds))
}
return parts
}
// formatNanos formats sub-second units
func (d Duration) formatNanos() string {
nanos := d.Nanos
if nanos == 0 {
return ""
}
switch {
case nanos >= 1000000:
if nanos%1000000 == 0 {
return fmt.Sprintf("%dms", nanos/1000000)
}
return fmt.Sprintf("%.3fms", float64(nanos)/1000000.0)
case nanos >= 1000:
if nanos%1000 == 0 {
return fmt.Sprintf("%dµs", nanos/1000)
}
return fmt.Sprintf("%.3fµs", float64(nanos)/1000.0)
default:
return fmt.Sprintf("%dns", nanos)
}
}
// String returns a human-readable representation of the duration
func (d Duration) String() string {
if d.IsZero() {
return "0s"
}
isNegative := d.isNegativeDuration()
temp := d
if isNegative {
temp = temp.abs()
}
parts := temp.formatMainUnits()
if nanos := temp.formatNanos(); nanos != "" {
parts = append(parts, nanos)
}
result := strings.Join(parts, " ")
if isNegative {
return "-" + result
}
return result
}
// MarshalJSON implements json.Marshaler
func (d Duration) MarshalJSON() ([]byte, error) {
return []byte(`"` + d.String() + `"`), nil
}
// UnmarshalJSON implements json.Unmarshaler
func (d *Duration) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
parsed, err := ParseDuration(s)
if err != nil {
return err
}
*d = parsed
return nil
}
// Value implements the driver.Valuer interface
func (d Duration) Value() (driver.Value, error) {
return d.String(), nil
}
// Scan implements the sql.Scanner interface
func (d *Duration) Scan(value interface{}) error {
var err error
switch v := value.(type) {
case []byte:
*d, err = ParseDuration(string(v))
case string:
*d, err = ParseDuration(v)
case nil:
*d = Duration{}
default:
err = fmt.Errorf("cannot scan type %T into Duration", value)
}
return err
}