解决XORM的时区问题
如果你升级使用了较为新版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%2FShanghai
对xorm
解析时间类型为map[string][]byte
有着影响

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
细说智能指针
提到指针,我们就会想到指针的高效,当然,滥用指针也会为我们带来许多的潜在bug。提到指针,我们就会想到内存泄漏。比如,使用指针后忘记释放,久而久之,堆空间就会全部使用完,那么会带来很大的危害。再比如,两个指针指向同一片内存区域,我们对同一片区域进行了多次释放,同样会造成内存泄漏。为了方便大家的理解,我们先来模拟一下,使用指针却忘记释放带来的危害。首先,我们要定义一个类。这次,还是定义女朋友类吧(之前写过一篇《细说C++的友元》用的就是女朋友类,这次还用这个吧,方便说明问题,更何况我们这群码畜怎么会有女朋友呢,没有女朋友只能自己创建了,哈哈哈哈)。女生都喜欢自拍,尤其是漂亮的女生。所以,女生会有很多照片,对吧。那么,我们创建的这个女朋友类,就让她有照片吧。当然了,你女朋友的照片肯定不会随便给别人的吧,所以要把picutre这个变量声明为private类型。既然,女生喜欢自拍,并且发朋友圈,也就是说,其他人虽然得不到她的照片,却可以通过她的朋友圈看到她的自拍,也就意味着我们可以通过一个public函数访问picture这个变量。那么,我们现在来写代码。 class Girlfriend{ ...
- 下一篇
DBA的40条军规
DBA操作规范 1、涉及业务上的修改/删除数据,在得到业务方、CTO的邮件批准后方可执行,执行前提前做好备份,必要时可逆。 2、所有上线需求必须走工单系统,口头通知视为无效。 3、在对大表做表结构变更时,如修改字段属性会造成锁表,并会造成从库延迟,从而影响线上业务,必须在凌晨0:00 后业务低峰期执行,另统一用工具 pt-online-schema-change 避免锁表且降低延迟执行时间。 使用范例: #pt-online-schema-change --alter="add index IX_id_no(id_no)" \ --no-check-replication-filters --recursion-method=none --user=dba \ --password=123456 D=test,t=t1 --execute 对于MongoDB创建索引要在后台创建,避免锁表。 使用范例: db.t1.createIndex({idCardNum:1},{background:1}) 4、所有线上业务库均必须搭建MHA高可用架构,避免单点问题。 5、给业务方开权限时,密码要...
相关文章
文章评论
共有0条评论来说两句吧...