From 98148951bfa6f11d2d8e6822b94e0d5610d1c231 Mon Sep 17 00:00:00 2001 From: Stano Bocinec Date: Fri, 12 Apr 2024 15:58:34 +0200 Subject: [PATCH] Handle parsing of timestamps in time.DateTime format. Fix #670 --- util/time.go | 15 ++++++++++----- util/time_test.go | 16 +++++++++++++--- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/util/time.go b/util/time.go index bf1268c25..bffa5df23 100644 --- a/util/time.go +++ b/util/time.go @@ -1,10 +1,11 @@ package util import ( + "time" + "github.com/aws/aws-sdk-go/aws" "github.com/gruntwork-io/cloud-nuke/logging" "github.com/gruntwork-io/go-commons/errors" - "time" ) const ( @@ -16,7 +17,8 @@ const ( FirstSeenTagKey = "cloud-nuke-first-seen" // The time format of the `firstSeenTagKey` tag value. - firstSeenTimeFormat = time.RFC3339 + firstSeenTimeFormat = time.RFC3339 + firstSeenTimeFormatLegacy = time.DateTime ) func IsFirstSeenTag(key *string) bool { @@ -26,9 +28,12 @@ func IsFirstSeenTag(key *string) bool { func ParseTimestamp(timestamp *string) (*time.Time, error) { parsed, err := time.Parse(firstSeenTimeFormat, aws.StringValue(timestamp)) if err != nil { - logging.Debugf("Error parsing the timestamp into a `RFC3339` Time format") - return nil, errors.WithStackTrace(err) - + logging.Debugf("Error parsing the timestamp into a `RFC3339` Time format. Trying parsing the timestamp using the legacy `time.DateTime` format.") + parsed, err = time.Parse(firstSeenTimeFormatLegacy, aws.StringValue(timestamp)) + if err != nil { + logging.Debugf("Error parsing the timestamp into legacy `time.DateTime` Time format") + return nil, errors.WithStackTrace(err) + } } return &parsed, nil diff --git a/util/time_test.go b/util/time_test.go index c55d8a658..a9e810f1f 100644 --- a/util/time_test.go +++ b/util/time_test.go @@ -19,9 +19,15 @@ func TestParseTimestamp(t *testing.T) { wantErr bool }{ { - "it should parse \"2023-12-19 10:38:44\" correctly", + "it should parse legacy firstSeenTag value \"2023-12-19 10:38:44\" correctly", args{timestamp: aws.String("2023-12-19 10:38:44")}, - &time.Time{2023, 12, 19, 10, 38, 44, 0, time.UTC}, + newTime(time.Date(2023, 12, 19, 10, 38, 44, 0, time.UTC)), + false, + }, + { + "it should parse RFC3339 firstSeenTag value \"2024-04-12T15:18:05Z\" correctly", + args{timestamp: aws.String("2024-04-12T15:18:05Z")}, + newTime(time.Date(2024, 4, 12, 15, 18, 5, 0, time.UTC)), false, }, } @@ -33,8 +39,12 @@ func TestParseTimestamp(t *testing.T) { return } if !reflect.DeepEqual(got, tt.want) { - t.Errorf("ParseTimestamp() = %v, want %v", got, tt.want) + t.Errorf("ParseTimestamp() = %v, want %v", got, &tt.want) } }) } } + +func newTime(t time.Time) *time.Time { + return &t +}