AddMicros adds the microsecond delta to the provided time value. The reason this function is necessary even though time.Add(duration) exists is that time.Duration can only hold values up to ~290 years, because it stores duration at the nanosecond resolution. This function makes it possible to add mo
(t time.Time, d int64)
| 668 | // resolution. This function makes it possible to add more than 290 years to a time.Time, |
| 669 | // at the tradeoff of working on a microsecond resolution. |
| 670 | func AddMicros(t time.Time, d int64) time.Time { |
| 671 | negMult := time.Duration(1) |
| 672 | if d < 0 { |
| 673 | negMult = -1 |
| 674 | d = -d |
| 675 | } |
| 676 | const maxMicroDur = int64(maxTimeDuration / time.Microsecond) |
| 677 | for d > maxMicroDur { |
| 678 | const maxWholeNanoDur = time.Duration(maxMicroDur) * time.Microsecond |
| 679 | t = t.Add(negMult * maxWholeNanoDur) |
| 680 | d -= maxMicroDur |
| 681 | } |
| 682 | return t.Add(negMult * time.Duration(d) * time.Microsecond) |
| 683 | } |
| 684 | |
| 685 | // Truncate returns a new duration obtained from the first argument |
| 686 | // by discarding the portions at finer resolution than that given by the |