()
| 292 | } |
| 293 | |
| 294 | func (d Duration) String() string { |
| 295 | var ( |
| 296 | ms = int64(time.Duration(d) / time.Millisecond) |
| 297 | r = "" |
| 298 | sign = "" |
| 299 | ) |
| 300 | |
| 301 | if ms == 0 { |
| 302 | return "0s" |
| 303 | } |
| 304 | |
| 305 | if ms < 0 { |
| 306 | sign, ms = "-", -ms |
| 307 | } |
| 308 | |
| 309 | f := func(unit string, mult int64, exact bool) { |
| 310 | if exact && ms%mult != 0 { |
| 311 | return |
| 312 | } |
| 313 | if v := ms / mult; v > 0 { |
| 314 | r += fmt.Sprintf("%d%s", v, unit) |
| 315 | ms -= v * mult |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | // Only format years and weeks if the remainder is zero, as it is often |
| 320 | // easier to read 90d than 12w6d. |
| 321 | f("y", 1000*60*60*24*365, true) |
| 322 | f("w", 1000*60*60*24*7, true) |
| 323 | |
| 324 | f("d", 1000*60*60*24, false) |
| 325 | f("h", 1000*60*60, false) |
| 326 | f("m", 1000*60, false) |
| 327 | f("s", 1000, false) |
| 328 | f("ms", 1, false) |
| 329 | |
| 330 | return sign + r |
| 331 | } |
| 332 | |
| 333 | // MarshalJSON implements the json.Marshaler interface. |
| 334 | func (d Duration) MarshalJSON() ([]byte, error) { |
no outgoing calls