-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Store timestamp as uint64 in protobuf (#15)
Store timestamp as uint64 in protobuf so no pointer is needed for timestamp field
- Loading branch information
Showing
7 changed files
with
371 additions
and
448 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License 2.0; | ||
// you may not use this file except in compliance with the Elastic License 2.0. | ||
|
||
// Package timestamppb provides helper functions to encode and decode time.Time | ||
// for protobuf. | ||
package timestamppb | ||
|
||
import "time" | ||
|
||
// TimeToPBTimestamp encodes a time.Time to Unix epoch nanos in uint64 for protobuf. | ||
func TimeToPBTimestamp(t time.Time) uint64 { | ||
return uint64(t.UnixNano()) | ||
} | ||
|
||
// PBTimestampToTime decodes a uint64 of Unix epoch nanos to a time.Time for protobuf. | ||
func PBTimestampToTime(timestamp uint64) time.Time { | ||
return time.Unix(0, int64(timestamp)).UTC() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License 2.0; | ||
// you may not use this file except in compliance with the Elastic License 2.0. | ||
|
||
package timestamppb | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestTimeToPBTimestamp(t *testing.T) { | ||
tt := time.Date(2000, 1, 2, 3, 4, 5, 6, time.UTC) | ||
assert.Equal(t, tt, PBTimestampToTime(TimeToPBTimestamp(tt))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters