Skip to content

Commit

Permalink
Fix convert time to local panic when time is nil
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Oct 17, 2022
1 parent c538c38 commit ca4edc1
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,17 +316,20 @@ func (dialector Dialector) QuoteTo(writer clause.Writer, str string) {
writer.WriteByte('`')
}

type localTimeInterface interface {
In(loc *time.Location) time.Time
}

func (dialector Dialector) Explain(sql string, vars ...interface{}) string {
if dialector.DSNConfig.Loc == time.Local {
if dialector.DSNConfig != nil && dialector.DSNConfig.Loc == time.Local {
for i, v := range vars {
switch v.(type) {
case time.Time:
vars[i] = v.(time.Time).In(time.Local)
case *time.Time:
if v.(*time.Time) != nil {
newValue := v.(*time.Time).In(time.Local)
vars[i] = &newValue
}
if p, ok := v.(localTimeInterface); ok {
func(i int, t localTimeInterface) {
defer func() {
recover()
}()
vars[i] = t.In(time.Local)
}(i, p)
}
}
}
Expand Down

1 comment on commit ca4edc1

@lvxiaorun
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is much stronger and more elegant than mine

Please # to comment.