About me
Article launch | my blog | welcome to pay attention
Go language time.Now () returns the local time zone
time.Now().Format("2006-01-02 15:04:05")
Time setting custom time zone
var cstSh, _ = time.LoadLocation("Asia/Shanghai") //ShangHai
fmt.Println("SH : ", time.Now().In(cstSh).Format("2006-01-02 15:04:05"))
There is a problem with load location. It depends on IANA time zone database (tzdata for short). Generally, Linux system has it, but windows system does not. Therefore, if the go environment is not installed in the windows system, an error will be reported by calling loadlocation.
The current solutions include:
1. We can put the tzdata file into our own program directory, and then let the time package load the time zone file from our own program directory.
The file directory can be set through the environment variable. In the main method:
os.Setenv("ZONEINFO", '/home/tz/data.zip')
Then call the loadlocation method.
Download tzdata
2. Use time zone mode recommended mode
var cstZone = time.FixedZone("CST", 8*3600) // East 8 District
fmt.Println("SH : ", time.Now().In(cstZone).Format("2006-01-02 15:04:05"))
Global settings
If you want to set the global time zone configuration, what should you do?
main.go Add an initialization method (at the main function)
func initInMain() {
var cstZone = time.FixedZone("CST", 8*3600) // East 8 District
time.Local = cstZone
}
Assign the defined time zone instance to the time.Local
Recommended reading
Redis: a new open source charging tool
Star’s top engineer skill map on GitHub
Chinese programmers are most likely to send wrong words
Recommended! Markdown icon Index Website
Read More:
- Golang: How to Read File All Content in one time
- Golang: How to determine structure whether it is empty
- How to Solve OffsetParent is not set error
- [Go]Understand the golang project performance analysis tool trace
- [Go]Understand the golang project performance analysis tool PProf
- [Solved] Warning: To load an ES module, set “type“: “module“ in the package.json or use the .mjs extension
- Error in nextTick: “TypeError: Cannot set properties of undefined (setting ‘checked‘)“
- How to solve Uncaught (in promise) error in VUE?
- [Solved] Binding onclick event in JS: for loop: error uncaught typeerror: cannot set properties of undefined (setting ‘classname’)
- Localstorage sets the expiration time.
- [Vue warn]: Error in callback for watcher “value“ (How to Solve)
- Method to solve uncaught typeerror: cannot set property ‘onclick’ of null error
- Interface request error 504 gateway time out [How to Solve]
- Vue: How to Fix “not displaying the holder in IE9 and below”
- Vue a page is mounted to send multiple requests at the same time, and the loading is processed uniformly
- How to open a page in a new window by Vue router
- How to Solve pinia Error in ts File
- ERROR in [eslint] ESLint is not a constructor (How to Solve)
- How to Solve Uncaught (in promise) Error (Two Solutions)
- [Solved] React Startup Error at the First time :SyntaxError: Unexpected token