From 3d5a68ad94faa2d87a75513505e94b8405499f49 Mon Sep 17 00:00:00 2001 From: m4ex <4exmail@gmail.com> Date: Tue, 10 Sep 2024 13:24:20 +0300 Subject: [PATCH] mysqldialect: add time location support --- dialect/mysqldialect/dialect.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/dialect/mysqldialect/dialect.go b/dialect/mysqldialect/dialect.go index 959747d94..865d2aa7a 100644 --- a/dialect/mysqldialect/dialect.go +++ b/dialect/mysqldialect/dialect.go @@ -32,6 +32,7 @@ type Dialect struct { tables *schema.Tables features feature.Feature + loc *time.Location } func New() *Dialect { @@ -50,6 +51,16 @@ func New() *Dialect { return d } +func NewWithLocation(loc string) *Dialect { + d := New() + location, err := time.LoadLocation(loc) + if err != nil { + panic(fmt.Errorf("mysqldialect can't load provided location %s: %s", loc, err)) + } + d.loc = location + return d +} + func (d *Dialect) Init(db *sql.DB) { var version string if err := db.QueryRow("SELECT version()").Scan(&version); err != nil { @@ -103,9 +114,13 @@ func (d *Dialect) IdentQuote() byte { return '`' } -func (*Dialect) AppendTime(b []byte, tm time.Time) []byte { +func (d *Dialect) AppendTime(b []byte, tm time.Time) []byte { b = append(b, '\'') - b = tm.AppendFormat(b, "2006-01-02 15:04:05.999999") + if d.loc != nil { + b = tm.In(d.loc).AppendFormat(b, "2006-01-02 15:04:05.999999") + } else { + b = tm.AppendFormat(b, "2006-01-02 15:04:05.999999") + } b = append(b, '\'') return b }