在 Go 语言中,时间包提供了确定和查看时间的功能。 Go 语言中的Time.UnixNano()函数用于生成“t”作为 Unix 时间,即从 1970 年 1 月 1 日起经过的秒数,以 UTC 表示,此处的输出不依赖于与 t 连接的位置.而且,这个函数是在time包下定义的。在这里,您需要导入“time”包才能使用这些功能。
Syntax:
Here, “t” is the stated time.
Note: Here, the output returned is not defined if the given Unix time in nanoseconds is not formed by an int64 type(which is a date before the year 1678 or after the year 2262). This implies that the result of calling the UnixNano() method on the zero Time is ambiguous.
Return value: It returns “t” as a Unix time which is of type int64.
示例 1:
func (t Time) UnixNano() int64
输出:
// Golang program to illustrate the usage of
// Time.UnixNano() function
// Including main package
package main
// Importing fmt and time
import "fmt"
import "time"
// Calling main
func main() {
// Defining t in UTC
// for UnixNano method
t := time.Date(2019, 13, 15, 23,
90, 12, 04, time.UTC)
// Calling UnixNano method
unixnano := t.UnixNano()
// Prints output
fmt.Printf("%v\n", unixnano)
}
示例 2:
1579134612000000004
输出:
// Golang program to illustrate the usage of
// Time.UnixNano() function
// Including main package
package main
// Importing fmt and time
import "fmt"
import "time"
// Calling main
func main() {
// Defining t in UTC
// for UnixNano method
t := time.Date(2001, 13, 15, 2e3,
1e1, 12e2, 04e1, time.UTC)
// Calling UnixNano method
unixnano := t.UnixNano()
// Prints output
fmt.Printf("%v\n", unixnano)
}
这里,上述代码中的时间“t”的值包含常数“e”,但它们在转换时在通常的范围内转换。