From de405a1e36cfca11c3401fb9e3e49c744825177f Mon Sep 17 00:00:00 2001 From: Tasos Derisiotis <50984242+Eengineer1@users.noreply.github.com> Date: Mon, 16 Jan 2023 17:20:33 +0200 Subject: [PATCH] fix: Fix for converting old time to timestamp (#511) (#512) Fix for converting old time to timestamp Co-authored-by: Andrew Nikitin --- app/migrations/helpers/constant.go | 5 +++++ app/migrations/helpers/protobuf.go | 8 ++++++++ app/migrations/helpers/protobuf_test.go | 20 ++++++++++++++++++++ 3 files changed, 33 insertions(+) create mode 100644 app/migrations/helpers/constant.go diff --git a/app/migrations/helpers/constant.go b/app/migrations/helpers/constant.go new file mode 100644 index 000000000..dd193b4a9 --- /dev/null +++ b/app/migrations/helpers/constant.go @@ -0,0 +1,5 @@ +package helpers + +const ( + OldTimeFormat = "2006-01-02 15:04:05.999999999 -0700 MST" +) diff --git a/app/migrations/helpers/protobuf.go b/app/migrations/helpers/protobuf.go index dcdc833c6..8c299c028 100644 --- a/app/migrations/helpers/protobuf.go +++ b/app/migrations/helpers/protobuf.go @@ -27,6 +27,14 @@ func MustParseFromStringTimeToGoTime(timeString string) time.Time { } t, err := time.Parse(time.RFC3339, timeString) + if err == nil { + return t + } + t, err = time.Parse(time.RFC3339Nano, timeString) + if err == nil { + return t + } + t, err = time.Parse(OldTimeFormat, timeString) if err != nil { panic(err) } diff --git a/app/migrations/helpers/protobuf_test.go b/app/migrations/helpers/protobuf_test.go index 9309f7c01..0f002bf99 100644 --- a/app/migrations/helpers/protobuf_test.go +++ b/app/migrations/helpers/protobuf_test.go @@ -1,10 +1,18 @@ package helpers import ( + "time" + . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" ) +var ( + expecteRFC3339, _ = time.Parse(time.RFC3339, "2022-09-06T16:19:39Z") + expecteRFC3339Nano, _ = time.Parse(time.RFC3339Nano, "2022-09-06T16:19:39.464251406Z") + expectedOldTimeFormat, _ = time.Parse(OldTimeFormat, "2022-02-22 13:32:19.464251406 +0000 UTC") +) + var _ = DescribeTable( "Test GenerateEd25519VerificationKey2020VerificationMaterial", func(v1PubKey string, v2PubKey string) { @@ -17,3 +25,15 @@ var _ = DescribeTable( // Mainnet case Entry("Valid: Real case", "zF7rhDBfUt9d1gJPjx7s1JXfUY7oVWkYsWCo7fztHtepn", "z6Mkta7joRuvDh7UnoESdgpr9dDUMh5LvdoECDi3WGrJoscA"), ) + +var _ = DescribeTable( + "Test MustParseFromStringTimeToGoTime", + func(inputS string, outputTT time.Time) { + timeTime := MustParseFromStringTimeToGoTime(inputS) + Expect(timeTime).To(Equal(outputTT)) + }, + + Entry("Valid: General conversion RFC3339", "2022-09-06T16:19:39Z", expecteRFC3339), + Entry("Valid: General conversion RFC3339Nano", "2022-09-06T16:19:39.464251406Z", expecteRFC3339Nano), + Entry("Valid: General conversion OldTimeFormat", "2022-02-22 13:32:19.464251406 +0000 UTC", expectedOldTimeFormat), +)