github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_datetime.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  	timefmt "github.com/itchyny/timefmt-go"
    17  	conv "github.com/spf13/cast"
    18  )
    19  
    20  func DateTimeChecking(ctx *runtime.Task, funcExpr *ast.CallExpr) *errchain.PlError {
    21  	if err := normalizeFuncArgsDeprecated(funcExpr, []string{
    22  		"key", "precision", "fmt", "tz",
    23  	}, 3); err != nil {
    24  		return runtime.NewRunError(ctx, err.Error(), funcExpr.NamePos)
    25  	}
    26  
    27  	if _, err := getKeyName(funcExpr.Param[0]); err != nil {
    28  		return runtime.NewRunError(ctx, err.Error(), funcExpr.Param[0].StartPos())
    29  	}
    30  
    31  	switch funcExpr.Param[1].NodeType { //nolint:exhaustive
    32  	case ast.TypeStringLiteral:
    33  	default:
    34  		return runtime.NewRunError(ctx, fmt.Sprintf(
    35  			"param `precision` expect StringLiteral, got %s",
    36  			funcExpr.Param[1].NodeType), funcExpr.Param[1].StartPos())
    37  	}
    38  
    39  	switch funcExpr.Param[2].NodeType { //nolint:exhaustive
    40  	case ast.TypeStringLiteral:
    41  	default:
    42  		return runtime.NewRunError(ctx, fmt.Sprintf(
    43  			"param `fmt` expect StringLiteral, got %s",
    44  			funcExpr.Param[2].NodeType), funcExpr.Param[2].StartPos())
    45  	}
    46  
    47  	if funcExpr.Param[3] != nil {
    48  		switch funcExpr.Param[3].NodeType { //nolint:exhaustive
    49  		case ast.TypeStringLiteral:
    50  		default:
    51  			return runtime.NewRunError(ctx, fmt.Sprintf(
    52  				"param `tz` expect StringLiteral, got %s",
    53  				funcExpr.Param[2].NodeType), funcExpr.Param[3].StartPos())
    54  		}
    55  	}
    56  
    57  	// switch
    58  	return nil
    59  }
    60  
    61  func DateTime(ctx *runtime.Task, funcExpr *ast.CallExpr) *errchain.PlError {
    62  	if len(funcExpr.Param) < 3 {
    63  		return runtime.NewRunError(ctx, fmt.Sprintf(
    64  			"func %s expected 3 or 4 args", funcExpr.Name), funcExpr.NamePos)
    65  	}
    66  
    67  	key, err := getKeyName(funcExpr.Param[0])
    68  	if err != nil {
    69  		return runtime.NewRunError(ctx, err.Error(), funcExpr.Param[0].StartPos())
    70  	}
    71  
    72  	var precision, fmts string
    73  
    74  	switch funcExpr.Param[1].NodeType { //nolint:exhaustive
    75  	case ast.TypeStringLiteral:
    76  		precision = funcExpr.Param[1].StringLiteral().Val
    77  	default:
    78  		return runtime.NewRunError(ctx, fmt.Sprintf(
    79  			"param `precision` expect StringLiteral, got %s",
    80  			funcExpr.Param[1].NodeType), funcExpr.Param[1].StartPos())
    81  	}
    82  
    83  	switch funcExpr.Param[2].NodeType { //nolint:exhaustive
    84  	case ast.TypeStringLiteral:
    85  		fmts = funcExpr.Param[2].StringLiteral().Val
    86  	default:
    87  		return runtime.NewRunError(ctx, fmt.Sprintf(
    88  			"param `fmt` expect StringLiteral, got %s",
    89  			funcExpr.Param[2].NodeType), funcExpr.Param[2].StartPos())
    90  	}
    91  
    92  	cont, err := ctx.GetKey(key)
    93  	if err != nil {
    94  		l.Debugf("key `%v' not exist, ignored", key)
    95  		return nil //nolint:nilerr
    96  	}
    97  
    98  	ts := conv.ToInt64(cont.Value)
    99  
   100  	switch precision {
   101  	case "us":
   102  		ts *= 1e3
   103  	case "ms":
   104  		ts *= 1e6
   105  	case "s":
   106  		ts *= 1e9
   107  	default:
   108  	}
   109  
   110  	t := time.Unix(0, ts)
   111  
   112  	var tz string
   113  	if funcExpr.Param[3] != nil {
   114  		if funcExpr.Param[3].NodeType == ast.TypeStringLiteral {
   115  			tz = funcExpr.Param[3].StringLiteral().Val
   116  		} else {
   117  			return runtime.NewRunError(ctx, fmt.Sprintf(
   118  				"param `tz` expect StringLiteral, got %s",
   119  				funcExpr.Param[2].NodeType), funcExpr.Param[3].StartPos())
   120  		}
   121  	}
   122  
   123  	if tz != "" {
   124  		loc, err := time.LoadLocation(tz)
   125  		if err != nil {
   126  			return runtime.NewRunError(ctx, err.Error(), funcExpr.Param[2].StartPos())
   127  		}
   128  		t = t.In(loc)
   129  	}
   130  
   131  	if datetimeInnerFormat(fmts) {
   132  		v, err := DateFormatHandle(&t, fmts)
   133  		if err != nil {
   134  			return runtime.NewRunError(ctx, err.Error(), funcExpr.NamePos)
   135  		}
   136  		_ = addKey2PtWithVal(ctx.InData(), key, v, ast.String, ptinput.KindPtDefault)
   137  	} else {
   138  		_ = addKey2PtWithVal(ctx.InData(), key, timefmt.Format(t, fmts),
   139  			ast.String, ptinput.KindPtDefault)
   140  	}
   141  
   142  	return nil
   143  }