🐛 修复 Windows time.LoadLocation

This commit is contained in:
naiba 2022-03-18 23:45:03 +08:00
parent 3ca23d8d88
commit d73ceaaaa5
3 changed files with 12 additions and 13 deletions

View File

@ -13,7 +13,6 @@ import (
"github.com/gin-contrib/pprof" "github.com/gin-contrib/pprof"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/naiba/nezha/model"
"github.com/naiba/nezha/pkg/mygin" "github.com/naiba/nezha/pkg/mygin"
"github.com/naiba/nezha/service/singleton" "github.com/naiba/nezha/service/singleton"
) )
@ -28,7 +27,7 @@ func ServeWeb(port uint) *http.Server {
r.Use(mygin.RecordPath) r.Use(mygin.RecordPath)
r.SetFuncMap(template.FuncMap{ r.SetFuncMap(template.FuncMap{
"tf": func(t time.Time) string { "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 { "len": func(slice []interface{}) string {
return strconv.Itoa(len(slice)) return strconv.Itoa(len(slice))
@ -40,7 +39,7 @@ func ServeWeb(port uint) *http.Server {
return template.HTML(`<` + s + `>`) // #nosec return template.HTML(`<` + s + `>`) // #nosec
}, },
"stf": func(s uint64) string { "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 { "sf": func(duration uint64) string {
return time.Duration(time.Duration(duration) * time.Second).String() return time.Duration(time.Duration(duration) * time.Second).String()

View File

@ -6,16 +6,6 @@ const CtxKeyAuthorizedUser = "ckau"
const CacheKeyOauth2State = "p:a:state" 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 { type Common struct {
ID uint64 `gorm:"primary_key"` ID uint64 `gorm:"primary_key"`
CreatedAt time.Time `sql:"index"` CreatedAt time.Time `sql:"index"`

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"sort" "sort"
"sync" "sync"
"time"
"github.com/patrickmn/go-cache" "github.com/patrickmn/go-cache"
"github.com/robfig/cron/v3" "github.com/robfig/cron/v3"
@ -20,6 +21,7 @@ var (
Conf *model.Config Conf *model.Config
Cache *cache.Cache Cache *cache.Cache
DB *gorm.DB DB *gorm.DB
Loc *time.Location
ServerList map[uint64]*model.Server ServerList map[uint64]*model.Server
SecretToID map[string]uint64 SecretToID map[string]uint64
@ -29,6 +31,14 @@ var (
SortedServerLock sync.RWMutex SortedServerLock sync.RWMutex
) )
func init() {
var err error
Loc, err = time.LoadLocation("Asia/Shanghai")
if err != nil {
panic(err)
}
}
func ReSortServer() { func ReSortServer() {
ServerLock.RLock() ServerLock.RLock()
defer ServerLock.RUnlock() defer ServerLock.RUnlock()