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 FormatTime and ParseTimeString methods #12995

Merged
merged 4 commits into from
Aug 22, 2022
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Improvements

* [#12995](https://github.com/cosmos/cosmos-sdk/pull/12995) Add `FormatTime` and `ParseTimeString` methods
* [#12952](https://github.com/cosmos/cosmos-sdk/pull/12952) Replace keyring module to Cosmos fork.
* [#12352](https://github.com/cosmos/cosmos-sdk/pull/12352) Move the `RegisterSwaggerAPI` logic into a separate helper function in the server package.
* [#12876](https://github.com/cosmos/cosmos-sdk/pull/12876) Remove proposer-based rewards.
Expand Down
35 changes: 30 additions & 5 deletions types/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,42 @@ const SortableTimeFormat = "2006-01-02T15:04:05.000000000"

// Formats a time.Time into a []byte that can be sorted
func FormatTimeBytes(t time.Time) []byte {
return []byte(t.UTC().Round(0).Format(SortableTimeFormat))
return []byte(FormatTimeString(t))
}

// Formats a time.Time into a string
func FormatTimeString(t time.Time) string {
return t.UTC().Round(0).Format(SortableTimeFormat)
}

// Parses a []byte encoded using FormatTimeKey back into a time.Time
func ParseTimeBytes(bz []byte) (time.Time, error) {
str := string(bz)
t, err := time.Parse(SortableTimeFormat, str)
return ParseTime(bz)
}

// Parses an encoded type using FormatTimeKey back into a time.Time
func ParseTime(T any) (time.Time, error) { //nolint:gocritic
var (
result time.Time
err error
)

switch t := T.(type) {
case time.Time:
result, err = t, nil
case []byte:
result, err = time.Parse(SortableTimeFormat, string(t))
case string:
result, err = time.Parse(SortableTimeFormat, t)
default:
return time.Time{}, fmt.Errorf("unexpected type %T", t)
}

if err != nil {
return t, err
return result, err
}
return t.UTC().Round(0), nil

return result.UTC().Round(0), nil
}

// NewLevelDB instantiate a new LevelDB instance according to DBBackend.
Expand Down
58 changes: 58 additions & 0 deletions types/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ func (s *utilsTestSuite) TestFormatTimeBytes() {
s.Require().Equal("2020-03-03T19:54:00.000000000", string(sdk.FormatTimeBytes(tm)))
}

func (s *utilsTestSuite) TestFormatTimeString() {
tm, err := time.Parse("Jan 2, 2006 at 3:04pm (MST)", "Mar 3, 2020 at 7:54pm (UTC)")
s.Require().NoError(err)
s.Require().Equal("2020-03-03T19:54:00.000000000", sdk.FormatTimeString(tm))
}

func (s *utilsTestSuite) TestParseTimeBytes() {
tm, err := sdk.ParseTimeBytes([]byte("2020-03-03T19:54:00.000000000"))
s.Require().NoError(err)
Expand All @@ -119,6 +125,58 @@ func (s *utilsTestSuite) TestParseTimeBytes() {
s.Require().Error(err)
}

func (s *utilsTestSuite) TestParseTime() {
testCases := []struct {
name string
input any
expectErr bool
expectedOutput string
}{
{
name: "valid time string",
input: "2020-03-03T19:54:00.000000000",
expectErr: false,
},
{
name: "valid time []byte",
input: []byte("2020-03-03T19:54:00.000000000"),
expectErr: false,
},
{
name: "valid time",
input: time.Date(2020, 3, 3, 19, 54, 0, 0, time.UTC),
expectErr: false,
},
{
name: "valid time different timezone",
input: func() time.Time {
ams, _ := time.LoadLocation("Asia/Seoul") // no daylight saving time
return time.Date(2020, 3, 4, 4, 54, 0, 0, ams)
}(),
expectErr: false,
},
{
name: "invalid time",
input: struct{}{},
expectErr: true,
},
}

for _, tc := range testCases {
tc := tc

s.Run(tc.name, func() {
tm, err := sdk.ParseTime(tc.input)
if tc.expectErr {
s.Require().Error(err)
} else {
s.Require().NoError(err)
s.Require().True(tm.Equal(time.Date(2020, 3, 3, 19, 54, 0, 0, time.UTC)))
}
})
}
}

func (s *utilsTestSuite) TestAppendParseBytes() {
test1 := "test1"
test2 := "testString2"
Expand Down