Skip to content

Instantly share code, notes, and snippets.

@CodeSpoof
Last active January 26, 2025 20:56
Show Gist options
  • Save CodeSpoof/a61c3f420d5265d508ef068534aa0907 to your computer and use it in GitHub Desktop.
Save CodeSpoof/a61c3f420d5265d508ef068534aa0907 to your computer and use it in GitHub Desktop.
Python exec-block correct time with NTP
# Fetches the current time from time.nist.gov and overrides time.time to correct it with a delta value
# Don't use this, if your time deviation is within less than 10 ms.
# Network delay will often cause worse accuracy than what you OS provides.
# This code will help, when your OS has a REALLY low accuracy and you can't just fix your OS's clock.
# If you have code that needs to work without further user-interaction and needs reasonably good time, this is for you.
# Fix wrong time from OS with NTP (time.nist.gov)
exec("import contextlib as f,socket as e,struct as d,time as c\ndef g():\n\twith f.closing(e.socket(e.AF_INET,e.SOCK_DGRAM))as s:s.settimeout(5);s.sendto(b'\\x1b'+47*b'\\x00',('time.nist.gov',123));i,_=s.recvfrom(1024)\n\th=d.unpack('!12I',i[0:48]);return h[10]+float(h[11])/2**32-2208988800\nc.b=c.time;exec('def a():return c.b()+'+str(g()-c.time()));c.time=a;del g;del a;del f;del e;del d")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment