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