()
| 257 | } |
| 258 | |
| 259 | func (d Duration) String() string { |
| 260 | var ( |
| 261 | ms = int64(time.Duration(d) / time.Millisecond) |
| 262 | r = "" |
| 263 | ) |
| 264 | if ms == 0 { |
| 265 | return "0s" |
| 266 | } |
| 267 | |
| 268 | f := func(unit string, mult int64, exact bool) { |
| 269 | if exact && ms%mult != 0 { |
| 270 | return |
| 271 | } |
| 272 | if v := ms / mult; v > 0 { |
| 273 | r += fmt.Sprintf("%d%s", v, unit) |
| 274 | ms -= v * mult |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | // Only format years and weeks if the remainder is zero, as it is often |
| 279 | // easier to read 90d than 12w6d. |
| 280 | f("y", 1000*60*60*24*365, true) |
| 281 | f("w", 1000*60*60*24*7, true) |
| 282 | |
| 283 | f("d", 1000*60*60*24, false) |
| 284 | f("h", 1000*60*60, false) |
| 285 | f("m", 1000*60, false) |
| 286 | f("s", 1000, false) |
| 287 | f("ms", 1, false) |
| 288 | |
| 289 | return r |
| 290 | } |
| 291 | |
| 292 | // MarshalJSON implements the json.Marshaler interface. |
| 293 | func (d Duration) MarshalJSON() ([]byte, error) { |
no outgoing calls