Q:

How to get the number of seconds since the epoch using syscall in Golang?

belongs to collection: Golang syscall Package Programs

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

How to make directories using syscall in Golang?... >>
<< How to get the working directory using syscall in ...