Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggest - Compare local time with NTP time before any transfert #77

Open
FSOL-XDAG opened this issue May 15, 2023 · 0 comments
Open

Suggest - Compare local time with NTP time before any transfert #77

FSOL-XDAG opened this issue May 15, 2023 · 0 comments

Comments

@FSOL-XDAG
Copy link

Regularly, internal clock synchronization problems block transfers.

There should be a way to check if the time on the computer is the same as the NTP time before any tx, and warn user if time isn't sync.

Here is an example of a check proposed by ChatGPT:

package main

import (
	"fmt"
	"net"
	"time"

	"github.com/beevik/ntp"
)

func compareTimeWithNTPServer(ntpServer string) (time.Duration, time.Duration, error) {
	startTime := time.Now()

	// Get the current time from the NTP server
	ntpTime, err := ntp.Time(ntpServer)
	if err != nil {
		return 0, 0, err
	}

	endTime := time.Now()

	// Calculate the round-trip request time
	requestTime := endTime.Sub(startTime)

	// Calculate the difference between system time and NTP server time
	timeDifference := ntpTime.Sub(endTime)

	return requestTime, timeDifference, nil
}

func main() {
	ntpServer := "pool.ntp.org"

	requestTime, timeDifference, err := compareTimeWithNTPServer(ntpServer)
	if err != nil {
		fmt.Println("Error comparing clocks:", err)
		return
	}

	fmt.Println("Request Time:", requestTime)
	fmt.Println("Difference between system time and NTP server time:", timeDifference)
}

In this example, we use the github.com/beevik/ntp library to interact with the NTP server and obtain the current time. Make sure to install this library by running the command go get github.com/beevik/ntp before running the code.

The compareTimeWithNTPServer function takes the NTP server name as a parameter and returns the round-trip request time (requestTime), the difference between system time and NTP server time (timeDifference), and any potential errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant