Skip to content
Closed
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
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ Richard Wilkes <wilkes at me.com>
Robert Russell <robert at rrbrussell.com>
Runrioter Wung <runrioter at gmail.com>
Santhosh Kumar Tekuri <santhosh.tekuri at gmail.com>
Santiago Corredoira <scorredoira at gmail.com>
Sho Iizuka <sho.i518 at gmail.com>
Sho Ikeda <suicaicoca at gmail.com>
Shuode Li <elemount at qq.com>
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
9 changes: 9 additions & 0 deletions dsn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions dsn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
6 changes: 5 additions & 1 deletion packets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down