Skip to content

Commit

Permalink
Improved support for various time formats in parse.ParseTime. Closes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
graza-io committed Feb 12, 2025
1 parent a11e104 commit 8c5beb4
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 25 deletions.
8 changes: 0 additions & 8 deletions constants/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,5 @@ Supported formats:
• T-180d (180 days ago)
• T-9H (9 hours ago)
• T-10m (10 minutes ago)
`
InvalidTimeFormat = `Invalid time format
Supported formats:
• 2024-01-06
• 2006-01-06T15:04:05
• 2006-01-06T15:04:05.000
• 2006-01-06T15:04:05Z07:00
`
)
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ require (
github.com/spf13/viper v1.19.0
github.com/stevenle/topsort v0.2.0
github.com/stretchr/testify v1.10.0
github.com/turbot/go-kit v1.0.0
github.com/turbot/go-kit v1.0.1-rc.0
github.com/xlab/treeprint v1.2.0
github.com/zclconf/go-cty v1.14.4
github.com/zclconf/go-cty-yaml v1.0.3
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,7 @@ github.com/tkrajina/go-reflector v0.5.6 h1:hKQ0gyocG7vgMD2M3dRlYN6WBBOmdoOzJ6njQ
github.com/tkrajina/go-reflector v0.5.6/go.mod h1:ECbqLgccecY5kPmPmXg1MrHW585yMcDkVl6IvJe64T4=
github.com/turbot/go-kit v1.0.0 h1:TeVYepMyPMpiQ8GxmZcth9h1CzKRshWK+X9JGKAnXOo=
github.com/turbot/go-kit v1.0.0/go.mod h1:vPk4gTUM8HhYGdzfKKLrPeZgnjLVBin41uqxjHScz6k=
github.com/turbot/go-kit v1.0.1-rc.0/go.mod h1:vPk4gTUM8HhYGdzfKKLrPeZgnjLVBin41uqxjHScz6k=
github.com/turbot/pipes-sdk-go v0.12.0 h1:esbbR7bALa5L8n/hqroMPaQSSo3gNM/4X0iTmHa3D6U=
github.com/turbot/pipes-sdk-go v0.12.0/go.mod h1:Mb+KhvqqEdRbz/6TSZc2QWDrMa5BN3E4Xw+gPt2TRkc=
github.com/turbot/steampipe-plugin-code v1.0.1-alpha.1 h1:mN0k0SGAN0pqPh92QZfJIzFzXuz6TiMALnLLLgCqnTI=
Expand Down
27 changes: 11 additions & 16 deletions parse/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,26 @@ import (
"strings"
"time"

"github.com/turbot/go-kit/helpers"
"github.com/turbot/pipe-fittings/v2/constants"
)

// ParseTime parses a time string into a time.Time object.
func ParseTime(input string, now time.Time) (time.Time, error) {
// Handle absolute time formats
absoluteLayouts := []string{
"2006-01-02", // ISO 8601 date
"2006-01-02T15:04:05", // ISO 8601 datetime
"2006-01-02T15:04:05.000", // ISO 8601 datetime with milliseconds
time.RFC3339, // RFC 3339 datetime with timezone
}

for _, layout := range absoluteLayouts {
if t, err := time.Parse(layout, input); err == nil {
return t.UTC(), nil // Normalize to UTC
}
}

// Handle relative formats
// short-circuit if time is relative
if strings.HasPrefix(input, "T-") {
return parseRelativeTime(input, now)
}

return time.Time{}, errors.New(constants.InvalidTimeFormat)
// Handle absolute time formats using go-kit helpers.ParseTime
t, err := helpers.ParseTime(input)
if err != nil {
// TODO #error improve the error message to link to docs for supported formats: https://github.com/turbot/pipe-fittings/issues/639
return time.Time{}, err
}

// normalize to UTC
return t.UTC(), nil
}

// parseRelativeTime parses relative time strings.
Expand Down

0 comments on commit 8c5beb4

Please sign in to comment.