Skip to content

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

@FSOL-XDAG

Description

@FSOL-XDAG

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions