diff --git a/AUTHORS b/AUTHORS index 876b2964a..b46ecb2b3 100644 --- a/AUTHORS +++ b/AUTHORS @@ -83,6 +83,7 @@ Richard Wilkes Robert Russell Runrioter Wung Santhosh Kumar Tekuri +Santiago Corredoira Sho Iizuka Sho Ikeda Shuode Li diff --git a/README.md b/README.md index ded6e3b16..5ae297974 100644 --- a/README.md +++ b/README.md @@ -268,6 +268,18 @@ Note that this sets the location for time.Time values but does not change MySQL' Please keep in mind, that param values must be [url.QueryEscape](https://golang.org/pkg/net/url/#QueryEscape)'ed. Alternatively you can manually replace the `/` with `%2F`. For example `US/Pacific` would be `loc=US%2FPacific`. +##### `localTime` + +``` +Type: bool +Valid Values: true, false +Default: false +``` +Don't alter the location for time.Time values. + +Note that this ignores the `loc` setting. + + ##### `maxAllowedPacket` ``` Type: decimal number diff --git a/connection.go b/connection.go index 835f89729..70071177d 100644 --- a/connection.go +++ b/connection.go @@ -246,7 +246,11 @@ func (mc *mysqlConn) interpolateParams(query string, args []driver.Value) (strin buf = append(buf, "'0000-00-00'"...) } else { buf = append(buf, '\'') - buf, err = appendDateTime(buf, v.In(mc.cfg.Loc)) + if mc.cfg.LocalTime { + buf, err = appendDateTime(buf, v) + } else { + buf, err = appendDateTime(buf, v.In(mc.cfg.Loc)) + } if err != nil { return "", err } diff --git a/dsn.go b/dsn.go index a306d66a3..6a1a5c880 100644 --- a/dsn.go +++ b/dsn.go @@ -42,6 +42,7 @@ type Config struct { Params map[string]string // Connection parameters Collation string // Connection collation Loc *time.Location // Location for time.Time values + LocalTime bool // Don't alter the location for time.Time values MaxAllowedPacket int // Max packet size allowed ServerPubKey string // Server public key name pubKey *rsa.PublicKey // Server public key @@ -456,6 +457,14 @@ func parseDSNParams(cfg *Config, params string) (err error) { return } + // LocalTime + case "localTime": + var isBool bool + cfg.LocalTime, isBool = readBool(value) + if !isBool { + return errors.New("invalid bool value: " + value) + } + // multiple statements in one query case "multiStatements": var isBool bool diff --git a/dsn_test.go b/dsn_test.go index fc6eea9c8..99193e9ef 100644 --- a/dsn_test.go +++ b/dsn_test.go @@ -403,6 +403,17 @@ func TestNormalizeTLSConfig(t *testing.T) { } } +func TestDNSLocalTime(t *testing.T) { + dsn := "User:password@tcp(localhost:5555)/dbname?localTime=true" + cfg, err := ParseDSN(dsn) + + if err != nil { + t.Error(err.Error()) + } else if !cfg.LocalTime { + t.Errorf("expected localTime enabled") + } +} + func BenchmarkParseDSN(b *testing.B) { b.ReportAllocs() diff --git a/packets.go b/packets.go index ab30601ae..7bae6f910 100644 --- a/packets.go +++ b/packets.go @@ -1115,7 +1115,11 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error { if v.IsZero() { b = append(b, "0000-00-00"...) } else { - b, err = appendDateTime(b, v.In(mc.cfg.Loc)) + if mc.cfg.LocalTime { + b, err = appendDateTime(b, v) + } else { + b, err = appendDateTime(b, v.In(mc.cfg.Loc)) + } if err != nil { return err }