Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用time.Time作为参数进行查询时区不正确 #1013

Closed
tiansin opened this issue Nov 23, 2020 · 3 comments
Closed

使用time.Time作为参数进行查询时区不正确 #1013

tiansin opened this issue Nov 23, 2020 · 3 comments
Labels

Comments

@tiansin
Copy link
Contributor

tiansin commented Nov 23, 2020

1. What version of Go and system type/arch are you using?

go version go1.15.5 darwin/amd64

2. What version of GoFrame are you using?

github.com/gogf/gf v1.14.5

3. Can this issue be reproduced with the latest release?

4. What did you do?

使用 time.Time 类型插入数据时mysql执行日志正常,使用同一个参数作为查询参数时,框架日志显示的时区正常,mysql日志执行中时区变成了UTC时区.

5. What did you expect to see?

6. What did you see instead?

测试代码

package test

import (
	"fmt"
	"log"
	"testing"
	"time"

	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/os/gtime"
	"github.com/gogf/gf/test/gtest"
)

func createTable() string {
	db := g.Database()
	db.SetDebug(true)
	tableName := "test_date"
	if _, err := db.Exec(fmt.Sprintf(`
	    CREATE TABLE %s (
	        id      int(10) unsigned NOT NULL AUTO_INCREMENT,
	        date    datetime NULL,
	        PRIMARY KEY (id)
	    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
	    `, tableName,
	)); err != nil {
		gtest.Fatal(err)
	}
	return tableName

}

func dropTable(table string) {
	db := g.Database()
	if _, err := db.Exec(fmt.Sprintf("DROP TABLE IF EXISTS `%s`", table)); err != nil {
		gtest.Error(err)
	}

}

func Test_mainFile(t *testing.T) {
	db := g.Database()

	tableName := createTable()
	defer dropTable(tableName)

	type TestDate struct {
		Id   int         `json:"id"`
		Date *gtime.Time `json:"date"`
	}

	str := "2020-11-23 00:00:00"
	date, _ := time.ParseInLocation("2006-01-02 15:04:05", str, time.Local)
	log.Print(date)

	u := &TestDate{
		Id:   1,
		Date: gtime.New(date),
	}

	gtest.C(t, func(t *gtest.T) {
		_, _ = db.Table(tableName).Insert(u)
		dateEntity := &TestDate{}
		err := db.Table(tableName).Where("date=?", date).Struct(&dateEntity)
		t.Assert(err, nil)
		t.Assert(dateEntity.Date.String(), str)
	})
}

日志输出:

2020-11-23 16:56:08.667 [DEBU] [ 25 ms] [default] 
	    CREATE TABLE test_date (
	        id      int(10) unsigned NOT NULL AUTO_INCREMENT,
	        date    datetime NULL,
	        PRIMARY KEY (id)
	    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2020/11/23 16:56:08 2020-11-23 00:00:00 +0800 CST
2020-11-23 16:56:08.670 [DEBU] [  3 ms] [default] SHOW FULL COLUMNS FROM `test_date`
2020-11-23 16:56:08.671 [DEBU] [  1 ms] [default] INSERT INTO `test_date`(`id`,`date`) VALUES(1,'2020-11-23 00:00:00')
2020-11-23 16:56:11.973 [DEBU] [  1 ms] [default] SELECT * FROM `test_date` WHERE date='2020-11-23 00:00:00' LIMIT 1
...
2020-11-23 16:56:11.975 [DEBU] [  2 ms] [default] DROP TABLE IF EXISTS `test_date`
--- FAIL: Test_mainFile (3.33s)
FAIL

MYSQL执行日志:

201123 16:56:08	    63 Connect	root@localhost on test_data using TCP/IP
		    63 Query	SET NAMES utf8mb4
		    63 Query	CREATE TABLE test_date (
	        id      int(10) unsigned NOT NULL AUTO_INCREMENT,
	        date    datetime NULL,
	        PRIMARY KEY (id)
	    ) ENGINE=InnoDB DEFAULT CHARSET=utf8
		    63 Query	SHOW FULL COLUMNS FROM `test_date`
		    63 Prepare	INSERT INTO `test_date`(`id`,`date`) VALUES(?,?)
		    63 Execute	INSERT INTO `test_date`(`id`,`date`) VALUES(1,'2020-11-23 00:00:00')
		    63 Close stmt
201123 16:56:11	    63 Prepare	SELECT * FROM `test_date` WHERE date=? LIMIT 1
		    63 Execute	SELECT * FROM `test_date` WHERE date='2020-11-22 16:00:00' LIMIT 1
		    63 Close stmt
		    63 Query	DROP TABLE IF EXISTS `test_date`
@tiansin tiansin closed this as completed Nov 23, 2020
@tiansin tiansin reopened this Nov 23, 2020
@gqcn
Copy link
Member

gqcn commented Nov 26, 2020

@tiansin 看了下应该是底层drivertime.Time做了自动转换为UTC时区,你可以用time.Parse

@gqcn gqcn closed this as completed Nov 26, 2020
@gqcn gqcn added the question label Nov 26, 2020
@gqcn
Copy link
Member

gqcn commented Nov 27, 2020

@tiansin time.Time参数会被底层的driver做时区转换,建议你日常中使用gtime.Time/*gtime.Time,不会被底层driver转换。

@tiansin
Copy link
Contributor Author

tiansin commented Jun 6, 2022

@gqcn v2.1.0-rc4 最新版本gtime的时区也被转换了.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants