Skip to content

Commit d40f2bc

Browse files
committed
Merge pull request #117 from jamesharr/master
[FIX] Timezone fix Convert to DB timezone when inserting time.Time
2 parents ceeeaea + 3ee9d38 commit d40f2bc

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

driver_test.go

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,47 @@ func TestRawBytesResultExceedsBuffer(t *testing.T) {
161161
})
162162
}
163163

164+
func TestTimezoneConversion(t *testing.T) {
165+
166+
zones := []string{"UTC", "US/Central", "US/Pacific", "Local"}
167+
168+
// Regression test for timezone handling
169+
tzTest := func(dbt *DBTest) {
170+
171+
// Create table
172+
dbt.mustExec("CREATE TABLE test (ts TIMESTAMP)")
173+
174+
// Insert local time into database (should be converted)
175+
usCentral, _ := time.LoadLocation("US/Central")
176+
now := time.Now().In(usCentral)
177+
dbt.mustExec("INSERT INTO test VALUE (?)", now)
178+
179+
// Retrieve time from DB
180+
rows := dbt.mustQuery("SELECT ts FROM test")
181+
if !rows.Next() {
182+
dbt.Fatal("Didn't get any rows out")
183+
}
184+
185+
var nowDB time.Time
186+
err := rows.Scan(&nowDB)
187+
if err != nil {
188+
dbt.Fatal("Err", err)
189+
}
190+
191+
// Check that dates match
192+
if now.Unix() != nowDB.Unix() {
193+
dbt.Errorf("Times don't match.\n")
194+
dbt.Errorf(" Now(%v)=%v\n", usCentral, now)
195+
dbt.Errorf(" Now(UTC)=%v\n", nowDB)
196+
}
197+
198+
}
199+
200+
for _, tz := range zones {
201+
runTests(t, dsn+"&parseTime=true&loc="+tz, tzTest)
202+
}
203+
}
204+
164205
func TestCRUD(t *testing.T) {
165206
runTests(t, dsn, func(dbt *DBTest) {
166207
// Create Table
@@ -1054,7 +1095,7 @@ func TestStmtMultiRows(t *testing.T) {
10541095

10551096
func TestConcurrent(t *testing.T) {
10561097
if enabled, _ := readBool(os.Getenv("MYSQL_TEST_CONCURRENT")); !enabled {
1057-
t.Skip("CONCURRENT env var not set")
1098+
t.Skip("MYSQL_TEST_CONCURRENT env var not set")
10581099
}
10591100

10601101
runTests(t, dsn, func(dbt *DBTest) {

packets.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,7 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
819819
if v.IsZero() {
820820
val = []byte("0000-00-00")
821821
} else {
822-
val = []byte(v.Format(timeFormat))
822+
val = []byte(v.In(stmt.mc.cfg.loc).Format(timeFormat))
823823
}
824824

825825
paramValues[i] = append(

0 commit comments

Comments
 (0)