improve time argument timezone handling for package gdb

This commit is contained in:
Jack
2020-11-27 21:03:22 +08:00
parent 3eba8d690f
commit 9d0f306684
3 changed files with 28 additions and 5 deletions

View File

@ -31,6 +31,7 @@ func (d *DriverMysql) New(core *Core, node *ConfigNode) (DB, error) {
}
// Open creates and returns a underlying sql.DB object for mysql.
// Note that it converts time.Time argument to local timezone in default.
func (d *DriverMysql) Open(config *ConfigNode) (*sql.DB, error) {
var source string
if config.LinkInfo != "" {
@ -41,7 +42,7 @@ func (d *DriverMysql) Open(config *ConfigNode) (*sql.DB, error) {
}
} else {
source = fmt.Sprintf(
"%s:%s@tcp(%s:%s)/%s?charset=%s&multiStatements=true&parseTime=true",
"%s:%s@tcp(%s:%s)/%s?charset=%s&multiStatements=true&parseTime=true&loc=Local",
config.User, config.Pass, config.Host, config.Port, config.Name, config.Charset,
)
}

View File

@ -658,9 +658,13 @@ func handleArguments(sql string, args []interface{}) (newSql string, newArgs []i
newArgs = append(newArgs, arg)
continue
// Special handling for gtime.Time.
// Special handling for gtime.Time/*gtime.Time.
case gtime.Time:
newArgs = append(newArgs, v.String())
newArgs = append(newArgs, v.Time)
continue
case *gtime.Time:
newArgs = append(newArgs, v.Time)
continue
default:

View File

@ -2841,7 +2841,7 @@ func Test_Model_HasField(t *testing.T) {
func Test_Model_Issue1002(t *testing.T) {
table := createTable()
defer dropTable(table)
db.SetDebug(true)
result, err := db.Table(table).Data(g.Map{
"id": 1,
"passport": "port_1",
@ -2901,9 +2901,27 @@ func Test_Model_Issue1002(t *testing.T) {
t.Assert(err, nil)
t.Assert(v.Int(), 1)
})
// where + time.Time arguments.
// where + time.Time arguments, UTC.
t1, _ := time.Parse("2006-01-02 15:04:05", "2020-10-27 19:03:32")
t2, _ := time.Parse("2006-01-02 15:04:05", "2020-10-27 19:03:34")
gtest.C(t, func(t *gtest.T) {
v, err := db.Table(table).Fields("id").Where("create_time>? and create_time<?", t1, t2).Value()
t.Assert(err, nil)
t.Assert(v.Int(), 0)
})
gtest.C(t, func(t *gtest.T) {
v, err := db.Table(table).Fields("id").Where("create_time>? and create_time<?", t1, t2).FindValue()
t.Assert(err, nil)
t.Assert(v.Int(), 0)
})
gtest.C(t, func(t *gtest.T) {
v, err := db.Table(table).Where("create_time>? and create_time<?", t1, t2).FindValue("id")
t.Assert(err, nil)
t.Assert(v.Int(), 0)
})
// where + time.Time arguments, local.
t1, _ = time.ParseInLocation("2006-01-02 15:04:05", "2020-10-27 19:03:32", time.Local)
t2, _ = time.ParseInLocation("2006-01-02 15:04:05", "2020-10-27 19:03:34", time.Local)
gtest.C(t, func(t *gtest.T) {
v, err := db.Table(table).Fields("id").Where("create_time>? and create_time<?", t1, t2).Value()
t.Assert(err, nil)