fix: time in location

This commit is contained in:
deepzz0
2021-04-28 15:37:10 +08:00
parent 872d0b1987
commit 43d7a26e19
6 changed files with 79 additions and 13 deletions

View File

@@ -24,6 +24,7 @@ blogapp:
length: 400 # 自动截取预览, 字符数 length: 400 # 自动截取预览, 字符数
trash: -48 # 回收箱保留48小时 trash: -48 # 回收箱保留48小时
clean: 1 # 定时清理回收箱,每 %d 小时 clean: 1 # 定时清理回收箱,每 %d 小时
timezone: Asia/Shanghai # 时区
disqus: # 评论相关 disqus: # 评论相关
shortname: xxxxxx shortname: xxxxxx
publickey: wdSgxRm9rdGAlLKFcFdToBe3GT4SibmV7Y8EjJQ0r4GWXeKtxpopMAeIeoI2dTEg publickey: wdSgxRm9rdGAlLKFcFdToBe3GT4SibmV7Y8EjJQ0r4GWXeKtxpopMAeIeoI2dTEg
@@ -56,4 +57,4 @@ blogapp:
backupapp: backupapp:
name: cmd-backup name: cmd-backup
enablehttp: true enablehttp: true
httpport: 9000 httpport: 9001

20
pkg/cache/cache.go vendored
View File

