Commit aa193c0b authored by Derek Phan's avatar Derek Phan Committed by Robert Griesemer

time: add methods to convert duration to microseconds and milliseconds

The return values are integers, as opposed to floats, since the fractionals can be derived from multiplying t.Seconds().

Fixes #28564

Change-Id: I3796227e1f64ead39ff0aacfbdce912d952f2994
GitHub-Last-Rev: b843ab740bf5a8216478322533521d6243fe1cb1
GitHub-Pull-Request: golang/go#30819
Reviewed-on: https://go-review.googlesource.com/c/go/+/167387
Run-TryBot: Robert Griesemer <gri@golang.org>
Reviewed-by: default avatarRobert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent f832a97e
...@@ -783,6 +783,12 @@ func fmtInt(buf []byte, v uint64) int { ...@@ -783,6 +783,12 @@ func fmtInt(buf []byte, v uint64) int {
// Nanoseconds returns the duration as an integer nanosecond count. // Nanoseconds returns the duration as an integer nanosecond count.
func (d Duration) Nanoseconds() int64 { return int64(d) } func (d Duration) Nanoseconds() int64 { return int64(d) }
// Microseconds returns the duration as an integer microsecond count.
func (d Duration) Microseconds() int64 { return int64(d) / 1e3 }
// Milliseconds returns the duration as an integer millisecond count.
func (d Duration) Milliseconds() int64 { return int64(d) / 1e6 }
// These methods return float64 because the dominant // These methods return float64 because the dominant
// use case is for printing a floating point number like 1.5s, and // use case is for printing a floating point number like 1.5s, and
// a truncation to integer would make them not useful in those cases. // a truncation to integer would make them not useful in those cases.
......
...@@ -1021,7 +1021,39 @@ var nsDurationTests = []struct { ...@@ -1021,7 +1021,39 @@ var nsDurationTests = []struct {
func TestDurationNanoseconds(t *testing.T) { func TestDurationNanoseconds(t *testing.T) {
for _, tt := range nsDurationTests { for _, tt := range nsDurationTests {
if got := tt.d.Nanoseconds(); got != tt.want { if got := tt.d.Nanoseconds(); got != tt.want {
t.Errorf("d.Nanoseconds() = %d; want: %d", got, tt.want) t.Errorf("Duration(%s).Nanoseconds() = %d; want: %d", tt.d, got, tt.want)
}
}
}
var usDurationTests = []struct {
d Duration
want int64
}{
{Duration(-1000), -1},
{Duration(1000), 1},
}
func TestDurationMicroseconds(t *testing.T) {
for _, tt := range usDurationTests {
if got := tt.d.Microseconds(); got != tt.want {
t.Errorf("Duration(%s).Microseconds() = %d; want: %d", tt.d, got, tt.want)
}
}
}
var msDurationTests = []struct {
d Duration
want int64
}{
{Duration(-1000000), -1},
{Duration(1000000), 1},
}
func TestDurationMilliseconds(t *testing.T) {
for _, tt := range msDurationTests {
if got := tt.d.Milliseconds(); got != tt.want {
t.Errorf("Duration(%s).Milliseconds() = %d; want: %d", tt.d, got, tt.want)
} }
} }
} }
...@@ -1036,7 +1068,7 @@ var secDurationTests = []struct { ...@@ -1036,7 +1068,7 @@ var secDurationTests = []struct {
func TestDurationSeconds(t *testing.T) { func TestDurationSeconds(t *testing.T) {
for _, tt := range secDurationTests { for _, tt := range secDurationTests {
if got := tt.d.Seconds(); got != tt.want { if got := tt.d.Seconds(); got != tt.want {
t.Errorf("d.Seconds() = %g; want: %g", got, tt.want) t.Errorf("Duration(%s).Seconds() = %g; want: %g", tt.d, got, tt.want)
} }
} }
} }
...@@ -1055,7 +1087,7 @@ var minDurationTests = []struct { ...@@ -1055,7 +1087,7 @@ var minDurationTests = []struct {
func TestDurationMinutes(t *testing.T) { func TestDurationMinutes(t *testing.T) {
for _, tt := range minDurationTests { for _, tt := range minDurationTests {
if got := tt.d.Minutes(); got != tt.want { if got := tt.d.Minutes(); got != tt.want {
t.Errorf("d.Minutes() = %g; want: %g", got, tt.want) t.Errorf("Duration(%s).Minutes() = %g; want: %g", tt.d, got, tt.want)
} }
} }
} }
...@@ -1074,7 +1106,7 @@ var hourDurationTests = []struct { ...@@ -1074,7 +1106,7 @@ var hourDurationTests = []struct {
func TestDurationHours(t *testing.T) { func TestDurationHours(t *testing.T) {
for _, tt := range hourDurationTests { for _, tt := range hourDurationTests {
if got := tt.d.Hours(); got != tt.want { if got := tt.d.Hours(); got != tt.want {
t.Errorf("d.Hours() = %g; want: %g", got, tt.want) t.Errorf("Duration(%s).Hours() = %g; want: %g", tt.d, got, tt.want)
} }
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment