Encode returns three integers such that the original Duration is recoverable (using Decode) and the first int will approximately sort a collection of encoded Durations.
()
| 370 | // (using Decode) and the first int will approximately sort a collection of |
| 371 | // encoded Durations. |
| 372 | func (d Duration) Encode() (sortNanos int64, months int64, days int64, err error) { |
| 373 | // The number of whole years equivalent to any value of Duration always fits |
| 374 | // in an int64. Use this to compute a conservative estimate of overflow. |
| 375 | // |
| 376 | // TODO(dan): Compute overflow exactly, then document that EncodeBigInt can be |
| 377 | // used in overflow cases. |
| 378 | years := d.Months/12 + d.Days/DaysPerMonth/12 + d.nanos/nanosInMonth/12 |
| 379 | if years > maxYearsInDuration || years < minYearsInDuration { |
| 380 | return 0, 0, 0, errEncodeOverflow |
| 381 | } |
| 382 | |
| 383 | totalNanos := d.Months*nanosInMonth + d.Days*nanosInDay + d.nanos |
| 384 | return totalNanos, d.Months, d.Days, nil |
| 385 | } |
| 386 | |
| 387 | // EncodeBigInt is the same as Encode, except that it always returns |
| 388 | // successfully and is slower. |
no outgoing calls
no test coverage detected