Home > Backend Development > Golang > How to set time zone in go language

How to set time zone in go language

青灯夜游
Release: 2023-01-09 10:27:45
Original
3495 people have browsed it

Two setting methods: 1. Fixed time zone, syntax "var c = time.FixedZone("CST", 8*3600)" and "time.Now().In(c).Format(" Time ")"; 2. Load time zone, syntax "var c, _ = time.LoadLocation("Asia/Shanghai")" and "time.Now().In(c).Format("time")".

How to set time zone in go language

The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.

Division of time zones

The world takes Greenwich, London, England as the starting point of the zero-degree longitude, and every 15 longitudes is a time zone, 15 degrees The 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

Current timetime.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
}
Copy after login

CST time

CST can represent the following four different time zones:

  • Central Standard Time (USA) UT-6:00: United States Standard Time
  • Central Standard Time (Australia) UT 9:30:Australia Standard Time
  • China Standard Time UT 8:00:China Standard Time
  • Cuba Standard Time UT- 4:00: Cuba Standard Time

time.Now()The 0800 CST returned by Conversion:

GMT + 8 = UTC + 8 = CST
Copy after login

Monotonic Clocks and Wall Clocks

Wall Clocks represents wall clock time and stores the time since 0:00:00 on January 1, 1970 Timestamp, when the system and timing server perform time calibration operations, it may cause this second to be 2018-1-1 00:00:00, and the next second to become 2017-12-31 23:59:59 Case.
Monotonic Clocks means monotonic time. The so-called monotonic means that it will only continue to grow forward and is not affected by the time adjustment operation. This time is the number of seconds since the process started.
time.Now()The returned m= 0.004002201 means Monotonic Clocks

Time zone setting

If the specified time zone is not set in the go language, the local time zone is obtained through time.Now():

func main() {
    t := time.Now()   //返回本地时区格式
    fmt.Println(t)
}
Copy after login

The go language does not have such a global setting time zone Something, every time you output the time, you need to call an In() function to change the time zone.

There are two ways to set the time zone:

1. Fixed time zone (recommended)

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

Fix the time zone to the East Eighth District. But this is not a global setting for the program. Each time you obtain it, you need to fix the time zone

2 and load the time zone

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

to 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.

Note: 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"]
Copy after login

[Related recommendations: Go video tutorial, Programming teaching

The above is the detailed content of How to set time zone in go language. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template