Files
truecharts/clustertool/pkg/helper/time.go
T
2024-10-19 15:18:05 +02:00

41 lines
1.2 KiB
Go

package helper
import (
"os"
"time"
"github.com/rs/zerolog/log"
"github.com/beevik/ntp"
)
// checkSystemTime compares the system time with an NTP server time and returns whether it's correct within the given threshold
func CheckSystemTime() bool {
log.Info().Msg("Checking if System Time is correct...")
threshold := 5 * time.Second
// Get the current system time
systemTime := time.Now()
// Get the time from an NTP server
ntpTime, err := ntp.Time("pool.ntp.org")
if err != nil {
log.Info().Msgf("Failed to get NTP time: %v", err)
return true
}
// Calculate the difference between system time and NTP time
timeDifference := systemTime.Sub(ntpTime)
// Check if the time difference is within the acceptable threshold
if timeDifference > -threshold && timeDifference < threshold {
log.Info().Msg("System Time is correct...")
} else {
log.Info().Msg("ERROR: System Time incorrect, please correct your systemtime:")
log.Info().Msgf("System time: %v", systemTime)
log.Info().Msgf("NTP time: %v", ntpTime)
log.Info().Msgf("Aborting command!", )
os.Exit(1)
}
return timeDifference > -threshold && timeDifference < threshold
}