Resolving the xorm time zone problem

Source: Internet
Author: User
Tags key string

If you upgrade xorm to the Go class library with newer versions (such as v0.6.3) and go-sql-driver (such as v1.3), you may experience time zone problems. Such as

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

When the write is a database, it will be changed to 2018-01-15T20:11:12+00:00 .
The above is the time zone problem, because we are using the 东8时区 default is set to 0时区 , the solution is very simple, only need to initialize the time zone in the main function or in the main package:

time.LoadLocation("Asia/Shanghai")

Database is configured to

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

The initialization of the Xorm is modified to:

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

The string conversion time also needs to be changed to

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

In this case, the Write library time zone problem can be solved, but read the library problems as follows:

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]), Elem. Field (i). Setint (x) Case String:elem. Field (i). SetString (String (Mapping[key])) Case Float64:x, _: = StrConv. Parsefloat (String (Mapping[key)), Elem. Field (i). SetFloat (x) Case Float32:x, _: = StrConv. Parsefloat (String (Mapping[key)), 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)}}}

Where MapToStruct the types in the function time.Time have a need for our attention, if the configured database is

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

The more &parseTime=true&loc=Local timeStr := string(mapping[key]) you get, the more it will be 2006-01-02T15:04:05+08:00 .
Then your conversion format should be 2006-01-02T15:04:05+08:00 .

To summarize:

    • In the project, the time zone must be set at the time of project initialization.
    • String conversion time to use as much as possibletime.ParseInLocation
    • parseTime=true&loc=Localor parseTime=true&loc=Asia%2FShanghai having an effect on the xorm parsing time type map[string][]byte
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.