Преглед изворни кода

parseTime=true tz fix w/ test

James Harr пре 12 година
родитељ
комит
da9f337e07
2 измењених фајлова са 42 додато и 1 уклоњено
  1. 41 0
      driver_test.go
  2. 1 1
      packets.go

+ 41 - 0
driver_test.go

@@ -161,6 +161,47 @@ func TestRawBytesResultExceedsBuffer(t *testing.T) {
 	})
 }
 
+func TestTimezoneConversion(t *testing.T) {
+
+	zones := []string{"UTC", "US/Central", "US/Pacific", "Local"}
+
+	// Regression test for timezone handling
+	tzTest := func(dbt *DBTest) {
+
+		// Create table
+		dbt.mustExec("CREATE TABLE test (ts TIMESTAMP)")
+
+		// Insert local time into database (should be converted)
+		usCentral, _ := time.LoadLocation("US/Central")
+		now := time.Now().In(usCentral)
+		dbt.mustExec("INSERT INTO test VALUE (?)", now)
+
+		// Retrieve time from DB
+		rows := dbt.mustQuery("SELECT ts FROM test")
+		if !rows.Next() {
+			dbt.Fatal("Didn't get any rows out")
+		}
+
+		var nowDB time.Time
+		err := rows.Scan(&nowDB)
+		if err != nil {
+			dbt.Fatal("Err", err)
+		}
+
+		// Check that dates match
+		if now.Unix() != nowDB.Unix() {
+			dbt.Errorf("Times don't match.\n")
+			dbt.Errorf(" Now(%v)=%v\n", usCentral, now)
+			dbt.Errorf(" Now(UTC)=%v\n", nowDB)
+		}
+
+	}
+
+	for _, tz := range zones {
+		runTests(t, dsn+"&parseTime=true&loc="+tz, tzTest)
+	}
+}
+
 func TestCRUD(t *testing.T) {
 	runTests(t, dsn, func(dbt *DBTest) {
 		// Create Table

+ 1 - 1
packets.go

@@ -819,7 +819,7 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
 			if v.IsZero() {
 				val = []byte("0000-00-00")
 			} else {
-				val = []byte(v.Format(timeFormat))
+				val = []byte(v.In(stmt.mc.cfg.loc).Format(timeFormat))
 			}
 
 			paramValues[i] = append(