From d73ceaaaa5278121803fded5068153e55faefac0 Mon Sep 17 00:00:00 2001 From: naiba Date: Fri, 18 Mar 2022 23:45:03 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20=E4=BF=AE=E5=A4=8D=20Windows=20t?= =?UTF-8?q?ime.LoadLocation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/dashboard/controller/controller.go | 5 ++--- model/common.go | 10 ---------- service/singleton/singleton.go | 10 ++++++++++ 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/cmd/dashboard/controller/controller.go b/cmd/dashboard/controller/controller.go index 1b8dbc0..2b576e9 100644 --- a/cmd/dashboard/controller/controller.go +++ b/cmd/dashboard/controller/controller.go @@ -13,7 +13,6 @@ import ( "github.com/gin-contrib/pprof" "github.com/gin-gonic/gin" - "github.com/naiba/nezha/model" "github.com/naiba/nezha/pkg/mygin" "github.com/naiba/nezha/service/singleton" ) @@ -28,7 +27,7 @@ func ServeWeb(port uint) *http.Server { r.Use(mygin.RecordPath) r.SetFuncMap(template.FuncMap{ "tf": func(t time.Time) string { - return t.In(model.Loc).Format("2006年1月2号 15:04:05") + return t.In(singleton.Loc).Format("2006年1月2号 15:04:05") }, "len": func(slice []interface{}) string { return strconv.Itoa(len(slice)) @@ -40,7 +39,7 @@ func ServeWeb(port uint) *http.Server { return template.HTML(`<` + s + `>`) // #nosec }, "stf": func(s uint64) string { - return time.Unix(int64(s), 0).In(model.Loc).Format("2006年1月2号 15:04") + return time.Unix(int64(s), 0).In(singleton.Loc).Format("2006年1月2号 15:04") }, "sf": func(duration uint64) string { return time.Duration(time.Duration(duration) * time.Second).String() diff --git a/model/common.go b/model/common.go index 1596b38..05ac8a7 100644 --- a/model/common.go +++ b/model/common.go @@ -6,16 +6,6 @@ const CtxKeyAuthorizedUser = "ckau" const CacheKeyOauth2State = "p:a:state" -var Loc *time.Location - -func init() { - var err error - Loc, err = time.LoadLocation("Asia/Shanghai") - if err != nil { - panic(err) - } -} - type Common struct { ID uint64 `gorm:"primary_key"` CreatedAt time.Time `sql:"index"` diff --git a/service/singleton/singleton.go b/service/singleton/singleton.go index 3a1a2df..f0c1d78 100644 --- a/service/singleton/singleton.go +++ b/service/singleton/singleton.go @@ -4,6 +4,7 @@ import ( "fmt" "sort" "sync" + "time" "github.com/patrickmn/go-cache" "github.com/robfig/cron/v3" @@ -20,6 +21,7 @@ var ( Conf *model.Config Cache *cache.Cache DB *gorm.DB + Loc *time.Location ServerList map[uint64]*model.Server SecretToID map[string]uint64 @@ -29,6 +31,14 @@ var ( SortedServerLock sync.RWMutex ) +func init() { + var err error + Loc, err = time.LoadLocation("Asia/Shanghai") + if err != nil { + panic(err) + } +} + func ReSortServer() { ServerLock.RLock() defer ServerLock.RUnlock()