Skip to content

Instantly share code, notes, and snippets.

View CodeSpoof's full-sized avatar
🔬
Analyzing some weird software bug

CodeSpoof CodeSpoof

🔬
Analyzing some weird software bug
View GitHub Profile
@CodeSpoof
CodeSpoof / ntp.py
Last active January 26, 2025 20:56
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")

Keybase proof

I hereby claim:

  • I am CodeSpoof on github.
  • I am codespoof (https://keybase.io/codespoof) on keybase.
  • I have a public key whose fingerprint is 172E FEAF F506 740C 910B 5882 4E50 0589 5C38 6DF4

To claim this, I am signing this object: