Skip to content

Instantly share code, notes, and snippets.

@Esonhugh
Created June 13, 2025 20:10
Show Gist options
  • Save Esonhugh/3868a3a3fad0bf0c055939dceecca9c6 to your computer and use it in GitHub Desktop.
Save Esonhugh/3868a3a3fad0bf0c055939dceecca9c6 to your computer and use it in GitHub Desktop.
ntplib based time checker
# /// script
# requires-python = ">=3.10"
# dependencies = [
# "ntplib",
# ]
# ///
import time,ntplib
from datetime import datetime
import sys
def main() -> None:
if len(sys.argv) < 2:
print("Usage: ntp_check.py [server]")
sys.exit(1)
server = sys.argv[1]
ntp_client = ntplib.NTPClient()
current = datetime.now().timestamp()
response = ntp_client.request(server)
time_str = response.tx_time
ntp_date = time.strftime('%Y-%m-%d',time.localtime(time_str))
ntp_time = time.strftime('%X',time.localtime(time_str))
date = time.strftime('%Y-%m-%d',time.localtime(current))
c_time = time.strftime('%X',time.localtime(current))
print("Current system time:", date, c_time)
print("NTP server time:", ntp_date, ntp_time)
offset = response.offset
print("diff: {:.3f} mintues".format(offset))
# os.system('date {} && time {}'.format(ntp_date,ntp_time))
print("try faketime with")
if offset > 0:
offset = "+" + str(offset)
print("faketime -f '{}' command ".format(offset))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment