Skip to content

Commit aa193c0

Browse files
dphan72griesemer
authored andcommitted
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: b843ab7 GitHub-Pull-Request: #30819 Reviewed-on: https://go-review.googlesource.com/c/go/+/167387 Run-TryBot: Robert Griesemer <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent f832a97 commit aa193c0

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

src/time/time.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,12 @@ func fmtInt(buf []byte, v uint64) int {
783783
// Nanoseconds returns the duration as an integer nanosecond count.
784784
func (d Duration) Nanoseconds() int64 { return int64(d) }
785785

786+
// Microseconds returns the duration as an integer microsecond count.
787+
func (d Duration) Microseconds() int64 { return int64(d) / 1e3 }
788+
789+
// Milliseconds returns the duration as an integer millisecond count.
790+
func (d Duration) Milliseconds() int64 { return int64(d) / 1e6 }
791+
786792
// These methods return float64 because the dominant
787793
// use case is for printing a floating point number like 1.5s, and
788794
// a truncation to integer would make them not useful in those cases.

src/time/time_test.go

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ var gobTests = []Time{
690690
Date(0, 1, 2, 3, 4, 5, 6, UTC),
691691
Date(7, 8, 9, 10, 11, 12, 13, FixedZone("", 0)),
692692
Unix(81985467080890095, 0x76543210), // Time.sec: 0x0123456789ABCDEF
693-
{}, // nil location
693+
{}, // nil location
694694
Date(1, 2, 3, 4, 5, 6, 7, FixedZone("", 32767*60)),
695695
Date(1, 2, 3, 4, 5, 6, 7, FixedZone("", -32768*60)),
696696
}
@@ -1021,7 +1021,39 @@ var nsDurationTests = []struct {
10211021
func TestDurationNanoseconds(t *testing.T) {
10221022
for _, tt := range nsDurationTests {
10231023
if got := tt.d.Nanoseconds(); got != tt.want {
1024-
t.Errorf("d.Nanoseconds() = %d; want: %d", got, tt.want)
1024+
t.Errorf("Duration(%s).Nanoseconds() = %d; want: %d", tt.d, got, tt.want)
1025+
}
1026+
}
1027+
}
1028+
1029+
var usDurationTests = []struct {
1030+
d Duration
1031+
want int64
1032+
}{
1033+
{Duration(-1000), -1},
1034+
{Duration(1000), 1},
1035+
}
1036+
1037+
func TestDurationMicroseconds(t *testing.T) {
1038+
for _, tt := range usDurationTests {
1039+
if got := tt.d.Microseconds(); got != tt.want {
1040+
t.Errorf("Duration(%s).Microseconds() = %d; want: %d", tt.d, got, tt.want)
1041+
}
1042+
}
1043+
}
1044+
1045+
var msDurationTests = []struct {
1046+
d Duration
1047+
want int64
1048+
}{
1049+
{Duration(-1000000), -1},
1050+
{Duration(1000000), 1},
1051+
}
1052+
1053+
func TestDurationMilliseconds(t *testing.T) {
1054+
for _, tt := range msDurationTests {
1055+
if got := tt.d.Milliseconds(); got != tt.want {
1056+
t.Errorf("Duration(%s).Milliseconds() = %d; want: %d", tt.d, got, tt.want)
10251057
}
10261058
}
10271059
}
@@ -1036,7 +1068,7 @@ var secDurationTests = []struct {
10361068
func TestDurationSeconds(t *testing.T) {
10371069
for _, tt := range secDurationTests {
10381070
if got := tt.d.Seconds(); got != tt.want {
1039-
t.Errorf("d.Seconds() = %g; want: %g", got, tt.want)
1071+
t.Errorf("Duration(%s).Seconds() = %g; want: %g", tt.d, got, tt.want)
10401072
}
10411073
}
10421074
}
@@ -1055,7 +1087,7 @@ var minDurationTests = []struct {
10551087
func TestDurationMinutes(t *testing.T) {
10561088
for _, tt := range minDurationTests {
10571089
if got := tt.d.Minutes(); got != tt.want {
1058-
t.Errorf("d.Minutes() = %g; want: %g", got, tt.want)
1090+
t.Errorf("Duration(%s).Minutes() = %g; want: %g", tt.d, got, tt.want)
10591091
}
10601092
}
10611093
}
@@ -1074,7 +1106,7 @@ var hourDurationTests = []struct {
10741106
func TestDurationHours(t *testing.T) {
10751107
for _, tt := range hourDurationTests {
10761108
if got := tt.d.Hours(); got != tt.want {
1077-
t.Errorf("d.Hours() = %g; want: %g", got, tt.want)
1109+
t.Errorf("Duration(%s).Hours() = %g; want: %g", tt.d, got, tt.want)
10781110
}
10791111
}
10801112
}

0 commit comments

Comments
 (0)