func Now() Time
Returns the current system time.
package main import ( "fmt" "time" ) func main() { now := () (()) }
func Parse(layout, value string) (Time, error)
Parses the string to a time type and returns the corresponding Time object. The layout parameter is a format that represents time, for example, "2006-01-02 15:04:05" represents year, month, day, hour, minute, and second format.
package main import ( "fmt" "time" ) func main() { layout := "2006-01-02 15:04:05" str := "2023-07-19 12:34:56" t, _ := (layout, str) (t) }
func ParseDuration(s string) (Duration, error)
Used to parse strings into Duration type.
package main import ( "fmt" "time" ) func main() { durationStr := "2h45m" duration, err := (durationStr) if err != nil { ("Resolve duration error:", err) return } ("The duration after parsing is: %v\n", duration) }
func ParseInLocation(layout, value string, loc *Location) (Time, error)
Used to parse a string into a Time type and parse the time in the specified time zone.
package main import ( "fmt" "time" ) func main() { location := ("CustomZone", 2*60*60) // Create a fixed time zone with an offset of 2 hours timeString := "2023-07-18 14:30:00" // The string to be parsed t, err := ("2006-01-02 15:04:05", timeString, location) if err != nil { ("Parse time error:", err) return } ("The time after parsing is: %v\n", t) }
func Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) Time
Create a Time object based on the given year, month, day, hour, minute, second, and nanoseconds, and the loc parameter represents the time zone.
package main import ( "fmt" "time" ) func main() { t := (2023, 7, 19, 12, 34, 56, 0, ) (t) }
func Since(t Time) Duration
Returns the time difference from the current time to t, in nanoseconds.
package main import ( "fmt" "time" ) func main() { t := (2023, 7, 19, 12, 34, 56, 0, ) duration := (t) (duration) }
func Until(t Time) Duration
Returns the time difference from t to the current time, in nanoseconds.
package main import ( "fmt" "time" ) func main() { t := (2023, 7, 19, 12, 34, 56, 0, ) duration := (t) (duration) }
func After(d Duration) <-chan Time
Used to create a channel that will send a value of type to the channel after the specified time interval.
package main import ( "fmt" "time" ) func main() { // Create a channel that will send values after 5 seconds timer := (5 * ) // Wait for channel to send value (<-timer) }
func AfterFunc(d Duration, f func()) *Timer
Used to create a timer and execute a function after the specified time interval.
package main import ( "fmt" "time" ) func main() { // Create a timer that will be triggered after 5 seconds timer := (5*, func() { ("Lu Duosin's blog!") }) // Stop the timer (10 * ) () // Output: Ludoshin's blog! ("Program ended") }
func Unix(sec int64, nsec int64) Time
Used to convert Unix timestamps to time type.
package main import ( "fmt" "time" ) func main() { // Define a Unix timestamp unixTime := int64(1691587957) // Use function to convert Unix timestamps to time type t := (unixTime, 0) // Print the result (t) }
func UnixMilli(msec int64) Time
Used to convert millisecond timestamps to time type.
package main import ( "fmt" "time" ) func main() { t := (1691587957000) // Print the result (t) }
func UnixMicro(usec int64) Time
Used to convert microsecond timestamps to time type.
package main import ( "fmt" "time" ) func main() { t := (1691587957000000) // Print the result (t) }
func Tick(d Duration) <-chan Time
Used to create a timer that sends the current time to the channel at a certain time interval.
package main import ( "fmt" "time" ) func main() { // Create a timer that prints every 1 second ticker := (1 * ) for { <-ticker // Wait for the timer to be sent ("This is a ticker print") } }
func LoadLocation(name string) (*Location, error)
Used to load and return a specific time zone.
package main import ( "fmt" "time" ) func main() { // Load "Asia/Shanghai" time zone location, err := ("Asia/Shanghai") if err != nil { ("Unable to load time zone:", err) return } // Create a time object using this time zone t := ().In(location) ("Current time:", t) }
func LoadLocationFromTZData(name string, data []byte) (*Location, error)
Used to load from TZ data and return a time zone.
package main import ( "fmt" "time" ) func main() { // Load "Asia/Shanghai" time zone location, err := ("Asia/Shanghai") if err != nil { ("Unable to load time zone:", err) return } // Create a time object using this time zone t := ().In(location) ("Current time:", t) }
func FixedZone(name string, offset int) *Location
Used to create a time zone with a fixed offset.
package main import ( "fmt" "time" ) func main() { // Create a fixed time zone with an offset of 2 hours location := ("CustomZone", 2*60*60) // Create a time object using this time zone t := ().In(location) ("Current time:", t) }
func NewTicker(d Duration) *Ticker
Used to create a new Ticker object that can repeatedly send Time values at specified time intervals.
package main import ( "fmt" "time" ) func main() { // Create a Ticker object sent once a second ticker := (1 * ) defer () for { select { case <-: ("Print Ludoshin's Blog") } } }
func NewTimer(d Duration) *Timer
Used to create a new Timer object that can send a value of type Time after a specified time interval.
package main import ( "fmt" "time" ) func main() { // Create a timer and send a time value after 2 seconds timer := (2 * ) defer () // Time value processing function sent using timer timerHandler := func() { ("Time value received") } // Execute the processing function before the timer sends the time value go timerHandler() // Wait for the timer to send the time value select { case <-: ("Timer sent time value") } }
func Sleep(d Duration)
Let the current goroutine sleep for a specified time.
package main import ( "fmt" "time" ) func main() { ("Start sleep") (2 * ) ("End of sleep") }
The above is the detailed explanation of the functions in the Golang time package. For more information about the functions of the Golang time package, please pay attention to my other related articles!