Home > Backend Development > Golang > golang seconds to time

golang seconds to time

WBOY
Release: 2023-05-13 11:33:36
Original
857 people have browsed it

Go language (Golang) is an open source, cross-platform programming language. Its biggest feature is its efficiency, simplicity and rapid iteration capabilities. The design of the time processing function is also very powerful, especially converting seconds into time, which is very simple and practical. This article will explain how to convert seconds to time in Golang.

In the Go language, time processing mainly relies on the time package, which provides a large number of time processing functions and types. Among them, the time.Duration type represents the time interval in nanoseconds; the time.Time type represents the time point, including year, month, day, hour, minute, second, nanosecond and other information.

To convert from seconds to time, you need to use the time.Unix() method. Its function prototype is as follows:

func Unix(sec int64, nsec int64) Time

where , sec represents the number of seconds, and nsec represents the number of nanoseconds. This method converts seconds and nanoseconds to time. Seconds are offsets relative to Unix time (1970-01-01 00:00:00 UTC). When calling this method, you need to provide the number of seconds and nanoseconds, where the nanoseconds can be ignored (just set to 0).

The following is a complete sample code:

package main

import (
    "fmt"
    "time"
)

func main() {
    sec := int64(1631149512)
    t := time.Unix(sec, 0)

    fmt.Println(t)
    fmt.Println(t.Year())
    fmt.Println(t.Month())
    fmt.Println(t.Day())
    fmt.Println(t.Hour())
    fmt.Println(t.Minute())
    fmt.Println(t.Second())
}
Copy after login

This code converts the number of seconds (1631149512) into time, and then prints the year, month, day, hour, minute, second and other information. The output result is as follows:

2021-09-09 07:18:32 +0000 UTC
2021
September
9
7
18
32
Copy after login

As can be seen from the above example, converting seconds to time is very simple, just call the time.Unix() method. Then, you can use various methods of the time.Time type to obtain various parts of the time point.

To summarize, this article introduces the method of converting seconds to time in Golang. Use the time.Unix() method to convert seconds and nanoseconds into time, where nanoseconds can be ignored. For various operations on time, you can use various methods of the time.Time type. Golang's time processing is very powerful, and its expression is also very simple. It is worth learning and using by the majority of developers.

The above is the detailed content of golang seconds to time. 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