]> Cypherpunks repositories - gostls13.git/commitdiff
time: fix Time.Add
authorHector Chu <hectorchu@gmail.com>
Sat, 10 Dec 2011 21:55:38 +0000 (21:55 +0000)
committerHector Chu <hectorchu@gmail.com>
Sat, 10 Dec 2011 21:55:38 +0000 (21:55 +0000)
R=rsc, r
CC=golang-dev
https://golang.org/cl/5448121

src/pkg/time/time.go
src/pkg/time/time_test.go

index 4e9accfe581a5b69bb96aaee931c2b7235ab6987..9bd58aeb8a9ca4fc422a14ea55f6756c1b0407cd 100644 (file)
@@ -548,7 +548,7 @@ func (d Duration) Hours() float64 {
 func (t Time) Add(d Duration) Time {
        t.sec += int64(d / 1e9)
        t.nsec += int32(d % 1e9)
-       if t.nsec > 1e9 {
+       if t.nsec >= 1e9 {
                t.sec++
                t.nsec -= 1e9
        } else if t.nsec < 0 {
index 464e9bfa2c6affda66ee395b3500d0c29219b950..ada3625078db998f7cbc780bc2d14ee93309af6f 100644 (file)
@@ -655,6 +655,17 @@ func TestDaysIn(t *testing.T) {
        }
 }
 
+func TestAddToExactSecond(t *testing.T) {
+       // Add an amount to the current time to round it up to the next exact second.
+       // This test checks that the nsec field still lies within the range [0, 999999999].
+       t1 := Now()
+       t2 := t1.Add(Second - Duration(t1.Nanosecond()))
+       sec := (t1.Second() + 1) % 60
+       if t2.Second() != sec || t2.Nanosecond() != 0 {
+               t.Errorf("sec = %d, nsec = %d, want sec = %d, nsec = 0", t2.Second(), t2.Nanosecond(), sec)
+       }
+}
+
 func BenchmarkNow(b *testing.B) {
        for i := 0; i < b.N; i++ {
                Now()