use time.Location struct instead of TimeZone string
This commit is contained in:
parent
52f7a96bfe
commit
b7690b1668
26
engine.go
26
engine.go
|
@ -37,7 +37,7 @@ type Engine struct {
|
||||||
//Pool IConnectPool
|
//Pool IConnectPool
|
||||||
//Filters []core.Filter
|
//Filters []core.Filter
|
||||||
Logger ILogger // io.Writer
|
Logger ILogger // io.Writer
|
||||||
TimeZone string
|
TZLocation *time.Location
|
||||||
}
|
}
|
||||||
|
|
||||||
func (engine *Engine) DriverName() string {
|
func (engine *Engine) DriverName() string {
|
||||||
|
@ -1094,28 +1094,8 @@ func (engine *Engine) Import(ddlPath string) ([]sql.Result, error) {
|
||||||
return results, lastError
|
return results, lastError
|
||||||
}
|
}
|
||||||
|
|
||||||
func (engine *Engine) TZTime(t time.Time) (r time.Time) {
|
func (engine *Engine) TZTime(t time.Time) time.Time {
|
||||||
switch engine.TimeZone {
|
return t.In(engine.TZLocation)
|
||||||
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) NowTime(sqlTypeName string) interface{} {
|
func (engine *Engine) NowTime(sqlTypeName string) interface{} {
|
||||||
|
|
12
session.go
12
session.go
|
@ -1987,17 +1987,17 @@ func (session *Session) byte2Time(col *core.Column, data []byte) (outTime time.T
|
||||||
x = time.Unix(0, sd)
|
x = time.Unix(0, sd)
|
||||||
}
|
}
|
||||||
} else if len(sdata) > 19 {
|
} 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 {
|
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 {
|
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 {
|
} 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] == '-' {
|
} 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 {
|
} else if col.SQLType.Name == core.Time {
|
||||||
if strings.Contains(sdata, " ") {
|
if strings.Contains(sdata, " ") {
|
||||||
ssd := strings.Split(sdata, " ")
|
ssd := strings.Split(sdata, " ")
|
||||||
|
@ -2012,7 +2012,7 @@ func (session *Session) byte2Time(col *core.Column, data []byte) (outTime time.T
|
||||||
//fmt.Println(sdata)
|
//fmt.Println(sdata)
|
||||||
|
|
||||||
st := fmt.Sprintf("2006-01-02 %v", 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 {
|
} else {
|
||||||
outErr = errors.New(fmt.Sprintf("unsupported time format %v", sdata))
|
outErr = errors.New(fmt.Sprintf("unsupported time format %v", sdata))
|
||||||
return
|
return
|
||||||
|
|
3
xorm.go
3
xorm.go
|
@ -9,6 +9,7 @@ import (
|
||||||
"reflect"
|
"reflect"
|
||||||
"runtime"
|
"runtime"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -92,7 +93,7 @@ func NewEngine(driverName string, dataSourceName string) (*Engine, error) {
|
||||||
mutex: &sync.RWMutex{},
|
mutex: &sync.RWMutex{},
|
||||||
TagIdentifier: "xorm",
|
TagIdentifier: "xorm",
|
||||||
Logger: NewSimpleLogger(os.Stdout),
|
Logger: NewSimpleLogger(os.Stdout),
|
||||||
TimeZone: "Local",
|
TZLocation: time.Local,
|
||||||
}
|
}
|
||||||
|
|
||||||
engine.SetMapper(core.NewCacheMapper(new(core.SnakeMapper)))
|
engine.SetMapper(core.NewCacheMapper(new(core.SnakeMapper)))
|
||||||
|
|
Loading…
Reference in New Issue