1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-14 11:57:48 +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:
dreamjz 2024-01-30 15:47:02 +08:00 committed by GitHub
parent 831bf3c074
commit 08d098d262
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 24 additions and 7 deletions

View file

@ -110,15 +110,28 @@ func jpTimeAdd(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
} 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) {

View file

@ -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')",
expectedResult: "2021-01-02T15:04:05Z",
},
{
test: "time_parse('1702691171', '1702691171')",
expectedResult: "2023-12-16T01:46:11Z",
},
}
for i, tc := range testCases {
t.Run(fmt.Sprintf("case %d", i), func(t *testing.T) {