diff --git a/engine.go b/engine.go index cd725708..b375e2a7 100644 --- a/engine.go +++ b/engine.go @@ -36,8 +36,8 @@ type Engine struct { ShowWarn bool //Pool IConnectPool //Filters []core.Filter - Logger ILogger // io.Writer - TimeZone string + Logger ILogger // io.Writer + TZLocation *time.Location } func (engine *Engine) DriverName() string { @@ -1094,28 +1094,8 @@ func (engine *Engine) Import(ddlPath string) ([]sql.Result, error) { return results, lastError } -func (engine *Engine) TZTime(t time.Time) (r time.Time) { - switch engine.TimeZone { - case "Local", "L": - r = t.Local() - case "UTC", "U": - fallthrough - default: - r = t.UTC() - } - return -} - -func (engine *Engine) TZLocation() (r *time.Location) { - switch engine.TimeZone { - case "Local", "L": - r = time.Local - case "UTC", "U": - fallthrough - default: - r = time.UTC - } - return +func (engine *Engine) TZTime(t time.Time) time.Time { + return t.In(engine.TZLocation) } func (engine *Engine) NowTime(sqlTypeName string) interface{} { diff --git a/session.go b/session.go index e349fb82..59ec4eb2 100644 --- a/session.go +++ b/session.go @@ -1987,17 +1987,17 @@ func (session *Session) byte2Time(col *core.Column, data []byte) (outTime time.T x = time.Unix(0, sd) } } else if len(sdata) > 19 { - x, err = time.ParseInLocation(time.RFC3339Nano, sdata, session.Engine.TZLocation()) + x, err = time.ParseInLocation(time.RFC3339Nano, sdata, session.Engine.TZLocation) if err != nil { - x, err = time.ParseInLocation("2006-01-02 15:04:05.999999999", sdata, session.Engine.TZLocation()) + x, err = time.ParseInLocation("2006-01-02 15:04:05.999999999", sdata, session.Engine.TZLocation) } if err != nil { - x, err = time.ParseInLocation("2006-01-02 15:04:05.9999999 Z07:00", sdata, session.Engine.TZLocation()) + x, err = time.ParseInLocation("2006-01-02 15:04:05.9999999 Z07:00", sdata, session.Engine.TZLocation) } } else if len(sdata) == 19 { - x, err = time.ParseInLocation("2006-01-02 15:04:05", sdata, session.Engine.TZLocation()) + x, err = time.ParseInLocation("2006-01-02 15:04:05", sdata, session.Engine.TZLocation) } else if len(sdata) == 10 && sdata[4] == '-' && sdata[7] == '-' { - x, err = time.ParseInLocation("2006-01-02", sdata, session.Engine.TZLocation()) + x, err = time.ParseInLocation("2006-01-02", sdata, session.Engine.TZLocation) } else if col.SQLType.Name == core.Time { if strings.Contains(sdata, " ") { ssd := strings.Split(sdata, " ") @@ -2012,7 +2012,7 @@ func (session *Session) byte2Time(col *core.Column, data []byte) (outTime time.T //fmt.Println(sdata) st := fmt.Sprintf("2006-01-02 %v", sdata) - x, err = time.ParseInLocation("2006-01-02 15:04:05", st, session.Engine.TZLocation()) + x, err = time.ParseInLocation("2006-01-02 15:04:05", st, session.Engine.TZLocation) } else { outErr = errors.New(fmt.Sprintf("unsupported time format %v", sdata)) return diff --git a/xorm.go b/xorm.go index 9bad07d6..5b94f298 100644 --- a/xorm.go +++ b/xorm.go @@ -9,6 +9,7 @@ import ( "reflect" "runtime" "sync" + "time" ) const ( @@ -92,7 +93,7 @@ func NewEngine(driverName string, dataSourceName string) (*Engine, error) { mutex: &sync.RWMutex{}, TagIdentifier: "xorm", Logger: NewSimpleLogger(os.Stdout), - TimeZone: "Local", + TZLocation: time.Local, } engine.SetMapper(core.NewCacheMapper(new(core.SnakeMapper)))