Merge pull request #99 from admpub/master

improved
This commit is contained in:
S.W.H 2014-04-16 21:05:53 +08:00
commit 41e3d14f20
3 changed files with 27 additions and 15 deletions

View File

@ -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]

View File

@ -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
@ -2403,7 +2403,7 @@ 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))
tf := session.Engine.FormatTime(col.SQLType.Name, fieldValue.Interface().(time.Time))
return tf, nil
default:
return fieldValue.Interface(), nil

View File

@ -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{})