温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

解决XORM的时区问题

发布时间:2020-03-30 01:38:02 来源:网络 阅读:8597 作者:梦朝思夕 栏目:编程语言

如果你升级使用了较为新版xorm(如v0.6.3)和go-sql-driver(如v1.3)的go类库,那么你就可能会遇到时区问题。 如

time.Parse("2006-01-02 15:04:05" ,"2018-01-15 12:11:12") // 2018-01-15T12:11:12+00:00

写入是数据库时候就会被改变为2018-01-15T20:11:12+00:00
上述的就是时区问题,因为我们使用的是东8时区,默认会被设置为0时区,解决方案很简单,只需要在main函数中或者main包中初始化时区:

time.LoadLocation("Asia/Shanghai")

数据库配置为

root:root@tcp(127.0.0.1:3306)/test?charset=utf8&interpolateParams=true

xorm的初始化修改为:

orm, err := initOrm(ds, maxIdleConn, maxOpenConn, debug)
if err != nil {
    return nil, err
}
r.Value = orm
orm.DatabaseTZ = time.Local // 必须
orm.TZLocation = time.Local // 必须
orm.SetMaxIdleConns(maxIdleConn)
orm.SetMaxOpenConns(maxOpenConn)

字符串转换时间也需要改为

time.ParseInLocation("2006-01-02 15:04:05" ,"2018-01-15 12:11:12",time.Local)

此时写库时区问题就可以得到解决了,但是读库问题如下的的方式:

rss, err := this.Repo.Query(ctx, sqlStr, pos, now, os)
images := make([]*models.ImageConf, 0, len(rss))

for _, rs := range rss {
    var tmpImage models.ImageConf
    MapToStruct(rs, &tmpImage)
    images = append(images, &tmpImage)
}

func MapToStruct(mapping map[string][]byte, j interface{}) {
    elem := reflect.ValueOf(j).Elem()
    for i := 0; i < elem.NumField(); i++ {
        var key string
        key = elem.Type().Field(i).Name
        switch elem.Field(i).Interface().(type) {
        case int, int8, int16, int32, int64:
            x, _ := strconv.ParseInt(string(mapping[key]), 10, 64)
            elem.Field(i).SetInt(x)
        case string:
            elem.Field(i).SetString(string(mapping[key]))
        case float64:
            x, _ := strconv.ParseFloat(string(mapping[key]), 64)
            elem.Field(i).SetFloat(x)
        case float32:
            x, _ := strconv.ParseFloat(string(mapping[key]), 32)
            elem.Field(i).SetFloat(x)
        case time.Time:
            timeStr := string(mapping[key])
            timeDB, err := time.ParseInLocation("2006-01-02 15:04:05", timeStr, time.Local)
            if err != nil {
                timeDB, err = time.ParseInLocation("2006-01-02", timeStr, time.Local)
                if err != nil {
                    timeDB, err = time.ParseInLocation("15:04:05", timeStr, time.Local)
                } else {
                    timeDB = time.Date(0, 0, 0, 0, 0, 0, 1, time.Local)
                }
            }
            elem.Field(i).Set(reflect.ValueOf(timeDB))
        }
    }
}

其中MapToStruct函数中的time.Time类型这儿有一个需要我们注意的,如果配置的数据库为

root:root@tcp(127.0.0.1:3306)/test?charset=utf8&interpolateParams=true&parseTime=true&loc=Local

多出了&parseTime=true&loc=Local此时timeStr := string(mapping[key])得到的将会是2006-01-02T15:04:05+08:00
那么你的转换格式应该为2006-01-02T15:04:05+08:00

总结一下:

  • 在项目中时区一定要在项目初始化时候就已经设置好
  • 字符串转换时间尽可能使用time.ParseInLocation
  • parseTime=true&loc=Local或者parseTime=true&loc=Asia%2FShanghaixorm解析时间类型为map[string][]byte有着影响
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI