Home > Article > Backend Development > How to set time zone in golang
How to set the time zone in golang: 1. Fixed the time zone to the East Eighth District, and set the code such as "var cstZone = time.FixedZone("CST", 8*3600)"; 2. Load the specified time zone and set the code Such as "var cstSh, _ = time.LoadLocation("Asia/Shanghai")".
The operating environment of this tutorial: Windows 10 system, GO version 1.18, Dell G3 computer.
How to set the time zone in golang?
Go time zone setting
Time zone division
The world uses Greenwich, London, England as the starting point of the zero-degree longitude, every 15 longitudes It is a time zone, and the 15th meridian is the central meridian of the time zone, which is divided into 24 time zones. A time zone increases every 15 degrees of longitude from west to east, and conversely, a time zone decreases every 15 degrees of longitude to the west. China's time zone is East 8.
Time format
The current time time.Now() returns the time in the local time zone:
func main() { t := time.Now() fmt.Println(t) //2020-12-16 09:34:19.5828312 +0800 CST m=+0.004002201 }
CST time
CST can represent the following four different time zones:
Central Standard Time (USA) UT-6:00:美国标准时间 Central Standard Time (Australia) UT+9:30:澳大利亚标准时间 China Standard Time UT+8:00:中国标准时间 Cuba Standard Time UT-4:00:古巴标准时间
The 0800 CST returned by time.Now() represents China Standard Time, and has the following conversion with UTC time:
GMT + 8 = UTC + 8 = CST
Monotonic Clocks and Wall Clocks
Wall Clocks represents wall clock time and stores the timestamp since 0:00:00 on January 1, 1970. When the system When calibrating the time with the timing server, it is possible that this second is 2018-1-1 00:00:00, and the next second becomes 2017-12-31 23:59:59.
Monotonic Clocks means monotonic time. The so-called monotonic means that it will only keep growing forward and will not be affected by the time adjustment operation. This time is the number of seconds since the process started.
time.Now() returns m= 0.004002201 which means Monotonic Clocks
Time zone setting
If the specified time zone is not set in the go language, What is obtained through time.Now() is the local time zone
func main() { t := time.Now() //返回本地时区格式 fmt.Println(t) }
There are two ways to set the time zone:
Fixed time zone (recommended)
var cstZone = time.FixedZone("CST", 8*3600) // 东八 fmt.Println(time.Now().In(cstZone).Format("2006-01-02 15:04:05"))
Fixed time zone to East Eighth District. But this is not a global setting for the program. The time zone needs to be fixed every time it is obtained.
Load time zone
var cstSh, _ = time.LoadLocation("Asia/Shanghai") // 上海 fmt.Println(time.Now().In(cstSh).Format("2006-01-02 15:04:05"))
Load the specified time zone. But if you use this method without a go environment, the loading will fail, because the time zone information is placed in the go installation package.
Configuring the time zone in docker
If you use the second method to load the time zone, you need to configure the time zone when building the docker image. The configuration file is as follows:
FROM golang:alpine as build RUN apk --no-cache add tzdata //构建阶段加载时区 WORKDIR /app ADD . /app RUN CGO_ENABLED=0 GOOS=linux go build -o myapp FROM scratch as final COPY --from=build /app/myapp . ### 下面这行是新加的,复制时区相关的信息到最终镜像中,否则程序会找不到改时区 COPY --from=build /usr/share/zoneinfo /usr/share/zoneinfo ENV TZ=Asia/Shanghai //在最终阶段设置时区环境变量 CMD ["/myapp"]
Recommended learning: "go video tutorial"
The above is the detailed content of How to set time zone in golang. For more information, please follow other related articles on the PHP Chinese website!