Skip to content

Commit

Permalink
Handle parsing of timestamps in time.DateTime format. Fix gruntwork-i…
Browse files Browse the repository at this point in the history
  • Loading branch information
sbocinec committed Apr 12, 2024
1 parent 98a6926 commit 9814895
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
15 changes: 10 additions & 5 deletions util/time.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand All @@ -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 {
Expand All @@ -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
Expand Down
16 changes: 13 additions & 3 deletions util/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
Expand All @@ -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
}

0 comments on commit 9814895

Please sign in to comment.