Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(1046)

Side by Side Diff: src/pkg/time/time.go

Issue 10328043: code review 10328043: time: handle integer overflow in Sub (Closed)
Patch Set: diff -r 0b9e3725cc84 https://code.google.com/p/go Created 10 years, 9 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | src/pkg/time/time_test.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2009 The Go Authors. All rights reserved. 1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style 2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file. 3 // license that can be found in the LICENSE file.
4 4
5 // Package time provides functionality for measuring and displaying time. 5 // Package time provides functionality for measuring and displaying time.
6 // 6 //
7 // The calendrical calculations always assume a Gregorian calendar. 7 // The calendrical calculations always assume a Gregorian calendar.
8 package time 8 package time
9 9
10 import "errors" 10 import "errors"
(...skipping 406 matching lines...) Expand 10 before | Expand all | Expand 10 after
417 func (t Time) YearDay() int { 417 func (t Time) YearDay() int {
418 _, _, _, yday := t.date(false) 418 _, _, _, yday := t.date(false)
419 return yday + 1 419 return yday + 1
420 } 420 }
421 421
422 // A Duration represents the elapsed time between two instants 422 // A Duration represents the elapsed time between two instants
423 // as an int64 nanosecond count. The representation limits the 423 // as an int64 nanosecond count. The representation limits the
424 // largest representable duration to approximately 290 years. 424 // largest representable duration to approximately 290 years.
425 type Duration int64 425 type Duration int64
426 426
427 const (
428 minDuration Duration = -1 << 63
429 maxDuration Duration = 1<<63 - 1
430 )
431
427 // Common durations. There is no definition for units of Day or larger 432 // Common durations. There is no definition for units of Day or larger
428 // to avoid confusion across daylight savings time zone transitions. 433 // to avoid confusion across daylight savings time zone transitions.
429 // 434 //
430 // To count the number of units in a Duration, divide: 435 // To count the number of units in a Duration, divide:
431 // second := time.Second 436 // second := time.Second
432 // fmt.Print(int64(second/time.Millisecond)) // prints 1000 437 // fmt.Print(int64(second/time.Millisecond)) // prints 1000
433 // 438 //
434 // To convert an integer number of units to a Duration, multiply: 439 // To convert an integer number of units to a Duration, multiply:
435 // seconds := 10 440 // seconds := 10
436 // fmt.Print(time.Duration(seconds)*time.Second) // prints 10s 441 // fmt.Print(time.Duration(seconds)*time.Second) // prints 10s
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
604 if t.nsec >= 1e9 { 609 if t.nsec >= 1e9 {
605 t.sec++ 610 t.sec++
606 t.nsec -= 1e9 611 t.nsec -= 1e9
607 } else if t.nsec < 0 { 612 } else if t.nsec < 0 {
608 t.sec-- 613 t.sec--
609 t.nsec += 1e9 614 t.nsec += 1e9
610 } 615 }
611 return t 616 return t
612 } 617 }
613 618
614 // Sub returns the duration t-u. 619 // Sub returns the duration t-u. If the result exceeds the maximum (or minimum)
620 // value that can be stored in a Duration, the maximum (or minimum) duration
621 // will be returned.
615 // To compute t-d for a duration d, use t.Add(-d). 622 // To compute t-d for a duration d, use t.Add(-d).
616 func (t Time) Sub(u Time) Duration { 623 func (t Time) Sub(u Time) Duration {
617 » return Duration(t.sec-u.sec)*Second + Duration(t.nsec-u.nsec) 624 » d := Duration(t.sec-u.sec)*Second + Duration(t.nsec-u.nsec)
r 2013/06/21 21:37:15 // Check for overflow or underflow.
rick 2013/06/21 21:44:53 Done.
625 » switch {
626 » case u.Add(d).Equal(t):
627 » » return d // d is correct
628 » case t.Before(u):
629 » » return minDuration // t - u is negative out of range
630 » default:
631 » » return maxDuration // t - u is positive out of range
632 » }
r 2013/06/21 21:37:15 you need to return something, which you don't, whi
rick 2013/06/21 21:44:53 What do you mean? There is a default case for the
r 2013/06/21 21:53:19 my mistake, sorry. the colors confuse me sometimes
618 } 633 }
619 634
620 // Since returns the time elapsed since t. 635 // Since returns the time elapsed since t.
621 // It is shorthand for time.Now().Sub(t). 636 // It is shorthand for time.Now().Sub(t).
622 func Since(t Time) Duration { 637 func Since(t Time) Duration {
623 return Now().Sub(t) 638 return Now().Sub(t)
624 } 639 }
625 640
626 // AddDate returns the time corresponding to adding the 641 // AddDate returns the time corresponding to adding the
627 // given number of years, months, and days to t. 642 // given number of years, months, and days to t.
(...skipping 511 matching lines...) Expand 10 before | Expand all | Expand 10 after
1139 // q*d + r = -t 1154 // q*d + r = -t
1140 // But the right answers are given by -(q-1), d-r: 1155 // But the right answers are given by -(q-1), d-r:
1141 // q*d + r = -t 1156 // q*d + r = -t
1142 // -q*d - r = t 1157 // -q*d - r = t
1143 // -(q-1)*d + (d - r) = t 1158 // -(q-1)*d + (d - r) = t
1144 qmod2 ^= 1 1159 qmod2 ^= 1
1145 r = d - r 1160 r = d - r
1146 } 1161 }
1147 return 1162 return
1148 } 1163 }
OLDNEW
« no previous file with comments | « no previous file | src/pkg/time/time_test.go » ('j') | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b