Skip to content

Commit

Permalink
add mymysql.TimeString single function into mysql.go
Browse files Browse the repository at this point in the history
  • Loading branch information
martianzhang committed Dec 24, 2018
1 parent bf0d01c commit ddd0a90
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
15 changes: 15 additions & 0 deletions database/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"regexp"
"strconv"
"strings"
"time"

"github.com/XiaoMi/soar/common"

Expand Down Expand Up @@ -287,3 +288,17 @@ func (db *Connector) dangerousQuery(query string) bool {

return false
}

// Sandard MySQL datetime format
const TimeFormat = "2006-01-02 15:04:05.000000000"

// TimeString returns t as string in MySQL format Converts time.Time zero to MySQL zero.
func TimeString(t time.Time) string {
if t.IsZero() {
return "0000-00-00 00:00:00"
}
if t.Nanosecond() == 0 {
return t.Format(TimeFormat[:19])
}
return t.Format(TimeFormat)
}
3 changes: 1 addition & 2 deletions database/sampling.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"time"

"github.com/XiaoMi/soar/common"
"github.com/ziutek/mymysql/mysql"
)

/*--------------------
Expand Down Expand Up @@ -135,7 +134,7 @@ func (db *Connector) startSampling(onlineConn *sql.DB, database, table string, w
case "TIMESTAMP", "DATETIME":
t, err := time.Parse(time.RFC3339, string(val))
common.LogIfWarn(err, "")
values = append(values, fmt.Sprintf(`"%s"`, mysql.TimeString(t)))
values = append(values, fmt.Sprintf(`"%s"`, TimeString(t)))
default:
values = append(values, fmt.Sprintf(`unhex("%s")`, fmt.Sprintf("%x", val)))
}
Expand Down

0 comments on commit ddd0a90

Please sign in to comment.