Browse Source

解决时区问题

Minho 7 years ago
parent
commit
813ef8a17b
3 changed files with 13 additions and 12 deletions
  1. 8 8
      commands/command.go
  2. 3 3
      controllers/document.go
  3. 2 1
      models/document.go

+ 8 - 8
commands/command.go

@@ -30,13 +30,20 @@ import (
 func RegisterDataBase() {
 func RegisterDataBase() {
 	beego.Info("正在初始化数据库配置.")
 	beego.Info("正在初始化数据库配置.")
 	adapter := beego.AppConfig.String("db_adapter")
 	adapter := beego.AppConfig.String("db_adapter")
+	timezone := beego.AppConfig.String("timezone")
+	location, err := time.LoadLocation(timezone)
+	if err == nil {
+		orm.DefaultTimeLoc = location
+	} else {
+		beego.Error("加载时区配置信息失败,请检查是否存在ZONEINFO环境变量:",err)
+	}
 
 
 	if adapter == "mysql" {
 	if adapter == "mysql" {
 		host := beego.AppConfig.String("db_host")
 		host := beego.AppConfig.String("db_host")
 		database := beego.AppConfig.String("db_database")
 		database := beego.AppConfig.String("db_database")
 		username := beego.AppConfig.String("db_username")
 		username := beego.AppConfig.String("db_username")
 		password := beego.AppConfig.String("db_password")
 		password := beego.AppConfig.String("db_password")
-		timezone := beego.AppConfig.String("timezone")
+
 
 
 		port := beego.AppConfig.String("db_port")
 		port := beego.AppConfig.String("db_port")
 
 
@@ -47,13 +54,6 @@ func RegisterDataBase() {
 			beego.Error("注册默认数据库失败:",err)
 			beego.Error("注册默认数据库失败:",err)
 			os.Exit(1)
 			os.Exit(1)
 		}
 		}
-
-		location, err := time.LoadLocation(timezone)
-		if err == nil {
-			orm.DefaultTimeLoc = location
-		} else {
-			beego.Error("加载时区配置信息失败,请检查是否存在ZONEINFO环境变量:",err)
-		}
 	} else if adapter == "sqlite3" {
 	} else if adapter == "sqlite3" {
 		database := beego.AppConfig.String("db_database")
 		database := beego.AppConfig.String("db_database")
 		if strings.HasPrefix(database, "./") {
 		if strings.HasPrefix(database, "./") {

+ 3 - 3
controllers/document.go

@@ -221,13 +221,13 @@ func (c *DocumentController) Read() {
 	}
 	}
 
 
 	docInfo += " 创建于 "
 	docInfo += " 创建于 "
-	docInfo += doc.CreateTime.Format("2006-01-02 15:04")
+	docInfo += doc.CreateTime.Local().Format("2006-01-02 15:04")
 
 
 	if doc.ModifyTime != doc.CreateTime {
 	if doc.ModifyTime != doc.CreateTime {
 		docInfo += ";更新于 "
 		docInfo += ";更新于 "
-		docInfo += doc.ModifyTime.Format("2006-01-02 15:04")
+		docInfo += doc.ModifyTime.Local().Format("2006-01-02 15:04")
 		if strings.TrimSpace(doc.Release) != "" {
 		if strings.TrimSpace(doc.Release) != "" {
-			doc.Release += "<div class=\"wiki-bottom\">文档更新时间: " + doc.ModifyTime.Format("2006-01-02 15:04") + "</div>";
+			doc.Release += "<div class=\"wiki-bottom\">文档更新时间: " + doc.ModifyTime.Local().Format("2006-01-02 15:04") + "</div>";
 		}
 		}
 	}
 	}
 
 

+ 2 - 1
models/document.go

@@ -33,7 +33,7 @@ type Document struct {
 	Content    string        `orm:"column(content);type(text);null" json:"content"`
 	Content    string        `orm:"column(content);type(text);null" json:"content"`
 	CreateTime time.Time     `orm:"column(create_time);type(datetime);auto_now_add" json:"create_time"`
 	CreateTime time.Time     `orm:"column(create_time);type(datetime);auto_now_add" json:"create_time"`
 	MemberId   int           `orm:"column(member_id);type(int)" json:"member_id"`
 	MemberId   int           `orm:"column(member_id);type(int)" json:"member_id"`
-	ModifyTime time.Time     `orm:"column(modify_time);type(datetime);auto_now" json:"modify_time"`
+	ModifyTime time.Time     `orm:"column(modify_time);type(datetime)" json:"modify_time"`
 	ModifyAt   int           `orm:"column(modify_at);type(int)" json:"-"`
 	ModifyAt   int           `orm:"column(modify_at);type(int)" json:"-"`
 	Version    int64         `orm:"type(bigint);column(version)" json:"version"`
 	Version    int64         `orm:"type(bigint);column(version)" json:"version"`
 	AttachList []*Attachment `orm:"-" json:"attach"`
 	AttachList []*Attachment `orm:"-" json:"attach"`
@@ -81,6 +81,7 @@ func (m *Document) InsertOrUpdate(cols ...string) error {
 	o := orm.NewOrm()
 	o := orm.NewOrm()
 	var err error
 	var err error
 	if m.DocumentId > 0 {
 	if m.DocumentId > 0 {
+		m.ModifyTime = time.Now().Local()
 		_, err = o.Update(m)
 		_, err = o.Update(m)
 	} else {
 	} else {
 		_, err = o.Insert(m)
 		_, err = o.Insert(m)