This is an automated email from the ASF dual-hosted git repository. lostluck pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/beam.git
The following commit(s) were added to refs/heads/master by this push: new f1f26f10698 [BEAM-13988] Update mtime to use time.UnixMilli() calls (#17578) f1f26f10698 is described below commit f1f26f1069891ea15eaf6aaa90d683b53c4ae340 Author: Jack McCluskey <34928439+jrmcclus...@users.noreply.github.com> AuthorDate: Fri May 6 15:59:37 2022 -0400 [BEAM-13988] Update mtime to use time.UnixMilli() calls (#17578) --- sdks/go/pkg/beam/core/graph/mtime/time.go | 6 +-- sdks/go/pkg/beam/core/graph/mtime/time_test.go | 54 ++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/sdks/go/pkg/beam/core/graph/mtime/time.go b/sdks/go/pkg/beam/core/graph/mtime/time.go index 33d858d0fb7..26a3dab66a4 100644 --- a/sdks/go/pkg/beam/core/graph/mtime/time.go +++ b/sdks/go/pkg/beam/core/graph/mtime/time.go @@ -65,8 +65,7 @@ func FromDuration(d time.Duration) Time { // FromTime returns a milli-second precision timestamp from a time.Time. func FromTime(t time.Time) Time { - // TODO(BEAM-13988): Replace t.UnixNano() with t.UnixMilli() for Go 1.17 or higher. - return Normalize(Time(t.UnixNano() / 1e6)) + return Normalize(Time(t.UnixMilli())) } // Milliseconds returns the number of milli-seconds since the Unix epoch. @@ -76,8 +75,7 @@ func (t Time) Milliseconds() int64 { // ToTime returns the Time represented as a time.Time func (t Time) ToTime() time.Time { - // TODO(BEAM-13988): Replace with time.UnixMilli(int64(t)).UTC() for Go 1.17 or higher. - return time.Unix(0, int64(t)*1e6).UTC() + return time.UnixMilli(int64(t)).UTC() } // Add returns the time plus the duration. Input Durations of less than one diff --git a/sdks/go/pkg/beam/core/graph/mtime/time_test.go b/sdks/go/pkg/beam/core/graph/mtime/time_test.go index e2f914686c4..26e2289172d 100644 --- a/sdks/go/pkg/beam/core/graph/mtime/time_test.go +++ b/sdks/go/pkg/beam/core/graph/mtime/time_test.go @@ -244,3 +244,57 @@ func TestToTime(t *testing.T) { }) } } + +func TestMilliseconds(t *testing.T) { + tests := []struct { + name string + inputMillis int64 + }{ + { + "Zero", + int64(0), + }, + { + "End of Global Window", + EndOfGlobalWindowTime.Milliseconds(), + }, + { + "some number", + int64(1000), + }, + } + for _, test := range tests { + milliTime := FromMilliseconds(test.inputMillis) + outputMillis := milliTime.Milliseconds() + if got, want := outputMillis, test.inputMillis; got != want { + t.Errorf("got %v milliseconds, want %v milliseconds", got, want) + } + } +} + +func TestFromDuration(t *testing.T) { + tests := []struct { + name string + dur time.Duration + }{ + { + "zero", + 0 * time.Millisecond, + }, + { + "End of Global Window", + time.Duration(EndOfGlobalWindowTime), + }, + { + "day", + 24 * time.Hour, + }, + } + for _, test := range tests { + durTime := FromDuration(test.dur) + timeMillis := durTime.Milliseconds() + if got, want := timeMillis, test.dur.Milliseconds(); got != want { + t.Errorf("got %v milliseconds, want %v milliseconds", got, want) + } + } +}