Skip to content
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
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions pbtime/README.md
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).
32 changes: 32 additions & 0 deletions pbtime/cmp.go
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
}
Comment on lines +12 to +14
Copy link
Member

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 to t == nil

Copy link
Collaborator Author

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.

Copy link
Member

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

Copy link
Collaborator Author

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.


// 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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should use durationpb here

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK,. we can have both: time.Duration and durationpb

if d == 0 {
t2 := *t
return &t2
}
t2 := t.AsTime()
return tspb.New(t2.Add(d))
}
79 changes: 79 additions & 0 deletions pbtime/cmp_test.go
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)
}