From 669163992b681495671b82d4d189478e783f584f Mon Sep 17 00:00:00 2001 From: Puskar Basu Date: Tue, 7 Jan 2025 19:52:10 +0530 Subject: [PATCH 1/2] Update connect to use time parsing function to support all expected time formats. Closes #85 --- cmd/connect.go | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/cmd/connect.go b/cmd/connect.go index 9c9dd15d..ba35cfd7 100644 --- a/cmd/connect.go +++ b/cmd/connect.go @@ -19,6 +19,7 @@ import ( "github.com/turbot/pipe-fittings/connection" pconstants "github.com/turbot/pipe-fittings/constants" "github.com/turbot/pipe-fittings/error_helpers" + "github.com/turbot/pipe-fittings/parse" "github.com/turbot/tailpipe/internal/config" "github.com/turbot/tailpipe/internal/constants" "github.com/turbot/tailpipe/internal/database" @@ -125,26 +126,30 @@ func getFilters() ([]string, error) { var result []string if viper.IsSet(pconstants.ArgFrom) { from := viper.GetString(pconstants.ArgFrom) - // verify this is a valid date - t, err := time.Parse(time.RFC3339, from) + // parse the string as time.Time + // arg `from` accepts ISO 8601 date(2024-01-01), ISO 8601 datetime(2006-01-02T15:04:05), ISO 8601 datetime with ms(2006-01-02T15:04:05.000), + // RFC 3339 datetime with timezone(2006-01-02T15:04:05Z07:00) and relative time formats(T-2Y, T-10m, T-10W, T-180d, T-9H, T-10M) + t, err := parse.ParseTime(from, time.Now()) if err != nil { return nil, fmt.Errorf("invalid date format for 'from': %s", from) } // format as SQL timestamp - fromDate := t.Format("2006-01-02") - fromTimestamp := t.Format("2006-01-02 15:04:05") + fromDate := t.Format(time.DateOnly) + fromTimestamp := t.Format(time.DateTime) result = append(result, fmt.Sprintf("tp_date >= DATE '%s' AND tp_timestamp >= TIMESTAMP '%s'", fromDate, fromTimestamp)) } if viper.IsSet(pconstants.ArgTo) { to := viper.GetString(pconstants.ArgTo) - // verify this is a valid date - t, err := time.Parse(time.RFC3339, to) + // parse the string as time.Time + // arg `to` accepts ISO 8601 date(2024-01-01), ISO 8601 datetime(2006-01-02T15:04:05), ISO 8601 datetime with ms(2006-01-02T15:04:05.000), + // RFC 3339 datetime with timezone(2006-01-02T15:04:05Z07:00) and relative time formats(T-2Y, T-10m, T-10W, T-180d, T-9H, T-10M) + t, err := parse.ParseTime(to, time.Now()) if err != nil { return nil, fmt.Errorf("invalid date format for 'to': %s", to) } // format as SQL timestamp - toDate := t.Format("2006-01-02") - toTimestamp := t.Format("2006-01-02 15:04:05") + toDate := t.Format(time.DateOnly) + toTimestamp := t.Format(time.DateTime) result = append(result, fmt.Sprintf("tp_date <= DATE '%s' AND tp_timestamp <= TIMESTAMP '%s'", toDate, toTimestamp)) } return result, nil From 1034e046748aa6300dea998aab7f448620e82875 Mon Sep 17 00:00:00 2001 From: Puskar Basu Date: Tue, 7 Jan 2025 20:07:10 +0530 Subject: [PATCH 2/2] fix linting --- cmd/collect.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/collect.go b/cmd/collect.go index b85b74f3..f0e3f7b8 100644 --- a/cmd/collect.go +++ b/cmd/collect.go @@ -207,7 +207,7 @@ func getPartitionsForArg(partitions []string, arg string) ([]string, error) { return res, nil } -func getPartitionMatchPatterns(partitions []string, arg string, parts []string) (string, string, error) { //nolint:staticcheck // TODO is tablePattern required as input? +func getPartitionMatchPatterns(partitions []string, arg string, parts []string) (string, string, error) { var tablePattern, partitionPattern string // '*' is not valid for a single part arg if parts[0] == "*" {