mirror of https://github.com/lqs/sqlingo
handle MySQL zero date
This commit is contained in:
parent
65c4cf7d2d
commit
2a5c602f5e
|
|
@ -53,6 +53,10 @@ func guessTimeLayout(s string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseTime(s string) (time.Time, error) {
|
func parseTime(s string) (time.Time, error) {
|
||||||
|
if strings.HasPrefix(s, "0000-00-00") {
|
||||||
|
// MySQL zero date
|
||||||
|
return time.Time{}, nil
|
||||||
|
}
|
||||||
layout := guessTimeLayout(s)
|
layout := guessTimeLayout(s)
|
||||||
t, err := time.Parse(layout, s)
|
t, err := time.Parse(layout, s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -219,6 +219,7 @@ func TestParseTime(t *testing.T) {
|
||||||
{"2024-09-06 11:22:33.444", time.Date(2024, 9, 6, 11, 22, 33, 444000000, 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-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)},
|
{"2024-09-06T11:22:33.444555666Z", time.Date(2024, 9, 6, 11, 22, 33, 444555666, time.UTC)},
|
||||||
|
{"0000-00-00 00:00:00", time.Time{}},
|
||||||
}
|
}
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
tm, err := parseTime(test.input)
|
tm, err := parseTime(test.input)
|
||||||
|
|
|
||||||
|
|
@ -356,11 +356,16 @@ func getSQL(scope scope, value interface{}) (sql string, priority priority, err
|
||||||
case CaseExpression:
|
case CaseExpression:
|
||||||
sql, err = value.(CaseExpression).End().GetSQL(scope)
|
sql, err = value.(CaseExpression).End().GetSQL(scope)
|
||||||
case time.Time:
|
case time.Time:
|
||||||
tmStr := value.(time.Time).Format(mysqlTimeFormat)
|
tm := value.(time.Time)
|
||||||
sql = quoteString(tmStr)
|
if tm.IsZero() {
|
||||||
|
sql = "NULL"
|
||||||
|
} else {
|
||||||
|
tmStr := tm.Format(mysqlTimeFormat)
|
||||||
|
sql = quoteString(tmStr)
|
||||||
|
}
|
||||||
case *time.Time:
|
case *time.Time:
|
||||||
tm := value.(*time.Time)
|
tm := value.(*time.Time)
|
||||||
if tm == nil {
|
if tm == nil || tm.IsZero() {
|
||||||
sql = "NULL"
|
sql = "NULL"
|
||||||
} else {
|
} else {
|
||||||
tmStr := tm.Format(mysqlTimeFormat)
|
tmStr := tm.Format(mysqlTimeFormat)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue