LEFT | RIGHT |
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 745 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
756 return | 756 return |
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. The result is undefined if the Unix time |
767 // It is a runtime error to call UnixNano if representing t in nanoseconds | 767 // in nanoseconds cannot be represented by an int64. Note that this |
768 // would overflow int64. The zero time is outside of the supported range. | 768 // means the result of calling UnixNano on the zero Time is undefined. |
769 func (t Time) UnixNano() int64 { | 769 func (t Time) UnixNano() int64 { |
770 » secs := t.sec + internalToUnix | 770 » return (t.sec+internalToUnix)*1e9 + int64(t.nsec) |
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 | |
776 } | 771 } |
777 | 772 |
778 const timeGobVersion byte = 1 | 773 const timeGobVersion byte = 1 |
779 | 774 |
780 // GobEncode implements the gob.GobEncoder interface. | 775 // GobEncode implements the gob.GobEncoder interface. |
781 func (t Time) GobEncode() ([]byte, error) { | 776 func (t Time) GobEncode() ([]byte, error) { |
782 var offsetMin int16 // minutes east of UTC. -1 is UTC. | 777 var offsetMin int16 // minutes east of UTC. -1 is UTC. |
783 | 778 |
784 if t.Location() == &utcLoc { | 779 if t.Location() == &utcLoc { |
785 offsetMin = -1 | 780 offsetMin = -1 |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
985 case utc < start: | 980 case utc < start: |
986 _, offset, _, _, _ = loc.lookup(start - 1) | 981 _, offset, _, _, _ = loc.lookup(start - 1) |
987 case utc >= end: | 982 case utc >= end: |
988 _, offset, _, _, _ = loc.lookup(end) | 983 _, offset, _, _, _ = loc.lookup(end) |
989 } | 984 } |
990 unix -= int64(offset) | 985 unix -= int64(offset) |
991 } | 986 } |
992 | 987 |
993 return Time{unix + unixToInternal, int32(nsec), loc} | 988 return Time{unix + unixToInternal, int32(nsec), loc} |
994 } | 989 } |
LEFT | RIGHT |