Skip to content

Commit

Permalink
[BEAM-13988] Update mtime to use time.UnixMilli() calls (#17578)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmccluskey authored May 6, 2022
1 parent a0684f5 commit f1f26f1
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
6 changes: 2 additions & 4 deletions sdks/go/pkg/beam/core/graph/mtime/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
54 changes: 54 additions & 0 deletions sdks/go/pkg/beam/core/graph/mtime/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

0 comments on commit f1f26f1

Please sign in to comment.