From 863ed0ce6e7ed4e0d0ae0bb0f4b57695a1c0ba12 Mon Sep 17 00:00:00 2001 From: pradeepnnv Date: Thu, 23 Feb 2023 21:03:44 -0600 Subject: [PATCH] add now function Signed-off-by: pradeepnnv --- .../transform/action/datetime/now.go | 18 ++++++++++++++++ .../transform/function/datatime_functions.go | 21 +++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 internal/primitive/transform/action/datetime/now.go diff --git a/internal/primitive/transform/action/datetime/now.go b/internal/primitive/transform/action/datetime/now.go new file mode 100644 index 000000000..b571b0a2a --- /dev/null +++ b/internal/primitive/transform/action/datetime/now.go @@ -0,0 +1,18 @@ +package datetime + +import ( + "github.com/linkall-labs/vanus/internal/primitive/transform/action" + "github.com/linkall-labs/vanus/internal/primitive/transform/arg" + "github.com/linkall-labs/vanus/internal/primitive/transform/function" +) + +func now() action.Action { + a := &action.SourceTargetSameAction{} + a.CommonAction = action.CommonAction{ + ActionName: "NOW", + FixedArgs: []arg.TypeList{arg.EventList, arg.All}, + VariadicArg: arg.All, + Fn: function.NowFunction, + } + return a +} diff --git a/internal/primitive/transform/function/datatime_functions.go b/internal/primitive/transform/function/datatime_functions.go index 908befb39..7e4729eba 100644 --- a/internal/primitive/transform/function/datatime_functions.go +++ b/internal/primitive/transform/function/datatime_functions.go @@ -60,3 +60,24 @@ var UnixTimeFormatFunction = function{ return t.In(loc).Format(format), nil }, } + +var NowFunction = function{ + name: "NOW", + fixedArgs: []common.Type{common.String, common.String}, + variadicArgs: common.TypePtr(common.String), + fn: func(args []interface{}) (interface{}, error) { + t, err := time.ParseInLocation(time.RFC3339, args[0].(string), time.UTC) + if err != nil { + return nil, err + } + loc := time.UTC + if len(args) > 2 && args[2].(string) != "" { + loc, err = time.LoadLocation(args[2].(string)) + if err != nil { + return nil, err + } + } + format := util.ConvertFormat2Go(args[1].(string)) + return t.In(loc).Format(format), nil + }, +}