From 5b40f7875641303019e021183998aea8a7a67e47 Mon Sep 17 00:00:00 2001 From: Peter Grlica Date: Fri, 8 Apr 2022 11:48:18 +0200 Subject: [PATCH] Time transform expression function accepts strings also --- pkg/expr/func_time.go | 8 ++++++++ pkg/expr/func_time_test.go | 7 +++++++ 2 files changed, 15 insertions(+) diff --git a/pkg/expr/func_time.go b/pkg/expr/func_time.go index 0b63e94c7..d8df175c2 100644 --- a/pkg/expr/func_time.go +++ b/pkg/expr/func_time.go @@ -173,6 +173,14 @@ func prepMod(base interface{}, mod interface{}) (*time.Time, int, error) { t = &auxt case *time.Time: t = auxt + case string: + tt, err := cast.ToTimeE(auxt) + + if err != nil { + return nil, 0, err + } + + t = &tt default: return nil, 0, errors.New("unexpected input type") } diff --git a/pkg/expr/func_time_test.go b/pkg/expr/func_time_test.go index d333a47e1..22080341b 100644 --- a/pkg/expr/func_time_test.go +++ b/pkg/expr/func_time_test.go @@ -37,6 +37,13 @@ func Example_strftimeWithModTime() { // 1993-02-02T06:30:00 } +func Example_strftimeWithStringValue() { + eval(`strftime("1993-02-02T06:30:00", "%Y-%m-%dT%H:%M:%S")`, exampleTimeParams) + + // output: + // 1993-02-02T06:30:00 +} + func Example_parseISODate() { eval(`date("1993-02-02T06:00:00-05:00")`, nil)