From 31eacd6c293958ae4c2e719495ac31858106bab2 Mon Sep 17 00:00:00 2001 From: soulseen Date: Wed, 18 Sep 2019 14:57:15 +0800 Subject: [PATCH] fix cronjob msg parse Signed-off-by: soulseen --- pkg/models/devops/devops.go | 16 ++++++++++------ pkg/models/devops/devops_test.go | 6 +++++- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/pkg/models/devops/devops.go b/pkg/models/devops/devops.go index 3ea0ce124..87c22bbe4 100644 --- a/pkg/models/devops/devops.go +++ b/pkg/models/devops/devops.go @@ -779,18 +779,22 @@ func parseCronJobTime(msg string) (string, string, error) { times := strings.Split(msg, ";") - lastTmp := strings.SplitN(times[0], " ", 2) - lastTime := strings.SplitN(lastTmp[1], " UTC", 2) - lastUinx, err := time.Parse(cronJobLayout, lastTime[0]) + lastTmp := strings.Split(times[0], " ") + lastCount := len(lastTmp) + lastTmp = lastTmp[lastCount-7 : lastCount-1] + lastTime := strings.Join(lastTmp, " ") + lastUinx, err := time.Parse(cronJobLayout, lastTime) if err != nil { klog.Error(err) return "", "", err } last := lastUinx.Format(time.RFC3339) - nextTmp := strings.SplitN(times[1], " ", 3) - nextTime := strings.SplitN(nextTmp[2], " UTC", 2) - nextUinx, err := time.Parse(cronJobLayout, nextTime[0]) + nextTmp := strings.Split(times[1], " ") + nextCount := len(nextTmp) + nextTmp = nextTmp[nextCount-7 : nextCount-1] + nextTime := strings.Join(nextTmp, " ") + nextUinx, err := time.Parse(cronJobLayout, nextTime) if err != nil { klog.Error(err) return "", "", err diff --git a/pkg/models/devops/devops_test.go b/pkg/models/devops/devops_test.go index c19def6be..3749d6041 100644 --- a/pkg/models/devops/devops_test.go +++ b/pkg/models/devops/devops_test.go @@ -1,6 +1,8 @@ package devops -import "testing" +import ( + "testing" +) func Test_parseCronJobTime(t *testing.T) { type Except struct { @@ -16,6 +18,8 @@ func Test_parseCronJobTime(t *testing.T) { {"上次运行的时间 Thursday, January 3, 2019 11:56:30 PM UTC; 下次运行的时间 Friday, January 3, 2020 12:11:30 AM UTC.", Except{Last: "2019-01-03T23:56:30Z", Next: "2020-01-03T00:11:30Z"}}, {"上次运行的时间 Tuesday, September 10, 2019 8:41:34 AM UTC; 下次运行的时间 Tuesday, September 10, 2019 9:41:34 AM UTC.", Except{Last: "2019-09-10T08:41:34Z", Next: "2019-09-10T09:41:34Z"}}, {"上次运行的时间 Tuesday, September 10, 2019 9:15:26 AM UTC; 下次运行的时间 Tuesday, September 10, 2019 10:03:26 AM UTC.", Except{Last: "2019-09-10T09:15:26Z", Next: "2019-09-10T10:03:26Z"}}, + {"Would last have run at Tuesday, September 10, 2019 9:15:26 AM UTC; would next run at Tuesday, September 10, 2019 10:03:26 AM UTC.", Except{Last: "2019-09-10T09:15:26Z", Next: "2019-09-10T10:03:26Z"}}, + {"Would last have run at Tuesday, September 10, 2019 8:41:34 AM UTC; would next run at Tuesday, September 10, 2019 9:41:34 AM UTC.", Except{Last: "2019-09-10T08:41:34Z", Next: "2019-09-10T09:41:34Z"}}, } for _, item := range Items {