aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorUnknwon <u@gogs.io>2015-08-20 00:56:12 +0800
committerUnknwon <u@gogs.io>2015-08-20 00:56:12 +0800
commitf114f7874303d249b2956c894b1a90b042430acf (patch)
tree63ea82c0a97dbb884de1f09620524f8d6c98f224
parent6c2536b89c30ef1c99752abd50fdb3b5d2cd0691 (diff)
downloadgitea-f114f7874303d249b2956c894b1a90b042430acf.tar.gz
gitea-f114f7874303d249b2956c894b1a90b042430acf.zip
fix timezone!
-rw-r--r--models/action.go3
-rw-r--r--models/issue.go6
-rw-r--r--models/models.go21
3 files changed, 24 insertions, 6 deletions
diff --git a/models/action.go b/models/action.go
index 2c08eebbd9..2beb42e576 100644
--- a/models/action.go
+++ b/models/action.go
@@ -82,8 +82,7 @@ type Action struct {
func (a *Action) AfterSet(colName string, _ xorm.Cell) {
switch colName {
case "created":
- now := time.Now()
- a.Created = a.Created.Add(now.Sub(now.UTC()))
+ a.Created = regulateTimeZone(a.Created)
}
}
diff --git a/models/issue.go b/models/issue.go
index 5e5c79c2e5..d8c4809f11 100644
--- a/models/issue.go
+++ b/models/issue.go
@@ -93,8 +93,7 @@ func (i *Issue) AfterSet(colName string, _ xorm.Cell) {
log.Error(3, "GetUserByID[%d]: %v", i.ID, err)
}
case "created":
- now := time.Now()
- i.Created = i.Created.Add(now.Sub(now.UTC()))
+ i.Created = regulateTimeZone(i.Created)
}
}
@@ -1360,8 +1359,7 @@ func (c *Comment) AfterSet(colName string, _ xorm.Cell) {
}
}
case "created":
- now := time.Now()
- c.Created = c.Created.Add(now.Sub(now.UTC()))
+ c.Created = regulateTimeZone(c.Created)
}
}
diff --git a/models/models.go b/models/models.go
index e06d5cf88e..7d02bb9565 100644
--- a/models/models.go
+++ b/models/models.go
@@ -10,7 +10,9 @@ import (
"os"
"path"
"strings"
+ "time"
+ "github.com/Unknwon/com"
_ "github.com/go-sql-driver/mysql"
"github.com/go-xorm/core"
"github.com/go-xorm/xorm"
@@ -40,6 +42,25 @@ func sessionRelease(sess *xorm.Session) {
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 (
x *xorm.Engine
tables []interface{}