Golang Resolving the xorm time zone problem

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. If you upgrade to the Go Class library with newer versions of ' Xorm ' (such as v0.6.3) and ' go-sql-driver ' (such as v1.3), you may experience time zone problems. such as ' Gotime '. Parse ("2006-01-02 15:04:05", "2018-01-15 12:11:12")//2018-01-15t12:11:12+00:00 "' Write is the database time will be changed to ' 2018-01-15t20 : 11:12+00:00 '. The above is the time zone problem, because we use the ' East 8 time zone ', default will be set to ' 0 time zone ', the solution is very simple, only need to initialize the time zone in the main function or the main package: "' Gotime. Loadlocation ("Asia/shanghai") ' database is configured as ' Root:root@tcp (127.0.0.1:3306)/test?charset=utf8&interpolateparams =true ' Xorm initialization modified to: ' ' goorm, err: = Initorm (ds, Maxidleconn, Maxopenconn, Debug) if err! = Nil {return nil, Err}r.value = Ormorm. Databasetz = time. Local//Must be ORM. Tzlocation = time. Local//Must be ORM. Setmaxidleconns (Maxidleconn) Orm. Setmaxopenconns (Maxopenconn) ' ' String conversion time also needs to be changed to ' ' Gotime. Parseinlocation ("2006-01-02 15:04:05", "2018-01-15 12:11:12", time. Local) "This time the library time zone problem can be solved, but the reading library problem is 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. Imageconfmaptostruct (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 stringkey = Elem. Type (). Field (i). Nameswitch 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 ' time ' in the ' maptostruct ' function. The time ' Type here is one that needs our attention if the configured database is ' root:root@tcp ' (127.0.0.1:3306)/test?charset=utf8&interpolateparams=true &parsetime=true&loc=local ' &parsetime=true&loc=local ' at this time ' timestr: = string (Mapping[key]) ' The resulting 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 a project, the time zone must be set at the time the project is initialized-the string conversion time is used whenever possible. Parseinlocation '-' parsetime=true&loc=local ' or ' Parsetime=true&loc=asia%2fshanghai ' to ' xorm ' parsing time type ' map[ String][]byte ' has an impact 255 reads  
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.