Browse Source

Allow the database's timezone to be specified

xormplus 9 years ago
parent
commit
a1c9fd2518
4 changed files with 20 additions and 10 deletions
  1. 1 1
      engine.go
  2. 5 1
      session.go
  3. 11 5
      sessionplus.go
  4. 3 3
      test/xorm_test.go

+ 1 - 1
engine.go

@@ -44,6 +44,7 @@ type Engine struct {
 
 	logger     core.ILogger
 	TZLocation *time.Location
+	DatabaseTZ *time.Location // The timezone of the database
 
 	disableGlobalCache bool
 }
@@ -290,7 +291,6 @@ func (engine *Engine) logSQLExecutionTime(sqlStr string, args []interface{}, exe
 		return res, err
 	}
 	return executionBlock()
-
 }
 
 // Sql will be depracated, please use SQL instead

+ 5 - 1
session.go

@@ -1837,9 +1837,13 @@ func (session *Session) _row2Bean(rows *core.Rows, fields []string, fieldsCount
 						t := vv.Convert(core.TimeType).Interface().(time.Time)
 						z, _ := t.Zone()
 						if len(z) == 0 || t.Year() == 0 { // !nashtsai! HACK tmp work around for lib/pq doesn't properly time with location
+							dbTZ := session.Engine.DatabaseTZ
+							if dbTZ == nil {
+								dbTZ = time.Local
+							}
 							session.Engine.logger.Debugf("empty zone key[%v] : %v | zone: %v | location: %+v\n", key, t, z, *t.Location())
 							t = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(),
-								t.Minute(), t.Second(), t.Nanosecond(), time.Local)
+								t.Minute(), t.Second(), t.Nanosecond(), dbTZ)
 						}
 						// !nashtsai! convert to engine location
 						if col.TimeZone == nil {

+ 11 - 5
sessionplus.go

@@ -796,21 +796,27 @@ func (session *Session) _row2BeanWithDateFormat(dateFormat string, rows *core.Ro
 
 						t := vv.Convert(core.TimeType).Interface().(time.Time)
 						z, _ := t.Zone()
-						if len(z) == 0 || t.Year() == 0 {
-							// !nashtsai! HACK tmp work around for lib/pq doesn't properly time with location
+						if len(z) == 0 || t.Year() == 0 { // !nashtsai! HACK tmp work around for lib/pq doesn't properly time with location
+							dbTZ := session.Engine.DatabaseTZ
+							if dbTZ == nil {
+								dbTZ = time.Local
+							}
 							session.Engine.logger.Debugf("empty zone key[%v] : %v | zone: %v | location: %+v\n", key, t, z, *t.Location())
 							t = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(),
-								t.Minute(), t.Second(), t.Nanosecond(), time.Local)
+								t.Minute(), t.Second(), t.Nanosecond(), dbTZ)
 						}
 						// !nashtsai! convert to engine location
+						var tz *time.Location
 						if col.TimeZone == nil {
 							t = t.In(session.Engine.TZLocation)
+							tz = session.Engine.TZLocation
 						} else {
 							t = t.In(col.TimeZone)
+							tz = col.TimeZone
 						}
 						// dateFormat to string
-						loc, _ := time.LoadLocation("Local") //重要:获取时区  rawValue.Interface().(time.Time).Format(dateFormat)
-						t, _ = time.ParseInLocation(dateFormat, t.Format(dateFormat), loc)
+						//loc, _ := time.LoadLocation("Local") //重要:获取时区  rawValue.Interface().(time.Time).Format(dateFormat)
+						t, _ = time.ParseInLocation(dateFormat, t.Format(dateFormat), tz)
 
 						fieldValue.Set(reflect.ValueOf(t).Convert(fieldType))
 					} else if rawValueType == core.IntType || rawValueType == core.Int64Type ||

+ 3 - 3
test/xorm_test.go

@@ -483,7 +483,7 @@ func Test_Query(t *testing.T) {
 
 func Test_Sql_Execute(t *testing.T) {
 
-	result, err := db.Sql("INSERT INTO categories VALUES (?, ?, ?, ?, ?)", 138, "xiaozhang", 1, 1, 1).Execute()
+	result, err := db.Sql("INSERT INTO categories VALUES (?, ?, ?, ?, ?)", 148, "xiaozhang", 1, 1, 1).Execute()
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -493,7 +493,7 @@ func Test_Sql_Execute(t *testing.T) {
 
 func Test_SqlMapClient_Execute(t *testing.T) {
 	db.AddSql("Test_SqlMapClient_Execute", "INSERT INTO categories VALUES (?id, ?name, ?counts, ?orders, ?pid)")
-	result, err := db.SqlMapClient("Test_SqlMapClient_Execute", &map[string]interface{}{"id": 139, "name": "xiaowang", "counts": 1, "orders": 1, "pid": 1}).Execute()
+	result, err := db.SqlMapClient("Test_SqlMapClient_Execute", &map[string]interface{}{"id": 149, "name": "xiaowang", "counts": 1, "orders": 1, "pid": 1}).Execute()
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -503,7 +503,7 @@ func Test_SqlMapClient_Execute(t *testing.T) {
 
 func Test_SqlTemplateClientt_Execute(t *testing.T) {
 	db.AddSqlTemplate("Test_SqlTemplateClientt_Execute", "INSERT INTO categories VALUES (?id, ?name, ?counts, ?orders, ?pid)")
-	result, err := db.SqlTemplateClient("Test_SqlTemplateClientt_Execute", &map[string]interface{}{"id": 230, "name": "laowang", "counts": 1, "orders": 1, "pid": 1}).Execute()
+	result, err := db.SqlTemplateClient("Test_SqlTemplateClientt_Execute", &map[string]interface{}{"id": 240, "name": "laowang", "counts": 1, "orders": 1, "pid": 1}).Execute()
 	if err != nil {
 		t.Fatal(err)
 	}