代码拉取完成,页面将自动刷新
package database
import (
"database/sql/driver"
"errors"
"fmt"
"strings"
"time"
)
//sgcTime 解决gorm中对mysql中datetime类型字段无法scan的问题
//参考https://blog.csdn.net/qq_37493556/article/details/105082422
type sgcTime time.Time
//UnmarshalJSON ...
func (t *sgcTime) UnmarshalJSON(data []byte) error {
if string(data) == "null" {
return nil
}
var err error
//前端接收的时间字符串
str := string(data)
//去除接收的str收尾多余的"
timeStr := strings.Trim(str, "\"")
t1, err := time.Parse("2006-01-02 15:04:05", timeStr)
*t = sgcTime(t1)
return err
}
//MarshalJSON ...
func (t sgcTime) MarshalJSON() ([]byte, error) {
formatted := fmt.Sprintf("\"%v\"", time.Time(t).Format("2006-01-02 15:04:05"))
return []byte(formatted), nil
}
//Value ...
func (t sgcTime) Value() (driver.Value, error) {
// sgcTime 转换成 time.Time 类型
tTime := time.Time(t)
return tTime.Format("2006-01-02 15:04:05"), nil
}
//Scan ...
func (t *sgcTime) Scan(v interface{}) error {
switch vt := v.(type) {
case time.Time:
// 字符串转成 time.Time 类型
*t = sgcTime(vt)
default:
return errors.New("类型处理错误")
}
return nil
}
//String ...
func (t *sgcTime) String() string {
return fmt.Sprintf("hhh:%s", time.Time(*t).String())
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。