在 Go 语言中,时间包提供了确定和查看时间的功能。 Go 语言中的Time.Unix()函数用于生成“t”作为 Unix 时间,即从 1970 年 1 月 1 日起经过的秒数,以 UTC 表示,此处的输出不依赖于与 t 连接的位置.而且,这个函数是在time包下定义的。在这里,您需要导入“time”包才能使用这些功能。
Syntax:
func (t Time) Unix() int64
Here, “t” is the stated time.
Note: A Unix like operating system frequently stores time as a 32-bit count of seconds. And on the other hand, the Unix() method here returns a 64-bit value so it’s period of validity is for billions of years into the past or the future.
Return value: It returns “t” as a Unix time which is of type int64.
Example 1:
// Golang program to illustrate the usage of
// Time.Unix() function
// Including main package
package main
// Importing fmt and time
import "fmt"
import "time"
// Calling main
func main() {
// Defining t in UTC for Unix method
t := time.Date(2020, 11, 14, 11, 30, 32, 0, time.UTC)
// Calling Unix method
unix := t.Unix()
// Prints output
fmt.Printf("%v\n", unix)
}
Output:
1605353432
Example 2:
// Golang program to illustrate the usage of
// Time.Unix() function
// Including main package
package main
// Importing fmt and time
import "fmt"
import "time"
// Calling main
func main() {
// Defining t in UTC for Unix method
t := time.Date(2013, 11, 14, 1e3, 3e5, 7e1, 0, time.UTC)
// Calling Unix method
unix := t.Unix()
// Prints output
fmt.Printf("%v\n", unix)
}
Output:
1405987270
Here, the time “t” stated in the above code has values which contain constant “e” which are converted in usual range while conversion.