About me
The article start | my blog | welcome the attention
Time.now () of the go language returns the local time zone
time.Now().Format("2006-01-02 15:04:05")
Copy the code
Time User-defined time zone
Var cstSh, _ = time.LoadLocation("Asia/Shanghai") // LoadLocation("Asia/Shanghai") ", time.Now().In(cstSh).Format("2006-01-02 15:04:05"))Copy the code
The problem with LoadLocation is that it relies on the IANA Time Zone Database (TZData), which is normally installed on Linux but not on Windows. So if the Go environment is not installed on your Windows system, calling LoadLocation will result in an error.
Current solutions also include:
1. We can put the tzData file in our own program directory, and then let the time package load the time zone file from our program directory.
File directories can be set using environment variables, in the main method:
os.Setenv("ZONEINFO", '/home/tz/data.zip')
Copy the code
Then call the LoadLocation method.
Download tzdata
2. Use the recommended time zone mode
Var cstZone = time.FixedZone("CST", 8*3600) // mdt. Println("SH: ", time.Now().In(cstZone).Format("2006-01-02 15:04:05"))Copy the code
Global Settings
What if you want to set the global time zone configuration?
Add an initialization method to main.go(at main function)
Func initInMain() {var cstZone = time.FixedZone("CST", 8*3600) // east8 time.Local = cstZone}Copy the code
Assign the defined time zone instance to time.local
Recommended reading
New open source has emerged after Redis tools were charged
The highest engineer skills chart on GitHub
Chinese programmers are most prone to mispronounce words
Recommended!!!!!! Markdown icon index site
The last
The end of this article, I hope to help you 😃
If there are any questions or suggestions, you can exchange more original articles, writing is limited, talent and learning is shallow, if there is something wrong in the article, hope to inform.
More wonderful technical articles in my public number [programmer tools]], continue to update, welcome to pay attention to the subscription collection