github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_parse_date.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 "strconv" 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 "github.com/spf13/cast" 17 ) 18 19 func ParseDateChecking(ctx *runtime.Task, funcExpr *ast.CallExpr) *errchain.PlError { 20 if err := normalizeFuncArgsDeprecated(funcExpr, []string{ 21 "key", "yy", "MM", "dd", "hh", "mm", "ss", "ms", "us", "ns", "zone", 22 }, 0); err != nil { 23 return runtime.NewRunError(ctx, err.Error(), funcExpr.NamePos) 24 } 25 return nil 26 } 27 28 func ParseDate(ctx *runtime.Task, funcExpr *ast.CallExpr) *errchain.PlError { 29 now := time.Now() 30 var key string 31 32 var zone *time.Location 33 34 var yy int 35 var MM time.Month 36 var dd, hh, mm, ss, ms, us, ns int 37 38 key, err := getKeyName(funcExpr.Param[0]) 39 if err != nil { 40 return runtime.NewRunError(ctx, err.Error(), funcExpr.Param[0].StartPos()) 41 } 42 43 // year 44 if x := getArgStrConv2Int(ctx, funcExpr.Param[1]); true { 45 yy, err = fixYear(now, x) 46 if err != nil { 47 return runtime.NewRunError(ctx, err.Error(), funcExpr.Param[1].StartPos()) 48 } 49 } 50 51 // month 52 if x := getArgStr(ctx, funcExpr.Param[2]); true { 53 MM, err = fixMonth(now, x) 54 if err != nil { 55 return runtime.NewRunError(ctx, err.Error(), funcExpr.Param[2].StartPos()) 56 } 57 } 58 59 // day 60 if x := getArgStrConv2Int(ctx, funcExpr.Param[3]); true { 61 dd, err = fixDay(now, x) 62 if err != nil { 63 return runtime.NewRunError(ctx, err.Error(), funcExpr.Param[3].StartPos()) 64 } 65 } 66 67 // hour 68 if x := getArgStrConv2Int(ctx, funcExpr.Param[4]); true { 69 hh, err = fixHour(now, x) 70 if err != nil { 71 return runtime.NewRunError(ctx, err.Error(), funcExpr.Param[4].StartPos()) 72 } 73 } 74 // mm 75 if x := getArgStrConv2Int(ctx, funcExpr.Param[5]); true { 76 mm, err = fixMinute(now, x) 77 if err != nil { 78 return runtime.NewRunError(ctx, err.Error(), funcExpr.Param[5].StartPos()) 79 } 80 } 81 82 // ss 83 if x := getArgStrConv2Int(ctx, funcExpr.Param[6]); true { 84 ss, err = fixSecond(x) 85 if err != nil { 86 return runtime.NewRunError(ctx, err.Error(), funcExpr.Param[6].StartPos()) 87 } 88 } 89 // ms 90 if x := getArgStrConv2Int(ctx, funcExpr.Param[7]); true { 91 if x == DefaultInt { 92 ms = 0 93 } else { 94 ms = int(x) 95 } 96 } 97 98 // us 99 if x := getArgStrConv2Int(ctx, funcExpr.Param[8]); true { 100 if x == DefaultInt { 101 us = 0 102 } else { 103 us = int(x) 104 } 105 } 106 107 // ns 108 if x := getArgStrConv2Int(ctx, funcExpr.Param[9]); true { 109 if x == DefaultInt { 110 ns = 0 111 } else { 112 ns = int(x) 113 } 114 } 115 116 if x := getArgStr(ctx, funcExpr.Param[10]); true { 117 if x == "" { 118 zone = time.UTC 119 } else { 120 zone, err = tz(x) 121 if err != nil { 122 return runtime.NewRunError(ctx, err.Error(), funcExpr.Param[10].StartPos()) 123 } 124 } 125 } 126 127 t := time.Date(yy, MM, dd, hh, mm, ss, ms*1000*1000+us*1000+ns, zone) 128 129 _ = addKey2PtWithVal(ctx.InData(), key, t.UnixNano(), ast.Int, ptinput.KindPtDefault) 130 131 return nil 132 } 133 134 func getArgStr(ctx *runtime.Task, node *ast.Node) string { 135 if node == nil { 136 return "" 137 } 138 139 if v, dtype, err := runtime.RunStmt(ctx, node); err == nil { 140 if dtype == ast.String { 141 return cast.ToString(v) 142 } 143 } 144 return "" 145 } 146 147 func getArgStrConv2Int(ctx *runtime.Task, node *ast.Node) int64 { 148 str := getArgStr(ctx, node) 149 if str == "" { 150 return DefaultInt 151 } 152 153 v, err := strconv.ParseInt(str, 10, 64) //nolint: gomnd 154 if err != nil { 155 return DefaultInt 156 } 157 return v 158 }