×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

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 [Last updated : March 05, 2023]

Getting the number of seconds since the epoch using syscall in Golang

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?

Golang code to get the number of seconds since the epoch using syscall

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 »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.