From 260af5090cc954ccc8ef6cf8f8bcf1c1566bb662 Mon Sep 17 00:00:00 2001 From: rainzm Date: Wed, 26 Jan 2022 11:49:21 +0800 Subject: [PATCH] fix(scheduledtask): format time from utc_offset --- cmd/climc/shell/scheduledtask/scheduledtask.go | 1 + pkg/scheduledtask/models/scheduled_tasks.go | 8 +++++--- pkg/scheduledtask/models/timer.go | 12 +++++------- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/cmd/climc/shell/scheduledtask/scheduledtask.go b/cmd/climc/shell/scheduledtask/scheduledtask.go index 90dc736300..0dcfcdc69d 100644 --- a/cmd/climc/shell/scheduledtask/scheduledtask.go +++ b/cmd/climc/shell/scheduledtask/scheduledtask.go @@ -41,6 +41,7 @@ func init() { ScheduledType string `help:"scheduled type" choices:"timing|cycle"` ResourceType string `help:"resource type"` Operation string `help:"operation"` + UtcOffset int `help:"utc offset"` } R(&ScheduledTaskListOptions{}, "scheduledtask-list", "list Scheduled Task", func(s *mcclient.ClientSession, args *ScheduledTaskListOptions) error { params, err := options.ListStructToParams(args) diff --git a/pkg/scheduledtask/models/scheduled_tasks.go b/pkg/scheduledtask/models/scheduled_tasks.go index 88b38ae197..0ce94a7304 100644 --- a/pkg/scheduledtask/models/scheduled_tasks.go +++ b/pkg/scheduledtask/models/scheduled_tasks.go @@ -118,11 +118,13 @@ func (stm *SScheduledTaskManager) FetchCustomizeColumns( fields stringutils2.SSortedStrings, isList bool, ) []api.ScheduledTaskDetails { + utcOffset, _ := query.Int("utc_offset") + zone := time.FixedZone("UTC", int(utcOffset)*3600) rows := make([]api.ScheduledTaskDetails, len(objs)) virRows := stm.SVirtualResourceBaseManager.FetchCustomizeColumns(ctx, userCred, query, objs, fields, isList) var err error for i := range rows { - rows[i], err = objs[i].(*SScheduledTask).getMoreDetails(ctx, userCred, query, isList) + rows[i], err = objs[i].(*SScheduledTask).getMoreDetails(ctx, userCred, query, isList, zone) if err != nil { log.Errorf("SScheduledTask.getMoreDetails error: %s", err) } @@ -131,7 +133,7 @@ func (stm *SScheduledTaskManager) FetchCustomizeColumns( return rows } -func (st *SScheduledTask) getMoreDetails(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, isList bool) (api.ScheduledTaskDetails, error) { +func (st *SScheduledTask) getMoreDetails(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, isList bool, zone *time.Location) (api.ScheduledTaskDetails, error) { var out api.ScheduledTaskDetails switch st.ScheduledType { case api.ST_TYPE_TIMING: @@ -139,7 +141,7 @@ func (st *SScheduledTask) getMoreDetails(ctx context.Context, userCred mcclient. case api.ST_TYPE_CYCLE: out.CycleTimer = st.STimer.CycleTimerDetails() } - out.TimerDesc = st.Description(ctx) + out.TimerDesc = st.Description(ctx, zone) // fill label stLabels, err := st.STLabels() if err != nil { diff --git a/pkg/scheduledtask/models/timer.go b/pkg/scheduledtask/models/timer.go index fb33c48a83..7ffb8f0698 100644 --- a/pkg/scheduledtask/models/timer.go +++ b/pkg/scheduledtask/models/timer.go @@ -172,13 +172,13 @@ func init() { timerDescTable.Set("timerLang", i18n.NewTableEntry().EN("en").CN("cn")) } -func (st *STimer) Description(ctx context.Context) string { +func (st *STimer) Description(ctx context.Context, zone *time.Location) string { lang := timerDescTable.Lookup(ctx, TIMERLANG) switch lang { case "en": - return st.descEnglish() + return st.descEnglish(zone) case "cn": - return st.descChinese() + return st.descChinese(zone) } return "" } @@ -186,11 +186,9 @@ func (st *STimer) Description(ctx context.Context) string { var ( wdsCN = []string{"", "一", "二", "三", "四", "五", "六", "日"} wdsEN = []string{"", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"} - zone = time.Now().Local().Location() - //zone = time.FixedZone("GMT", 8*3600) ) -func (st *STimer) descChinese() string { +func (st *STimer) descChinese(zone *time.Location) string { format := "2006-01-02 15:04:05" var prefix string switch st.Type { @@ -216,7 +214,7 @@ func (st *STimer) descChinese() string { return fmt.Sprintf("%s %02d:%02d触发 有效时间为%s至%s", prefix, st.Hour, st.Minute, st.StartTime.In(zone).Format(format), st.EndTime.In(zone).Format(format)) } -func (st *STimer) descEnglish() string { +func (st *STimer) descEnglish(zone *time.Location) string { var detail string format := "2006-01-02 15:04:05" switch st.Type {