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

fix timezone bug when persisting *gtime.Time to db #1714 #1729

Merged
merged 1 commit into from
May 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions database/gdb/gdb_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,7 @@ func handleArguments(sql string, args []interface{}) (newSql string, newArgs []i

// Special struct handling.
case reflect.Struct:
switch v := arg.(type) {
switch arg.(type) {
// The underlying driver supports time.Time/*time.Time types.
case time.Time, *time.Time:
newArgs = append(newArgs, arg)
Expand All @@ -779,11 +779,11 @@ func handleArguments(sql string, args []interface{}) (newSql string, newArgs []i
// according to underlying driver. And the underlying driver also
// converts the time.Time to string automatically as the following does.
case gtime.Time:
newArgs = append(newArgs, v.String())
newArgs = append(newArgs, arg.(gtime.Time).Time)
continue

case *gtime.Time:
newArgs = append(newArgs, v.String())
newArgs = append(newArgs, arg.(*gtime.Time).Time)
continue

default:
Expand Down
1 change: 1 addition & 0 deletions database/gdb/gdb_z_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func init() {
Port: "3306",
User: TestDbUser,
Pass: TestDbPass,
Timezone: "Asia/Shanghai",
Name: parser.GetOpt("name", "").String(),
Type: parser.GetOpt("type", "mysql").String(),
Role: "master",
Expand Down
1 change: 1 addition & 0 deletions database/gdb/gdb_z_mysql_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func init() {
Port: "3306",
User: TestDbUser,
Pass: TestDbPass,
Timezone: "Asia/Shanghai",
Name: parser.GetOpt("name", "").String(),
Type: parser.GetOpt("type", "mysql").String(),
Role: "master",
Expand Down
18 changes: 9 additions & 9 deletions database/gdb/gdb_z_mysql_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3009,8 +3009,8 @@ func Test_Model_Issue1002(t *testing.T) {
})
// where + time.Time arguments, UTC.
gtest.C(t, func(t *gtest.T) {
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")
t1, _ := time.Parse("2006-01-02 15:04:05", "2020-10-27 11:03:32")
t2, _ := time.Parse("2006-01-02 15:04:05", "2020-10-27 11:03:34")
{
v, err := db.Model(table).Fields("id").Where("create_time>? and create_time<?", t1, t2).Value()
t.AssertNil(err)
Expand Down Expand Up @@ -3067,7 +3067,7 @@ func Test_TimeZoneInsert(t *testing.T) {
tableName := createTableForTimeZoneTest()
defer dropTable(tableName)

asiaLocal, err := time.LoadLocation("Asia/Shanghai")
tokyoLoc, err := time.LoadLocation("Asia/Tokyo")
gtest.AssertNil(err)

CreateTime := "2020-11-22 12:23:45"
Expand All @@ -3079,9 +3079,9 @@ func Test_TimeZoneInsert(t *testing.T) {
UpdatedAt gtime.Time `json:"updated_at"`
DeletedAt time.Time `json:"deleted_at"`
}
t1, _ := time.ParseInLocation("2006-01-02 15:04:05", CreateTime, asiaLocal)
t2, _ := time.ParseInLocation("2006-01-02 15:04:05", UpdateTime, asiaLocal)
t3, _ := time.ParseInLocation("2006-01-02 15:04:05", DeleteTime, asiaLocal)
t1, _ := time.ParseInLocation("2006-01-02 15:04:05", CreateTime, tokyoLoc)
t2, _ := time.ParseInLocation("2006-01-02 15:04:05", UpdateTime, tokyoLoc)
t3, _ := time.ParseInLocation("2006-01-02 15:04:05", DeleteTime, tokyoLoc)
u := &User{
Id: 1,
CreatedAt: gtime.New(t1.UTC()),
Expand All @@ -3094,9 +3094,9 @@ func Test_TimeZoneInsert(t *testing.T) {
userEntity := &User{}
err := db.Model(tableName).Where("id", 1).Unscoped().Scan(&userEntity)
t.AssertNil(err)
t.Assert(userEntity.CreatedAt.String(), "2020-11-22 04:23:45")
t.Assert(userEntity.UpdatedAt.String(), "2020-11-22 05:23:45")
t.Assert(gtime.NewFromTime(userEntity.DeletedAt).String(), "2020-11-22 06:23:45")
t.Assert(userEntity.CreatedAt.String(), "2020-11-22 11:23:45")
t.Assert(userEntity.UpdatedAt.String(), "2020-11-22 12:23:45")
t.Assert(gtime.NewFromTime(userEntity.DeletedAt).String(), "2020-11-22 13:23:45")
})
}

Expand Down