Skip to content

Commit cdf80ec

Browse files
committed
fix: 读取数据库时间格式化
1 parent a293fb2 commit cdf80ec

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

bootstrap/servers/queue_delay.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func (d *DelayQueueForMysql) Loop() {
7777
interval := app2.Config("queue.delay.interval", 60)
7878
for {
7979
list := make([]*OrmDelayQueue, 0)
80-
dbRet := d.mysql.Model(&OrmDelayQueue{}).Where("run_at <= ? and fail = 0", time.Now()).Limit(100).Order("Id desc").Find(&list)
80+
dbRet := d.mysql.Model(&OrmDelayQueue{}).Where("run_at <= ? and fail = 0", time.Now()).Limit(100).Order("id desc").Find(&list)
8181

8282
if dbRet.Error != nil {
8383
logrus.Error(dbRet.Error)

bootstrap/services/database/time.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,22 @@ func (t Time) Value() (driver.Value, error) {
3535

3636
// Scan 读取数据库时会调用该方法将时间数据转换成自定义时间类型
3737
func (t *Time) Scan(v interface{}) error {
38-
value, ok := v.(time.Time)
39-
if ok {
38+
switch v.(type) {
39+
case time.Time:
40+
value, _ := v.(time.Time)
4041
t.Time = value
4142
return nil
43+
case []byte:
44+
value, _ := v.([]byte)
45+
dateString := string(value)
46+
var err error
47+
t.Time, err = time.Parse("2006-01-02 15:04:05", dateString)
48+
if err != nil {
49+
return fmt.Errorf("无法格式化数据, 时间当前支持Y-m-d H:i:s %v to timestamp", v)
50+
}
51+
return nil
4252
}
43-
return fmt.Errorf("can not convert %v to timestamp", v)
53+
return fmt.Errorf("无法格式化数据 %v to timestamp", v)
4454
}
4555

4656
func (t *Time) UnmarshalJSON(data []byte) error {

0 commit comments

Comments
 (0)