github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_default_time_with_fmt.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 "fmt" 10 "time" 11 12 "github.com/GuanceCloud/cliutils/pipeline/ptinput" 13 "github.com/GuanceCloud/platypus/pkg/ast" 14 "github.com/GuanceCloud/platypus/pkg/engine/runtime" 15 "github.com/GuanceCloud/platypus/pkg/errchain" 16 ) 17 18 func DefaultTimeWithFmtChecking(ctx *runtime.Task, funcExpr *ast.CallExpr) *errchain.PlError { 19 if len(funcExpr.Param) < 2 { 20 return runtime.NewRunError(ctx, fmt.Sprintf( 21 "func %s expected more than 2 args", funcExpr.Name), funcExpr.NamePos) 22 } 23 24 switch funcExpr.Param[0].NodeType { //nolint:exhaustive 25 case ast.TypeAttrExpr, ast.TypeIdentifier: 26 default: 27 return runtime.NewRunError(ctx, fmt.Sprintf( 28 "param key expect AttrExpr or Identifier, got %s", 29 funcExpr.Param[0].NodeType), funcExpr.Param[0].StartPos()) 30 } 31 32 switch funcExpr.Param[1].NodeType { //nolint:exhaustive 33 case ast.TypeStringLiteral: 34 default: 35 return runtime.NewRunError(ctx, fmt.Sprintf( 36 "param key expect StringLiteral, got %s", 37 funcExpr.Param[1].NodeType), funcExpr.Param[1].StartPos()) 38 } 39 40 if len(funcExpr.Param) > 2 { 41 switch funcExpr.Param[2].NodeType { //nolint:exhaustive 42 case ast.TypeStringLiteral: 43 default: 44 return runtime.NewRunError(ctx, fmt.Sprintf( 45 "param key expect StringLiteral, got %s", 46 funcExpr.Param[2].NodeType), funcExpr.Param[2].StartPos()) 47 } 48 } 49 50 return nil 51 } 52 53 func DefaultTimeWithFmt(ctx *runtime.Task, funcExpr *ast.CallExpr) *errchain.PlError { 54 var err error 55 var goTimeFmt string 56 var tz string 57 var t time.Time 58 timezone := time.Local 59 60 if len(funcExpr.Param) < 2 { 61 return runtime.NewRunError(ctx, fmt.Sprintf( 62 "func %s expected more than 2 args", funcExpr.Name), funcExpr.NamePos) 63 } 64 65 key, err := getKeyName(funcExpr.Param[0]) 66 if err != nil { 67 return runtime.NewRunError(ctx, err.Error(), funcExpr.Param[0].StartPos()) 68 } 69 70 switch funcExpr.Param[1].NodeType { //nolint:exhaustive 71 case ast.TypeStringLiteral: 72 goTimeFmt = funcExpr.Param[1].StringLiteral().Val 73 default: 74 return runtime.NewRunError(ctx, fmt.Sprintf( 75 "param key expect StringLiteral, got %s", 76 funcExpr.Param[1]), funcExpr.Param[1].StartPos()) 77 } 78 79 if len(funcExpr.Param) > 2 { 80 switch funcExpr.Param[2].NodeType { //nolint:exhaustive 81 case ast.TypeStringLiteral: 82 tz = funcExpr.Param[2].StringLiteral().Val 83 default: 84 return runtime.NewRunError(ctx, fmt.Sprintf( 85 "param key expect StringLiteral, got %s", 86 funcExpr.Param[2].NodeType), funcExpr.Param[2].StartPos()) 87 } 88 } 89 90 timeStr, err := ctx.GetKeyConv2Str(key) 91 if err != nil { 92 l.Debugf("key `%v' not exist, ignored", key) 93 return nil //nolint:nilerr 94 } 95 96 if tz != "" { 97 timezone, err = time.LoadLocation(tz) 98 } 99 100 if err == nil { 101 t, err = time.ParseInLocation(goTimeFmt, timeStr, timezone) 102 } 103 104 if err != nil { 105 l.Debugf("time string: %s, time format: %s, timezone: %s, error msg: %s", 106 timeStr, goTimeFmt, tz, err) 107 return nil 108 } else { 109 _ = addKey2PtWithVal(ctx.InData(), key, t.UnixNano(), ast.Int, ptinput.KindPtDefault) 110 return nil 111 } 112 }