Golang Time zone settings

Source: Internet
Author: User
This is a created article in which the information may have evolved or changed.

When you deploy a PHP project, add a line to the PHP portal file:

date_default_timezone_set('PRC');

Time formatted output the time zone for the East Eight zone in Beijing time without the need for a relational system in the time zone.

In the go language, the go language time. Now () returns the time in the local time zone, directly using:

time.Now().Format("2006-01-02 15:04:05")

The time in the local time zone is output.

The go language does not have a global settings time zone such a thing, each output time needs to call an in () function to change the time zone:

var cstSh, _ = time.LoadLocation("Asia/Shanghai") //上海fmt.Println("SH : ", time.Now().In(cstSh).Format("2006-01-02 15:04:05"))

On Windows systems, time is not installed on the go locale. Loadlocation will fail to load.

var cstZone = time.FixedZone("CST", 8*3600)       // 东八fmt.Println("SH : ", time.Now().In(cstZone).Format("2006-01-02 15:04:05"))

The best way is to use time. Fixedzone

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.