ADVERTISEMENT
ADVERTISEMENT

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 »



ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.