Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Ensure that time conversion for 0 returns nil timestamps or Time where IsZero returns true #1550

Merged
merged 1 commit into from
Aug 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions internal/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,9 @@ func UnixNanoToTimestamp(u pdata.TimestampUnixNano) *timestamp.Timestamp {
}

func UnixNanoToTime(u pdata.TimestampUnixNano) time.Time {
// 0 is a special case and want to make sure we return a time that IsZero() returns true.
if u == 0 {
return time.Time{}
}
return time.Unix(0, int64(u)).UTC()
}
17 changes: 11 additions & 6 deletions internal/internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package internal_test
package internal

import (
"testing"
Expand All @@ -22,25 +22,30 @@ import (
"github.com/stretchr/testify/assert"

"go.opentelemetry.io/collector/consumer/pdata"
"go.opentelemetry.io/collector/internal"
)

func TestTimeConverters(t *testing.T) {
// Ensure that we nanoseconds but that they are also preserved.
t1 := time.Date(2018, 10, 31, 19, 43, 35, 789, time.UTC)

assert.EqualValues(t, int64(1541015015000000789), t1.UnixNano())
tp := internal.TimeToTimestamp(t1)
tp := TimeToTimestamp(t1)
assert.EqualValues(t, &timestamp.Timestamp{Seconds: 1541015015, Nanos: 789}, tp)
assert.EqualValues(t, int64(1541015015000000789), internal.TimestampToTime(tp).UnixNano())
assert.EqualValues(t, int64(1541015015000000789), TimestampToTime(tp).UnixNano())
}

func TestUnixNanosConverters(t *testing.T) {
t1 := time.Date(2020, 03, 24, 1, 13, 23, 789, time.UTC)
tun := pdata.TimestampUnixNano(t1.UnixNano())

assert.EqualValues(t, uint64(1585012403000000789), tun)
tp := internal.UnixNanoToTimestamp(tun)
tp := UnixNanoToTimestamp(tun)
assert.EqualValues(t, &timestamp.Timestamp{Seconds: 1585012403, Nanos: 789}, tp)
assert.EqualValues(t, tun, internal.TimestampToUnixNano(tp))
assert.EqualValues(t, tun, TimestampToUnixNano(tp))
}

func TestZeroTimestamps(t *testing.T) {
assert.Nil(t, TimeToTimestamp(time.Time{}))
assert.Nil(t, UnixNanoToTimestamp(0))
assert.True(t, UnixNanoToTime(0).IsZero())
}