diff options
author | Derek Phan <derekphan94@gmail.com> | 2019-03-14 00:55:05 +0000 |
---|---|---|
committer | Robert Griesemer <gri@golang.org> | 2019-03-14 18:27:01 +0000 |
commit | aa193c0b9623acd7397c0799ffc9efe5845216b2 (patch) | |
tree | ffdb7bd88fb0b0e337069241b5a8fd9279daf65e /src/time/time_test.go | |
parent | f832a97e454a5e2bed336321965fd2382b2af868 (diff) | |
download | go-aa193c0b9623acd7397c0799ffc9efe5845216b2.tar.gz go-aa193c0b9623acd7397c0799ffc9efe5845216b2.zip |
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: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src/time/time_test.go')
-rw-r--r-- | src/time/time_test.go | 42 |
1 files changed, 37 insertions, 5 deletions
diff --git a/src/time/time_test.go b/src/time/time_test.go index 76924e36f3..0ac3c3a27f 100644 --- a/src/time/time_test.go +++ b/src/time/time_test.go @@ -690,7 +690,7 @@ var gobTests = []Time{ Date(0, 1, 2, 3, 4, 5, 6, UTC), Date(7, 8, 9, 10, 11, 12, 13, FixedZone("", 0)), Unix(81985467080890095, 0x76543210), // Time.sec: 0x0123456789ABCDEF - {}, // nil location + {}, // nil location Date(1, 2, 3, 4, 5, 6, 7, FixedZone("", 32767*60)), Date(1, 2, 3, 4, 5, 6, 7, FixedZone("", -32768*60)), } @@ -1021,7 +1021,39 @@ var nsDurationTests = []struct { func TestDurationNanoseconds(t *testing.T) { for _, tt := range nsDurationTests { 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 { func TestDurationSeconds(t *testing.T) { for _, tt := range secDurationTests { 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 { func TestDurationMinutes(t *testing.T) { for _, tt := range minDurationTests { 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 { func TestDurationHours(t *testing.T) { for _, tt := range hourDurationTests { 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) } } } |