mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-31 03:45:17 +00:00
feat(jmespath):time_parse() support epoch time (#9173)
Signed-off-by: dreamjz <25699818+dreamjz@users.noreply.github.com> Co-authored-by: Vishal Choudhary <vishal.choudhary@nirmata.com> Co-authored-by: shuting <shuting@nirmata.com> Co-authored-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
This commit is contained in:
parent
831bf3c074
commit
08d098d262
2 changed files with 24 additions and 7 deletions
|
@ -110,15 +110,28 @@ func jpTimeAdd(arguments []interface{}) (interface{}, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func jpTimeParse(arguments []interface{}) (interface{}, error) {
|
func jpTimeParse(arguments []interface{}) (interface{}, error) {
|
||||||
if layout, err := validateArg(timeParse, arguments, 0, reflect.String); err != nil {
|
var err error
|
||||||
|
layout, err := validateArg(timeParse, arguments, 0, reflect.String)
|
||||||
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
} else if ts, err := validateArg(timeParse, arguments, 1, reflect.String); err != nil {
|
|
||||||
return nil, err
|
|
||||||
} else if t, err := time.Parse(layout.String(), ts.String()); err != nil {
|
|
||||||
return nil, err
|
|
||||||
} else {
|
|
||||||
return t.Format(time.RFC3339), nil
|
|
||||||
}
|
}
|
||||||
|
ts, err := validateArg(timeParse, arguments, 1, reflect.String)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
_, err = strconv.ParseInt(layout.String(), 10, 64)
|
||||||
|
if err == nil { // epoch time layout
|
||||||
|
epochTime, err := strconv.ParseInt(ts.String(), 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return time.Unix(epochTime, 0).UTC().Format(time.RFC3339), nil
|
||||||
|
}
|
||||||
|
t, err := time.Parse(layout.String(), ts.String())
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return t.Format(time.RFC3339), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func jpTimeUtc(arguments []interface{}) (interface{}, error) {
|
func jpTimeUtc(arguments []interface{}) (interface{}, error) {
|
||||||
|
|
|
@ -112,6 +112,10 @@ func Test_TimeParse(t *testing.T) {
|
||||||
test: "time_parse('Mon Jan 02 15:04:05 MST 2006', 'Sat Jan 02 15:04:05 MST 2021')",
|
test: "time_parse('Mon Jan 02 15:04:05 MST 2006', 'Sat Jan 02 15:04:05 MST 2021')",
|
||||||
expectedResult: "2021-01-02T15:04:05Z",
|
expectedResult: "2021-01-02T15:04:05Z",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
test: "time_parse('1702691171', '1702691171')",
|
||||||
|
expectedResult: "2023-12-16T01:46:11Z",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
for i, tc := range testCases {
|
for i, tc := range testCases {
|
||||||
t.Run(fmt.Sprintf("case %d", i), func(t *testing.T) {
|
t.Run(fmt.Sprintf("case %d", i), func(t *testing.T) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue