Skip to content

Commit c0fdf9d

Browse files
committed
Update default now func, close go-gorm/gorm#4155
1 parent 11089cf commit c0fdf9d

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ go 1.14
44

55
require (
66
github.com/go-sql-driver/mysql v1.5.0
7-
gorm.io/gorm v1.21.2
7+
gorm.io/gorm v1.21.3
88
)

mysql.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"math"
88
"strings"
9+
"time"
910

1011
_ "github.com/go-sql-driver/mysql"
1112
"gorm.io/gorm"
@@ -45,6 +46,19 @@ func (dialector Dialector) Name() string {
4546
return "mysql"
4647
}
4748

49+
func (dialector Dialector) Apply(config *gorm.Config) error {
50+
if config.NowFunc == nil {
51+
if dialector.DefaultDatetimePrecision == nil {
52+
var defaultDatetimePrecision = 3
53+
dialector.DefaultDatetimePrecision = &defaultDatetimePrecision
54+
}
55+
56+
round := time.Second / time.Duration(math.Pow10(*dialector.DefaultDatetimePrecision))
57+
config.NowFunc = func() time.Time { return time.Now().Local().Round(round) }
58+
}
59+
return nil
60+
}
61+
4862
func (dialector Dialector) Initialize(db *gorm.DB) (err error) {
4963
ctx := context.Background()
5064

0 commit comments

Comments
 (0)