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 + }, +}