Open In App

time.Time.UnixNano() Function in Golang with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

In Go language, time packages supply functionality for determining as well as viewing time. The Time.UnixNano() function in Go language is used to yield “t” as a Unix time that is the number of seconds passed from January 1, 1970, in UTC and the output here doesn’t rely upon the location connected with t. Moreover, this function is defined under the time package. Here, you need to import the “time” package in order to use these functions.
 

Syntax: 

func (t Time) UnixNano() int64

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. 

Example 1:

Go




// 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)
}


Output: 

1579134612000000004

Example 2:

Go




// 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)
}


Output: 

input
1018254600000000040

Here, the time “t” stated in the above code has values which contain constant “e” but they are converted in usual range while conversion.



Last Updated : 24 Aug, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads