Home »
Golang »
Golang Programs
How to get the number of seconds since the epoch using syscall in Golang?
Here, we will learn to get the number of seconds since the epoch using syscall in Golang.
Submitted by IncludeHelp, on November 13, 2021
In the Go programming language, to get the number of seconds since the epoch using syscall – we use the Gettimeofday() function of the syscall package. The Gettimeofday() function returns the number of seconds and microseconds since the epoch.
Syntax:
func Gettimeofday(tv *Timeval) (err error)
Consider the below example demonstrating how to get the number of seconds since the epoch using syscall in Golang?
package main
import (
"fmt"
"syscall"
"time"
)
func main() {
var TimeValue syscall.Timeval
if err := syscall.Gettimeofday(&TimeValue); err != nil {
fmt.Printf("Error: %v", err)
}
// Printing the seconds & micro seconds
fmt.Println("Seconds: ", TimeValue.Sec)
fmt.Println("Micro: ", TimeValue.Usec)
// Calculate & Print the Current Date & Time
// Using the seconds & micro seconds
fmt.Println(time.Unix(TimeValue.Sec, TimeValue.Usec))
}
Output
Seconds: 1636704620
Micro: 779023
2021-11-12 08:10:20.000779023 +0000 UTC
Golang syscall Package Programs »