Skip to content

Commit

Permalink
fix time layout
Browse files Browse the repository at this point in the history
  • Loading branch information
lqs committed Sep 6, 2024
1 parent d32d0ea commit 3b42e2a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 15 deletions.
37 changes: 24 additions & 13 deletions cursor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"database/sql"
"fmt"
"reflect"
"regexp"
"strconv"
"strings"
"time"
)

Expand All @@ -31,23 +33,32 @@ func (c cursor) Next() bool {

var timeType = reflect.TypeOf(time.Time{})

var timeLayouts = []string{
"2006-01-02",
"2006-01-02 15:04:05",
"2006-01-02 15:04:05.000",
"2006-01-02 15:04:05.000000",
"2006-01-02 15:04:05.000000000",
time.RFC3339Nano,
var simpleTimeLayoutRegexp = regexp.MustCompile(`^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}(\.(\d+))?$`)

func guessTimeLayout(s string) string {
matches := simpleTimeLayoutRegexp.FindStringSubmatch(s)
if len(matches) > 0 {
var sb strings.Builder
sb.Grow(32)
sb.WriteString("2006-01-02 15:04:05")
if matches[1] != "" {
sb.WriteString(".")
for i := 0; i < len(matches[2]); i++ {
sb.WriteByte('0')
}
}
return sb.String()
}
return time.RFC3339Nano
}

func parseTime(s string) (time.Time, error) {
for _, layout := range timeLayouts {
t, err := time.Parse(layout, s)
if err == nil {
return t, nil
}
layout := guessTimeLayout(s)
t, err := time.Parse(layout, s)
if err != nil {
return time.Time{}, fmt.Errorf("unknown time format %s: %w", s, err)
}
return time.Time{}, fmt.Errorf("unknown time format %s", s)
return t, nil
}

func isScanner(val reflect.Value) bool {
Expand Down
22 changes: 22 additions & 0 deletions cursor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,25 @@ func TestCursorMap(t *testing.T) {
t.Error()
}
}

func TestParseTime(t *testing.T) {
tests := []struct {
input string
output time.Time
}{
{"2024-09-06 11:22:33", time.Date(2024, 9, 6, 11, 22, 33, 0, time.UTC)},
{"2024-09-06 11:22:33.444", time.Date(2024, 9, 6, 11, 22, 33, 444000000, time.UTC)},
{"2024-09-06 11:22:33.444555666", time.Date(2024, 9, 6, 11, 22, 33, 444555666, time.UTC)},
{"2024-09-06T11:22:33.444555666Z", time.Date(2024, 9, 6, 11, 22, 33, 444555666, time.UTC)},
}
for _, test := range tests {
tm, err := parseTime(test.input)
if err != nil {
t.Error(err)
continue
}
if tm != test.output {
t.Error(tm, test.output)
}
}
}
5 changes: 3 additions & 2 deletions expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ func quoteString(s string) string {
}

func getSQL(scope scope, value interface{}) (sql string, priority priority, err error) {
const mysqlTimeFormat = "2006-01-02 15:04:05.000000"
if value == nil {
sql = "NULL"
return
Expand Down Expand Up @@ -355,14 +356,14 @@ func getSQL(scope scope, value interface{}) (sql string, priority priority, err
case CaseExpression:
sql, err = value.(CaseExpression).End().GetSQL(scope)
case time.Time:
tmStr := value.(time.Time).Format("2006-01-02 15:04:05")
tmStr := value.(time.Time).Format(mysqlTimeFormat)
sql = quoteString(tmStr)
case *time.Time:
tm := value.(*time.Time)
if tm == nil {
sql = "NULL"
} else {
tmStr := tm.Format("2006-01-02 15:04:05")
tmStr := tm.Format(mysqlTimeFormat)
sql = quoteString(tmStr)
}
default:
Expand Down

0 comments on commit 3b42e2a

Please sign in to comment.