summaryrefslogtreecommitdiff
path: root/libgo/go/time/time.go
diff options
context:
space:
mode:
Diffstat (limited to 'libgo/go/time/time.go')
-rw-r--r--libgo/go/time/time.go45
1 files changed, 22 insertions, 23 deletions
diff --git a/libgo/go/time/time.go b/libgo/go/time/time.go
index c504df7401..0300e846a4 100644
--- a/libgo/go/time/time.go
+++ b/libgo/go/time/time.go
@@ -31,6 +31,11 @@ import "errors"
// change the instant in time being denoted and therefore does not affect the
// computations described in earlier paragraphs.
//
+// Note that the Go == operator compares not just the time instant but also the
+// Location. Therefore, Time values should not be used as map or database keys
+// without first guaranteeing that the identical Location has been set for all
+// values, which can be achieved through use of the UTC or Local method.
+//
type Time struct {
// sec gives the number of seconds elapsed since
// January 1, year 1 00:00:00 UTC.
@@ -39,14 +44,7 @@ type Time struct {
// nsec specifies a non-negative nanosecond
// offset within the second named by Seconds.
// It must be in the range [0, 999999999].
- //
- // It is declared as uintptr instead of int32 or uint32
- // to avoid garbage collector aliasing in the case where
- // on a 64-bit system the int32 or uint32 field is written
- // over the low half of a pointer, creating another pointer.
- // TODO(rsc): When the garbage collector is completely
- // precise, change back to int32.
- nsec uintptr
+ nsec int32
// loc specifies the Location that should be used to
// determine the minute, hour, month, day, and year
@@ -475,29 +473,28 @@ func (d Duration) String() string {
if u < uint64(Second) {
// Special case: if duration is smaller than a second,
// use smaller units, like 1.2ms
- var (
- prec int
- unit byte
- )
+ var prec int
+ w--
+ buf[w] = 's'
+ w--
switch {
case u == 0:
return "0"
case u < uint64(Microsecond):
// print nanoseconds
prec = 0
- unit = 'n'
+ buf[w] = 'n'
case u < uint64(Millisecond):
// print microseconds
prec = 3
- unit = 'u'
+ // U+00B5 'µ' micro sign == 0xC2 0xB5
+ w-- // Need room for two bytes.
+ copy(buf[w:], "µ")
default:
// print milliseconds
prec = 6
- unit = 'm'
+ buf[w] = 'm'
}
- w -= 2
- buf[w] = unit
- buf[w+1] = 's'
w, u = fmtFrac(buf[:w], u, prec)
w = fmtInt(buf[:w], u)
} else {
@@ -620,7 +617,7 @@ func (t Time) Add(d Duration) Time {
t.sec--
nsec += 1e9
}
- t.nsec = uintptr(nsec)
+ t.nsec = nsec
return t
}
@@ -783,7 +780,7 @@ func now() (sec int64, nsec int32)
// Now returns the current local time.
func Now() Time {
sec, nsec := now()
- return Time{sec + unixToInternal, uintptr(nsec), Local}
+ return Time{sec + unixToInternal, nsec, Local}
}
// UTC returns t with the location set to UTC.
@@ -900,7 +897,7 @@ func (t *Time) UnmarshalBinary(data []byte) error {
int64(buf[3])<<32 | int64(buf[2])<<40 | int64(buf[1])<<48 | int64(buf[0])<<56
buf = buf[8:]
- t.nsec = uintptr(int32(buf[3]) | int32(buf[2])<<8 | int32(buf[1])<<16 | int32(buf[0])<<24)
+ t.nsec = int32(buf[3]) | int32(buf[2])<<8 | int32(buf[1])<<16 | int32(buf[0])<<24
buf = buf[4:]
offset := int(int16(buf[1])|int16(buf[0])<<8) * 60
@@ -934,6 +931,8 @@ func (t *Time) GobDecode(data []byte) error {
// The time is a quoted string in RFC 3339 format, with sub-second precision added if present.
func (t Time) MarshalJSON() ([]byte, error) {
if y := t.Year(); y < 0 || y >= 10000 {
+ // RFC 3339 is clear that years are 4 digits exactly.
+ // See golang.org/issue/4556#c15 for more discussion.
return nil, errors.New("Time.MarshalJSON: year outside of range [0,9999]")
}
return []byte(t.Format(`"` + RFC3339Nano + `"`)), nil
@@ -977,7 +976,7 @@ func Unix(sec int64, nsec int64) Time {
sec--
}
}
- return Time{sec + unixToInternal, uintptr(nsec), Local}
+ return Time{sec + unixToInternal, int32(nsec), Local}
}
func isLeap(year int) bool {
@@ -1086,7 +1085,7 @@ func Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) T
unix -= int64(offset)
}
- return Time{unix + unixToInternal, uintptr(nsec), loc}
+ return Time{unix + unixToInternal, int32(nsec), loc}
}
// Truncate returns the result of rounding t down to a multiple of d (since the zero time).