Last active
January 26, 2025 20:56
-
-
Save CodeSpoof/a61c3f420d5265d508ef068534aa0907 to your computer and use it in GitHub Desktop.
Python exec-block correct time with NTP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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