OLD | NEW |
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 746 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
757 } | 757 } |
758 | 758 |
759 // Unix returns t as a Unix time, the number of seconds elapsed | 759 // Unix returns t as a Unix time, the number of seconds elapsed |
760 // since January 1, 1970 UTC. | 760 // since January 1, 1970 UTC. |
761 func (t Time) Unix() int64 { | 761 func (t Time) Unix() int64 { |
762 return t.sec + internalToUnix | 762 return t.sec + internalToUnix |
763 } | 763 } |
764 | 764 |
765 // UnixNano returns t as a Unix time, the number of nanoseconds elapsed | 765 // UnixNano returns t as a Unix time, the number of nanoseconds elapsed |
766 // since January 1, 1970 UTC. | 766 // since January 1, 1970 UTC. |
| 767 // It is a runtime error to call UnixNano if representing t in nanoseconds |
| 768 // would overflow int64. The zero time is outside of the supported range. |
767 func (t Time) UnixNano() int64 { | 769 func (t Time) UnixNano() int64 { |
768 » return (t.sec+internalToUnix)*1e9 + int64(t.nsec) | 770 » secs := t.sec + internalToUnix |
| 771 » nano := secs*1e9 + int64(t.nsec) |
| 772 » if (secs < 0) != (nano < 0) { |
| 773 » » panic("UnixNano: time in nanoseconds overflows int64") |
| 774 » } |
| 775 » return nano |
769 } | 776 } |
770 | 777 |
771 const timeGobVersion byte = 1 | 778 const timeGobVersion byte = 1 |
772 | 779 |
773 // GobEncode implements the gob.GobEncoder interface. | 780 // GobEncode implements the gob.GobEncoder interface. |
774 func (t Time) GobEncode() ([]byte, error) { | 781 func (t Time) GobEncode() ([]byte, error) { |
775 var offsetMin int16 // minutes east of UTC. -1 is UTC. | 782 var offsetMin int16 // minutes east of UTC. -1 is UTC. |
776 | 783 |
777 if t.Location() == &utcLoc { | 784 if t.Location() == &utcLoc { |
778 offsetMin = -1 | 785 offsetMin = -1 |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
978 case utc < start: | 985 case utc < start: |
979 _, offset, _, _, _ = loc.lookup(start - 1) | 986 _, offset, _, _, _ = loc.lookup(start - 1) |
980 case utc >= end: | 987 case utc >= end: |
981 _, offset, _, _, _ = loc.lookup(end) | 988 _, offset, _, _, _ = loc.lookup(end) |
982 } | 989 } |
983 unix -= int64(offset) | 990 unix -= int64(offset) |
984 } | 991 } |
985 | 992 |
986 return Time{unix + unixToInternal, int32(nsec), loc} | 993 return Time{unix + unixToInternal, int32(nsec), loc} |
987 } | 994 } |
OLD | NEW |