@@ -32,6 +32,14 @@ var (
) )
func init() { func init() {
// init timezone
var err error
tools.TimeLocation, err = time.LoadLocation(
config.Conf.BlogApp.General.Timezone)
if err != nil {
panic(err)
}
// init store
store, err := store.NewStore(config.Conf.Database.Driver, store, err := store.NewStore(config.Conf.Database.Driver,
config.Conf.Database.Source) config.Conf.Database.Source)
if err != nil { if err != nil {
@@ -514,23 +522,25 @@ func (c *Cache) regeneratePages() {
gt12Month = len(Ei.Archives) > 12 gt12Month = len(Ei.Archives) > 12
) )
for _, archive := range c.Archives { for _, archive := range c.Archives {
t := archive.Time.In(tools.TimeLocation)
if gt12Month { if gt12Month {
year := archive.Time.Format("2006 年") year := t.Format("2006 年")
if currentYear != year { if currentYear != year {
currentYear = year currentYear = year
buf.WriteString(fmt.Sprintf("\n### %s\n\n", archive.Time.Format("2006 年"))) buf.WriteString(fmt.Sprintf("\n### %s\n\n", t.Format("2006 年")))
} }
} else { } else {
buf.WriteString(fmt.Sprintf("\n### %s\n\n", archive.Time.Format("2006年1月"))) buf.WriteString(fmt.Sprintf("\n### %s\n\n", t.Format("2006年1月")))
} }
for i, article := range archive.Articles { for i, article := range archive.Articles {
createdAt := article.CreatedAt.In(time.Location)
if i == 0 && gt12Month { if i == 0 && gt12Month {
str := fmt.Sprintf(`* *[%s](/post/%s.html) <span class="date">(%s)</span>`, str := fmt.Sprintf(`* *[%s](/post/%s.html) <span class="date">(%s)</span>`,
article.Title, article.Slug, article.CreatedAt.Format("Jan 02, 2006")) article.Title, article.Slug, createdAt.Format("Jan 02, 2006"))
buf.WriteString(str) buf.WriteString(str)
} else { } else {
str := fmt.Sprintf(`* [%s](/post/%s.html) <span class="date">(%s)</span>`, str := fmt.Sprintf(`* [%s](/post/%s.html) <span class="date">(%s)</span>`,
article.Title, article.Slug, article.CreatedAt.Format("Jan 02, 2006")) article.Title, article.Slug, createdAt.Format("Jan 02, 2006"))
buf.WriteString(str) buf.WriteString(str)
} }
buf.WriteByte('\n') buf.WriteByte('\n')

View File

@@ -48,6 +48,7 @@ type General struct {
Length int `yaml:"length"` // 文章预览长度 Length int `yaml:"length"` // 文章预览长度
Trash int `yaml:"trash"` // 回收箱文章保留时间 Trash int `yaml:"trash"` // 回收箱文章保留时间
Clean int `yaml:"clean"` // 清理回收箱频率 Clean int `yaml:"clean"` // 清理回收箱频率
Timezone string `yaml:"timezone"` // 时区
} }
// Disqus comments // Disqus comments

View File

@@ -227,13 +227,14 @@ func handleAPIPostCreate(c *gin.Context) {
cid int cid int
) )
defer func() { defer func() {
now := time.Now().In(tools.TimeLocation)
switch do { switch do {
case "auto": // 自动保存 case "auto": // 自动保存
if err != nil { if err != nil {
c.JSON(http.StatusOK, gin.H{"fail": 1, "time": time.Now().Format("15:04:05 PM"), "cid": cid}) c.JSON(http.StatusOK, gin.H{"fail": 1, "time": now.Format("15:04:05 PM"), "cid": cid})
return return
} }
c.JSON(http.StatusOK, gin.H{"success": 0, "time": time.Now().Format("15:04:05 PM"), "cid": cid}) c.JSON(http.StatusOK, gin.H{"success": 0, "time": now.Format("15:04:05 PM"), "cid": cid})
case "save", "publish": // 草稿,发布 case "save", "publish": // 草稿,发布
if err != nil { if err != nil {
responseNotice(c, NoticeNotice, err.Error(), "") responseNotice(c, NoticeNotice, err.Error(), "")
@@ -498,9 +499,9 @@ func handleAPIQiniuDelete(c *gin.Context) {
// parseLocationDate 解析日期 // parseLocationDate 解析日期
func parseLocationDate(date string) time.Time { func parseLocationDate(date string) time.Time {
t, err := time.ParseInLocation("2006-01-02 15:04", date, time.Local) t, err := time.ParseInLocation("2006-01-02 15:04", date, tools.TimeLocation)
if err == nil { if err == nil {
return t return t.UTC()
} }
return time.Now() return time.Now()
} }

View File

@@ -12,7 +12,12 @@ import (
"time" "time"
) )
var TplFuncMap = make(template.FuncMap) var (
// TplFuncMap template func map
TplFuncMap = make(template.FuncMap)
// TimeLocation set location timezone
TimeLocation = time.UTC
)
func init() { func init() {
TplFuncMap["dateformat"] = DateFormat TplFuncMap["dateformat"] = DateFormat
@@ -22,19 +27,23 @@ func init() {
TplFuncMap["getavatar"] = GetAvatar TplFuncMap["getavatar"] = GetAvatar
} }
// Str2html string to html
func Str2html(raw string) htmpl.HTML { func Str2html(raw string) htmpl.HTML {
return htmpl.HTML(raw) return htmpl.HTML(raw)
} }
// DateFormat takes a time and a layout string and returns a string with the formatted date. Used by the template parser as "dateformat" // DateFormat takes a time and a layout string and returns a string with the formatted date.
// Used by the template parser as "dateformat"
func DateFormat(t time.Time, layout string) string { func DateFormat(t time.Time, layout string) string {
return t.Format(layout) return t.In(TimeLocation).Format(layout)
} }
// Join join string array with sep
func Join(a []string, sep string) string { func Join(a []string, sep string) string {
return strings.Join(a, sep) return strings.Join(a, sep)
} }
// IsNotZero judge t is zero
func IsNotZero(t time.Time) bool { func IsNotZero(t time.Time) bool {
return !t.IsZero() return !t.IsZero()
} }
@@ -43,6 +52,7 @@ func IsNotZero(t time.Time) bool {
// url: https://<static_domain>/static/img/avatar.png // url: https://<static_domain>/static/img/avatar.png
var avatar string var avatar string
// GetAvatar store avatar base64 into css
func GetAvatar(domain string) string { func GetAvatar(domain string) string {
if avatar == "" { if avatar == "" {
resp, err := http.Get("https://" + domain + "/static/img/avatar.png") resp, err := http.Get("https://" + domain + "/static/img/avatar.png")

43
tools/tmplfunc_test.go Normal file
View File

@@ -0,0 +1,43 @@
// Package tools provides ...
package tools
import (
"testing"
"time"
)
func TestDateFormat(t *testing.T) {
now := time.Now()
layout := "2006-01-02 15:04:05"
str := DateFormat(now, layout)
t.Log(str)
var err error
TimeLocation, err = time.LoadLocation("Asia/Shanghai")
if err != nil {
t.Fatal(err)
}
str = DateFormat(now, layout)
t.Log(str)
}
func TestParseInLocation(t *testing.T) {
date := "2021-04-27 15:33"
layout := "2006-01-02 15:04"
tm, err := time.Parse(layout, date)
if err != nil {
t.Fatal(err)
}
t.Log(tm)
TimeLocation, err = time.LoadLocation("Asia/Shanghai")
if err != nil {
t.Fatal(err)
}
tm, err = time.ParseInLocation(layout, date, TimeLocation)
if err != nil {
t.Fatal(err)
}
t.Log(tm.UTC())
}