Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the time module in golang

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Xiaobian to share with you how to use the time module in golang, I hope you have something to gain after reading this article, let's discuss it together!

Common examples of time

Print current timestamp

fmt.Println(time.Now().Unix())# 1389058332

str formatting time

fmt.Println(time.Now().Format("2006-01-02 15:04:05")) #This is a strange thing, must be this time point, it is said to be the birth day of go, memory method:6-1-2-3-4- 5#2018 -08-25 09:42:20

Time stamp to str formatting time

str_time := time.Unix(1389058332, 0).Format("2006-01-02 15:04:05")fmt.Println(str_time)str Format time to timestamp

WAR package

the_time := time.Date(2014, 1, 7, 5, 50, 4, 0, time.Local)unix_time := the_time.Unix()fmt.Println(unix_time)# 1389045004

time.Parse formatting time

the_time, err := time.Parse("2006-01-02 15:04:05", "2014-01-08 09:04:41")if err == nil {unix_time := the_time.Unix()fmt.Println(unix_time)}#1389171881 Difference between two, time.Parse and time.Format

Under windows, the time zone of time.Parse() is identical to the time zone of time.Format(). However, in Linux environments, the default time zone for time.Parse() is UTC (8 hours away from the standard time CST we use), and the default time zone for time.Format() is local. If you don't handle both well, it will lead to errors. Specifically, you can do the following operation, find a linux host, change its/etc/localtime from Shanghai to UTC, and then execute the following code respectively, see the difference between the output:

package mainimport "time"import "fmt"func main(){t, err := time.Parse("2006-01-02 15:04:05", "2017-12-03 22:01:02")//localTime, err := time.ParseInLocation("2006-01-02 15:04:05", "2017-12-03 22:01:02", time.Local)if err != nil{fmt.Println(err)return}fmt.Println(t)fmt.Println(time.Now())fmt.Println(time.Now().Sub(t).Seconds())}

Use time.Parse and time.ParseInLocation to calculate the difference between the two times respectively. Which value is correct?

III. Another example

Finally, an example code for outputting different information such as the current time year, month, day, hour, etc. is given as follows:

package mainimport func main(){t := time.Now()y,m,d := t.Date()today := time. Now ().Format("2006-01-02")datetime := time. Now (). Format ("2006 - 01 -02 15:04:05")//The following parameters are fixed or fmt.Println cannot be output properly ("time is : ",t )fmt.Println("y m d is : ",y,m,d )fmt. Println("now is :",today)fmt. Println ("now is :",datetime)} After reading this article, I believe you have a certain understanding of" how to use the time module in golang". If you want to know more relevant knowledge, please pay attention to the industry information channel. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report