golang time conversion

PHPz
Release: 2023-05-27 13:38:08
Original
3630 people have browsed it

Golang is an efficient and concise programming language that is widely used in various fields. In development, we often need to convert time, such as converting strings to time, converting time to strings, etc., and the different time formats will also cause certain problems. Therefore, this article will introduce the time conversion operation in Golang and explain its specific usage through examples.

1. Convert string to time

In Golang, we can use the Parse function in the time package to convert a string into time. The specific definition of the Parse function is as follows:

func Parse(layout, value string) (Time, error)

Among them, layout represents the time format of the string to be converted, and value represents the character to be converted. string. These two parameters can use the predefined constants in the time package. The specific constants and their meanings are as follows:

Time constant Meaning
ANSIC "Mon Jan _2 15:04:05 2006"
UnixDate "Mon Jan _2 15:04:05 MST 2006"
RFC3339 "2006-01-02T15:04:05Z07:00"

For example, the sample code to convert the string "2022/04/12 15:20:00" into time is as follows:

timeStr := "2022/04/12 15:20:00"
layout := "2006/01/02 15:04:05"
t, err := time.Parse(layout, timeStr)
if err != nil {
    fmt.Println("转化失败:", err)
} else {
    fmt.Println("转化成功:", t)
}
Copy after login

Among them, timeStr is the string to be converted, layout is the time format of a string ("2006/01/02 15:04:05" represents the year, month, day, hour, minute, and second), t is the time object obtained after conversion, and err is the error message during the conversion process. Run the above code and the output result is as follows:

转化成功: 2022-04-12 15:20:00 +0000 UTC
Copy after login

2. Convert time to string

Similarly, in Golang, we can use the Format function to convert time into a string. The specific definition of this function is as follows:

func (t Time) Format(layout string) string

Among them, t is the time object to be converted, and layout represents the formatted time string. Its usage is also similar to the Parse function introduced above.

For example, the sample code to convert time into a string in the format of "2022/04/12 15:20:00" is as follows:

t := time.Now()
layout := "2006/01/02 15:04:05"
timeStr := t.Format(layout)
fmt.Println("转化后时间字符串:", timeStr)
Copy after login

Among them, t is the current time and layout is the conversion The format of the subsequent time ("2006/01/02 15:04:05" represents the year, month, day, hour, minute, and second), and timeStr is the converted time string. Run the above code and the output result is as follows:

转化后时间字符串: 2022/04/12 15:20:00
Copy after login

3. Time zone conversion

In Golang, the time zone representation of time can be obtained using the Location function in the time package. Its definition is as follows:

func LoadLocation(name string) (*Location, error)

Among them, name represents the time zone name (such as "Asia/Shanghai").

For example, when converting local time to UTC time, you can use the UTC function in the time package. The sample code is as follows:

t := time.Now()
utc := t.UTC()
fmt.Println("本地时间:", t)
fmt.Println("UTC时间:", utc)
Copy after login

Among them, t is the local time and utc is the converted UTC time. Run the above code and the output result is as follows:

本地时间: 2022-10-07 15:24:56.200122 +0800 CST m=+0.000498761
UTC时间: 2022-10-07 07:24:56.200122 +0000 UTC
Copy after login

4. Time addition and subtraction

In Golang, time addition and subtraction can be performed using the Add function and Sub function. Its specific definition is as follows:

func (t Time) Add(d Duration) Time
func (t Time) Sub(u Time) Duration

Among them, the Add function adds time t The previous Duration object d returns a new time object; the Sub function subtracts another time object u from time t and returns a Duration object.

For example, the sample code for adding 10 minutes to the existing time is as follows:

t := time.Now()
d := 10 * time.Minute
newTime := t.Add(d)
fmt.Println("原时间:", t)
fmt.Println("加10分钟后的时间:", newTime)
Copy after login

Among them, t is the existing time, d is the time interval to be added (10 minutes), newTime is the new time obtained after adding. Run the above code and the output result is as follows:

原时间: 2022-10-07 15:35:26.924559 +0800 CST m=+0.000213898
加10分钟后的时间: 2022-10-07 15:45:26.924559 +0800 CST
Copy after login

5. How to optimize the time conversion efficiency

In actual development, we may have a large number of time conversion needs, and use Golang’s own time Converting the package will greatly reduce the efficiency of the program. At this time, we can use third-party libraries for optimization.

Currently, the more popular time conversion libraries include:

  • The time conversion component in the Gorm library
  • Rob Pike’s open source Time library
  • Bjoernu's open source TimeUtils library

These third-party libraries can greatly improve the efficiency of time conversion, and also solve the problems caused by different time layouts on different systems.

Summary

This article introduces the operation of time conversion in Golang, including converting strings into time, converting time into strings, time zone conversion, and time addition and subtraction. At the same time, some common time conversion libraries are also provided to optimize time conversion efficiency. I hope this article can provide some help to Golang developers in time conversion.

The above is the detailed content of golang time conversion. For more information, please follow other related articles on the PHP Chinese website!

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