diff --git a/engine.go b/engine.go index 106f5b84..f98ea4c7 100644 --- a/engine.go +++ b/engine.go @@ -1086,13 +1086,25 @@ func (engine *Engine) TZTime(t time.Time) (r time.Time) { return } -func (engine *Engine) NowTime(SQLTypeName string) interface{} { - t := time.Now() - return engine.FormatTime(SQLTypeName, t) +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) FormatTime(SQLTypeName string, t time.Time) (v interface{}) { - switch SQLTypeName { +func (engine *Engine) NowTime(sqlTypeName string) interface{} { + t := time.Now() + return engine.FormatTime(sqlTypeName, t) +} + +func (engine *Engine) FormatTime(sqlTypeName string, t time.Time) (v interface{}) { + switch sqlTypeName { case Time: s := engine.TZTime(t).Format("2006-01-02 15:04:05") //time.RFC3339 v = s[11:19] diff --git a/session.go b/session.go index 36625653..1e3e9c19 100644 --- a/session.go +++ b/session.go @@ -1930,17 +1930,17 @@ func (session *Session) byte2Time(col *Column, data []byte) (outTime time.Time, x = time.Unix(0, sd) } } else if len(sdata) > 19 { - x, err = time.Parse(time.RFC3339Nano, sdata) + x, err = time.ParseInLocation(time.RFC3339Nano, sdata, session.Engine.TZLocation()) if err != nil { - x, err = time.Parse("2006-01-02 15:04:05.999999999", sdata) + x, err = time.ParseInLocation("2006-01-02 15:04:05.999999999", sdata, session.Engine.TZLocation()) } if err != nil { - x, err = time.Parse("2006-01-02 15:04:05.9999999 Z07:00", sdata) + 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.Parse("2006-01-02 15:04:05", sdata) + 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.Parse("2006-01-02", sdata) + x, err = time.ParseInLocation("2006-01-02", sdata, session.Engine.TZLocation()) } else if col.SQLType.Name == Time { if strings.Contains(sdata, " ") { ssd := strings.Split(sdata, " ") @@ -1955,7 +1955,7 @@ func (session *Session) byte2Time(col *Column, data []byte) (outTime time.Time, //fmt.Println(sdata) st := fmt.Sprintf("2006-01-02 %v", sdata) - x, err = time.Parse("2006-01-02 15:04:05", st) + 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 @@ -2402,10 +2402,10 @@ func (session *Session) value2Interface(col *Column, fieldValue reflect.Value) ( } } switch fieldValue.Interface().(type) { - case time.Time: - tf := session.Engine.FormatTime(col.SQLType.Name,fieldValue.Interface().(time.Time)) + case time.Time: + tf := session.Engine.FormatTime(col.SQLType.Name, fieldValue.Interface().(time.Time)) return tf, nil - default: + default: return fieldValue.Interface(), nil } } diff --git a/xorm.go b/xorm.go index 11028edf..88d99e79 100644 --- a/xorm.go +++ b/xorm.go @@ -24,7 +24,7 @@ func NewEngine(driverName string, dataSourceName string) (*Engine, error) { DriverName: driverName, DataSourceName: dataSourceName, Filters: make([]Filter, 0), - TimeZone: "UTC", + TimeZone: "Local", } engine.SetMapper(SnakeMapper{})