fix timezone!

This commit is contained in:
Unknwon 2015-08-20 00:56:12 +08:00
parent 6c2536b89c
commit f114f78743
3 changed files with 24 additions and 6 deletions

View File

@ -82,8 +82,7 @@ type Action struct {
func (a *Action) AfterSet(colName string, _ xorm.Cell) { func (a *Action) AfterSet(colName string, _ xorm.Cell) {
switch colName { switch colName {
case "created": case "created":
now := time.Now() a.Created = regulateTimeZone(a.Created)
a.Created = a.Created.Add(now.Sub(now.UTC()))
} }
} }

View File

@ -93,8 +93,7 @@ func (i *Issue) AfterSet(colName string, _ xorm.Cell) {
log.Error(3, "GetUserByID[%d]: %v", i.ID, err) log.Error(3, "GetUserByID[%d]: %v", i.ID, err)
} }
case "created": case "created":
now := time.Now() i.Created = regulateTimeZone(i.Created)
i.Created = i.Created.Add(now.Sub(now.UTC()))
} }
} }
@ -1360,8 +1359,7 @@ func (c *Comment) AfterSet(colName string, _ xorm.Cell) {
} }
} }
case "created": case "created":
now := time.Now() c.Created = regulateTimeZone(c.Created)
c.Created = c.Created.Add(now.Sub(now.UTC()))
} }
} }

View File

@ -10,7 +10,9 @@ import (
"os" "os"
"path" "path"
"strings" "strings"
"time"
"github.com/Unknwon/com"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
"github.com/go-xorm/core" "github.com/go-xorm/core"
"github.com/go-xorm/xorm" "github.com/go-xorm/xorm"
@ -40,6 +42,25 @@ func sessionRelease(sess *xorm.Session) {
sess.Close() sess.Close()
} }
// Note: get back time.Time from database Go sees it at UTC where they are really Local.
// So this function makes correct timezone offset.
func regulateTimeZone(t time.Time) time.Time {
if setting.UseSQLite3 {
return t
}
zone := t.Local().Format("-0700")
if len(zone) != 5 {
return t
}
offset := com.StrTo(zone[2:3]).MustInt()
if zone[0] == '-' {
return t.Add(time.Duration(offset) * time.Hour)
}
return t.Add(-1 * time.Duration(offset) * time.Hour)
}
var ( var (
x *xorm.Engine x *xorm.Engine
tables []interface{} tables []interface{}