Skip to content

Flag for disabling microseconds time resolution support #714

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

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
5 changes: 5 additions & 0 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ type mysqlConn struct {
sequence uint8
parseTime bool

disableMilliseconds bool

// for context support (Go 1.8+)
watching bool
watcher chan<- mysqlContext
Expand Down Expand Up @@ -231,6 +233,9 @@ func (mc *mysqlConn) interpolateParams(query string, args []driver.Value) (strin
if v.IsZero() {
buf = append(buf, "'0000-00-00'"...)
} else {
if mc.disableMilliseconds {
v = v.Round(time.Second)
}
v := v.In(mc.cfg.Loc)
v = v.Add(time.Nanosecond * 500) // To round under microsecond
year := v.Year()
Expand Down
1 change: 1 addition & 0 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func (d MySQLDriver) Open(dsn string) (driver.Conn, error) {
return nil, err
}
mc.parseTime = mc.cfg.ParseTime
mc.disableMilliseconds = mc.cfg.DisableMilliseconds

// Connect to Server
if dial, ok := dials[mc.cfg.Net]; ok {
Expand Down
17 changes: 17 additions & 0 deletions dsn.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type Config struct {
InterpolateParams bool // Interpolate placeholders into query string
MultiStatements bool // Allow multiple statements in one query
ParseTime bool // Parse time values to time.Time
DisableMilliseconds bool // Disable DATETIME sub-second precision and round time.Time down.
RejectReadOnly bool // Reject read-only connections
}

Expand Down Expand Up @@ -226,6 +227,15 @@ func (cfg *Config) FormatDSN() string {
}
}

if cfg.DisableMilliseconds {
if hasParam {
buf.WriteString("&disableMilliseconds=true")
} else {
hasParam = true
buf.WriteString("?disableMilliseconds=true")
}
}

if cfg.ReadTimeout > 0 {
if hasParam {
buf.WriteString("&readTimeout=")
Expand Down Expand Up @@ -488,6 +498,13 @@ func parseDSNParams(cfg *Config, params string) (err error) {
return errors.New("invalid bool value: " + value)
}

case "disableMilliseconds":
var isBool bool
cfg.DisableMilliseconds, isBool = readBool(value)
if !isBool {
return errors.New("invalid bool value: " + value)
}

// I/O read Timeout
case "readTimeout":
cfg.ReadTimeout, err = time.ParseDuration(value)
Expand Down