-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add pbtime util package #11085
Closed
Closed
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
230242b
feat: add pbtime util package
robert-zaremba ccc3f81
Update pbtime/cmp_test.go
robert-zaremba 32f322f
support nil values in Add
robert-zaremba 572c301
rename Add -> AddStd and new Add function which accepts PB duration
robert-zaremba b3a4c6d
add panic case to Compare
robert-zaremba c44f649
add overflow checks
robert-zaremba badc1a8
Merge branch 'master' into robert/pbtime
robert-zaremba File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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,3 @@ | ||
# pbtime | ||
|
||
pbtime is a Go package, part of the core Cosmos SDK module with helper methods for [protobuf timestamp](https://developers.google.com/protocol-buffers/docs/reference/google.protobuf#timestamp). |
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,32 @@ | ||
package pbtime | ||
|
||
import ( | ||
"time" | ||
|
||
tspb "google.golang.org/protobuf/types/known/timestamppb" | ||
) | ||
|
||
func IsZero(t *tspb.Timestamp) bool { | ||
return t == nil || t.Nanos == 0 && t.Seconds == 0 | ||
} | ||
|
||
// Commpare t1 and t2 and returns -1 when t1 < t2, 0 when t1 == t2 and 1 otherwise. | ||
// Panics if t1 or t2 is nil | ||
func Compare(t1, t2 *tspb.Timestamp) int { | ||
if t1.Seconds == t2.Seconds && t1.Nanos == t2.Nanos { | ||
return 0 | ||
} | ||
if t1.Seconds < t2.Seconds || t1.Seconds == t2.Seconds && t1.Nanos < t2.Nanos { | ||
return -1 | ||
} | ||
return 1 | ||
} | ||
|
||
func Add(t *tspb.Timestamp, d time.Duration) *tspb.Timestamp { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK,. we can have both: |
||
if d == 0 { | ||
t2 := *t | ||
return &t2 | ||
} | ||
t2 := t.AsTime() | ||
return tspb.New(t2.Add(d)) | ||
} |
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,79 @@ | ||
package pbtime | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"math/rand" | ||
|
||
"github.com/stretchr/testify/require" | ||
tspb "google.golang.org/protobuf/types/known/timestamppb" | ||
) | ||
|
||
func new(s int64, n int32) *tspb.Timestamp { | ||
return &tspb.Timestamp{Seconds: s, Nanos: n} | ||
} | ||
|
||
func TestIsZero(t *testing.T) { | ||
tcs := []struct { | ||
t *tspb.Timestamp | ||
expected bool | ||
}{ | ||
{&tspb.Timestamp{}, true}, | ||
{new(0, 0), true}, | ||
|
||
{new(1, 0), true}, | ||
{new(0, 1), false}, | ||
{tspb.New(time.Time{}), false}, | ||
} | ||
|
||
for i, tc := range tcs { | ||
require.Equal(t, tc.expected, IsZero(tc.t), "test_id %d", i) | ||
} | ||
} | ||
|
||
func TestCmpare(t *testing.T) { | ||
robert-zaremba marked this conversation as resolved.
Show resolved
Hide resolved
|
||
tcs := []struct { | ||
t1 *tspb.Timestamp | ||
t2 *tspb.Timestamp | ||
expected int | ||
}{ | ||
{&tspb.Timestamp{}, &tspb.Timestamp{}, 0}, | ||
{new(1, 1), new(1, 1), 0}, | ||
{new(-1, 1), new(-1, 1), 0}, | ||
{new(231, -5), new(231, -5), 0}, | ||
|
||
{new(1, -1), new(1, 0), -1}, | ||
{new(1, -1), new(12, -1), -1}, | ||
{new(-11, -1), new(-1, -1), -1}, | ||
|
||
{new(1, -1), new(0, -1), 1}, | ||
{new(1, -1), new(1, -2), 1}, | ||
} | ||
for i, tc := range tcs { | ||
t.Run(fmt.Sprint("test ", i), func(t *testing.T) { | ||
r := Compare(tc.t1, tc.t2) | ||
require.Equal(t, tc.expected, r) | ||
}) | ||
} | ||
} | ||
|
||
func TestAddFuzzy(t *testing.T) { | ||
requier := require.New(t) | ||
check := func(s, n int64, d time.Duration) { | ||
t := time.Unix(s, n) | ||
t_expected := t.Add(d) | ||
tb := tspb.New(t) | ||
tb = Add(tb, d) | ||
requier.Equal(*tspb.New(t_expected), *tb) | ||
} | ||
|
||
for i := 0; i < 2000; i++ { | ||
s, n, d := rand.Int63(), rand.Int63(), time.Duration(rand.Int63()) | ||
check(s, n, d) | ||
} | ||
check(0, 0, 0) | ||
check(1, 2, 0) | ||
check(-1, -1, 1) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this isn't generically true. it's sufficient if we assume that the time is in the future, but technically 1970-01-01 is a valid timestamp. generically
IsZero
is equivalent tot == nil
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function doesn't do a validation. The goal is to make a canonical zero value which work with a non nil version of
tspb.Timestamp
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But it's not zero. We can't use this to infer that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's zero value of
tspb.Timestamp
and I thought that yesterday we said to add a support for a non nil zero value.That being said, my if we can change non nil to nil in proto and handle that in a normal way, then that would be my preference.