github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_timestamp.go (about) 1 // Unless explicitly stated otherwise all files in this repository are licensed 2 // under the MIT License. 3 // This product includes software developed at Guance Cloud (https://www.guance.com/). 4 // Copyright 2021-present Guance, Inc. 5 6 package funcs 7 8 import ( 9 "time" 10 11 "github.com/GuanceCloud/platypus/pkg/ast" 12 "github.com/GuanceCloud/platypus/pkg/engine/runtime" 13 "github.com/GuanceCloud/platypus/pkg/errchain" 14 ) 15 16 func TimestampChecking(ctx *runtime.Task, funcExpr *ast.CallExpr) *errchain.PlError { 17 err := normalizeFuncArgsDeprecated(funcExpr, []string{"precision"}, 0) 18 if err != nil { 19 return runtime.NewRunError(ctx, err.Error(), funcExpr.NamePos) 20 } 21 return nil 22 } 23 24 func Timestamp(ctx *runtime.Task, funcExpr *ast.CallExpr) *errchain.PlError { 25 var precision string 26 if funcExpr.Param[0] != nil { 27 v, _, err := runtime.RunStmt(ctx, funcExpr.Param[0]) 28 if err != nil { 29 return err 30 } 31 if v, ok := v.(string); ok { 32 precision = v 33 } 34 } 35 var ts int64 36 switch precision { 37 case "us": 38 ts = time.Now().UnixMicro() 39 case "ms": 40 ts = time.Now().UnixMilli() 41 case "s": 42 ts = time.Now().Unix() 43 default: 44 ts = time.Now().UnixNano() 45 } 46 ctx.Regs.ReturnAppend(ts, ast.Int) 47 return nil 48 }