Bladeren bron

small refactors

xormplus 8 jaren geleden
bovenliggende
commit
ef81833999
1 gewijzigde bestanden met toevoegingen van 20 en 23 verwijderingen
  1. 20 23
      sessionplus.go

+ 20 - 23
sessionplus.go

@@ -755,6 +755,15 @@ func (session *Session) _row2BeanWithDateFormat(dateFormat string, rows *core.Ro
 		}
 	}()
 
+	dbTZ := session.Engine.DatabaseTZ
+	if dbTZ == nil {
+		if session.Engine.dialect.DBType() == core.SQLITE {
+			dbTZ = time.UTC
+		} else {
+			dbTZ = time.Local
+		}
+	}
+
 	var tempMap = make(map[string]int)
 	var pk core.PK
 	for ii, key := range fields {
@@ -913,20 +922,19 @@ func (session *Session) _row2BeanWithDateFormat(dateFormat string, rows *core.Ro
 				}
 			case reflect.Struct:
 				if fieldType.ConvertibleTo(core.TimeType) {
+					var tz *time.Location
+					if col.TimeZone == nil {
+						tz = session.Engine.TZLocation
+					} else {
+						tz = col.TimeZone
+					}
+
 					if rawValueType == core.TimeType {
 						hasAssigned = true
 
 						t := vv.Convert(core.TimeType).Interface().(time.Time)
-						z, _ := t.Zone()
-						dbTZ := session.Engine.DatabaseTZ
-						if dbTZ == nil {
-							if session.Engine.dialect.DBType() == core.SQLITE {
-								dbTZ = time.UTC
-							} else {
-								dbTZ = time.Local
-							}
-						}
 
+						z, _ := t.Zone()
 						// set new location if database don't save timezone or give an incorrect timezone
 						if len(z) == 0 || t.Year() == 0 || t.Location().String() != dbTZ.String() { // !nashtsai! HACK tmp work around for lib/pq doesn't properly time with location
 							session.Engine.logger.Debugf("empty zone key[%v] : %v | zone: %v | location: %+v\n", key, t, z, *t.Location())
@@ -935,14 +943,8 @@ func (session *Session) _row2BeanWithDateFormat(dateFormat string, rows *core.Ro
 						}
 
 						// !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
-						}
+						t = t.In(tz)
+
 						// dateFormat to string
 						//loc, _ := time.LoadLocation("Local") //重要:获取时区  rawValue.Interface().(time.Time).Format(dateFormat)
 						t, _ = time.ParseInLocation(dateFormat, t.Format(dateFormat), tz)
@@ -951,12 +953,7 @@ func (session *Session) _row2BeanWithDateFormat(dateFormat string, rows *core.Ro
 					} else if rawValueType == core.IntType || rawValueType == core.Int64Type ||
 						rawValueType == core.Int32Type {
 						hasAssigned = true
-						var tz *time.Location
-						if col.TimeZone == nil {
-							tz = session.Engine.TZLocation
-						} else {
-							tz = col.TimeZone
-						}
+
 						t := time.Unix(vv.Int(), 0).In(tz)
 						//vv = reflect.ValueOf(t)
 						fieldValue.Set(reflect.ValueOf(t).Convert(fieldType))