time library
A core library for handling time, date, and time zone. In actual development, it is often necessary to deal with time, such as recording logs, processing time difference, calculating time intervals, etc. Therefore, it is very important for Go developers to master the use of time libraries.
In Go language, time is represented as type, date is represented as type or type, and time zone is represented as type. The concepts and usage methods of these types will be described in detail below.
time
In Go, time is represented as type. A type is a structure that contains two fields: a large integer with fields such as century and second and a small integer representing the year of time and the decimal part of time, respectively.
You can use functions in the time package to get the current time, the specified time, or perform the addition and subtraction of time. Here are some commonly used time operation functions:
- (): Return the current local time
- (layout, str): Time to parse the string str into the specified format layout
- (d): Returns the result of the current time plus the given time interval d
- (t, u): Returns the time interval from time t minus time u
date
Date is represented as type or type. Among them, the type represents the complete time, including time, minute and second information; while the type represents only the date part and does not include the time part.
You can use functions in the time package to get the month and number of days of a specified date, or to add or subtract the date. Here are some commonly used date operation functions:
- (month int): Returns the string representation of the specified month
- (): Return the number of days in the current date
- (year int, month int, day int): Returns the result of the specified year, month, day, plus a given time interval
- (t, u): Returns the time interval from time t minus time u
Time zone
Time zone is represented as type. A type is a structure containing a string that represents the time zone identifier and some fields, such as seconds and bias, etc.
You can use functions in the time package to get the offset of the specified time zone or to convert the time zone. Here are some commonly used time zone operation functions:
- : Represents the local time zone
- : Indicates UTC time zone
- (name string): Returns the type object with the specified time zone name
- (loc , t ): Returns the result of converting time t of the specified time zone into the target time zone loc
function
(): Returns the current local time.
(layout, str): Time to parse the string str to the specified format layout.
(d): Returns the result of the current time plus the given time interval d.
(t, u): Returns the time interval from time t minus time u.
(t, u ): Returns the time interval from time t minus time u.
(years int, months int, days int) : Add time according to the increments of year, month, and day.
(t ): Returns the time interval from t to the present.
(u ): Returns the time interval from now to u.
(u): Return true if t is after u, otherwise false.
(u): Return true if t is before u, otherwise false.
(sec int64, nanosec int64): Convert Unix timestamps to time objects.
(str string, layout string) (sec int64, nanosec int64,err error): parses the time string into a Unix timestamp in the specified format.
(sec int64, n int) : Sets the current time to the given number of seconds and nanoseconds.
(d Duration): to allow the program to pause for a period of time during execution.
() int: Returns the integer value of the current hour.
() int: Returns the integer value of the current minute.
() int: Returns the integer value of the current second.
() int: Returns the integer value of the current nanosecond.
() : Returns the enumeration value of the current day of the week.
(): Returns the number of days on the current date.
() : Returns the enum value of the current month.
() int: Returns the integer value of the current year.
(year int, month , day, hour, min, sec, nanosec int, loc *) : Returns the local time object of the specified time.
(year int) bool: determines whether a given year is a leap year.
() int64: Returns the nanosecond value of the current time.
() : Returns the number of nanoseconds from 0:00:00 to the present.
(str string) (, error): parses the interval string into a time interval.
(layout string, value string, loc *) (, error): parse time according to format and time value, and specify the time zone.
(): Returns the start time of the current date.
(): Returns the end time of the current date.
(): Return to the Monday of the week where the current date is.
(): Return to the Sunday of the current date.
(): Returns the start time of the current month.
(): Returns the end time of the current month.
(): Returns the start time of the current year.
(): Returns the end time of the current year.
: Returns the enumeration value of the day of the week, 0 is Sunday, 6 is Saturday, and so on.
(str string) (, error): parses the interval string into a time interval.
() : Returns the time interval of the current hour.
() : Returns the time interval of the current minute.
() : Returns the time interval of the current second.
() : Returns the current nanosecond interval.
() string: Returns the date and time represented by the string corresponding to ANSI C.
(name string, offset int) *: Returns a fixed time zone object.
(name string) (*, error): Load a time zone object.
(t , loc *) : Convert time t to the time of the specified time zone.
type
Time: Represents a time point, including date and time.
Date: represents a date, including year, month, and day.
LocalTime: represents local time.
Location: represents the time zone.
ZeroTime: represents the zero point of time, that is, 00:00:00 UTC on January 1, 1970.
Duration: It represents a time period, which can be used in units of nanoseconds, microseconds, milliseconds, seconds, etc.
Example
Code 1
package main import ( "fmt" "time" ) func main() { // Get the current time currentTime := () ("Current Time:", currentTime) // Pause program execution for 5 seconds ("Waiting for 5 seconds: ") for i := 0; i < 5; i += 1 { () (".") } () // Get the local time of the specified date year := 2023 month := day := 17 hour := 0 minute := 0 second := 0 nano := 0 loc := &{} date := (year, month, day, hour, minute, second, nano, loc) ("Date:", date) // Convert time string to time object layout := "2006-01-02 15:04:05" value := "2023-03-23 08:30:00" parsedTime, err := (layout, value) if err != nil { ("Error:", err) return } ("Parsed Time:", parsedTime) // Format the time object as a string format := "2006-01-02 15:04:05 MST" formattedTime := (format) ("Formatted Time:", formattedTime) // Calculate the time difference between two times t2 := () diff := (parsedTime) ("Time Difference:", diff) // Convert time object to UTC time zone utcTime := () ("UTC Time:", utcTime) // Convert time objects to local time zone localTime := () ("Local Time:", localTime) }
Output:
Current Time: 2023-08-01 20:15:11.0034059 +0800 CST
Waiting for 5 seconds: .....
Date: 2023-03-17 00:00:00 +0000 UTC
Parsed Time: 2023-03-23 08:30:00 +0000 UTC
Formatted Time: 2023-03-23 08:30:00 UTC
Time Difference: 3147h45m16.0065551s
UTC Time: 2023-03-23 08:30:00 +0000 UTC
Local Time: 2023-03-23 16:30:00 +0800 CST
Code 2
package main import ( "fmt" "time" ) func main() { // parse the time string as a time object layout := "2006-01-02 15:04:05" strTime := "2023-03-23 12:30:00" t, err := (layout, strTime) if err != nil { ("Time parsing error:", err) } ("Time after parsing:", t) // Add or subtract time d := (24) * t = (d) ("Add a day:", t) u := (-d) ("Subtract the time of day:", u) // Time interval diff := (u) ("Add a day and minus a day interval:", diff) // Time format output layout = "2006-01-02" t = () formattedTime := (layout) ("Formatted time:", formattedTime) // Convert time to Unix timestamp unixTime := () ("Unix timestamp:", unixTime) // Time comparison t1 := () t2 := () diff = (t1) ("t2 - t1 =", diff) ("t1 < t2:", (t2)) ("t2 > t1:", (t1)) }
Output:
Time after parsing: 2023-03-23 12:30:00 +0000 UTC
Add the time of day: 2023-03-24 12:30:00 +0000 UTC
Subtract the time of day: 2023-03-23 12:30:00 +0000 UTC
Add and subtract the interval between day and day: 24h0m0s
Formatted time: 2023-08-01
Unix timestamp: 1690892235
t2 - t1 = 1h0m0s
t1 < t2: true
t2 > t1: true
Summarize
This article introduces the time library in Go language, including time, date and time zone and other related concepts and usage methods. By mastering this knowledge, we can better deal with time, date and time zone related issues and improve development efficiency.
This is the article about how to organize time and date in the Go Time library. For more related Go Time content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!