Skip to content

Instantly share code, notes, and snippets.

@dungsaga
Last active December 14, 2022 22:15
Show Gist options
  • Save dungsaga/216eccd9129edf7260d561c82477e6b6 to your computer and use it in GitHub Desktop.
Save dungsaga/216eccd9129edf7260d561c82477e6b6 to your computer and use it in GitHub Desktop.
sync local clock in without NTP in Linux, MacOS, Windows
# I want to sync the local clock but don't have access to any NTP server.
# Website https://time.is tells me the time difference of my system clock and I can manually adjust the local clock.
# But then I recall that a HTTP response often contains a date header with current time in GMT timezone.
# I'll use it to set the system clock with the precision of 1 or 2 seconds.
# When I need more accurate time, I can use `datetime` (or `utc_datetime`) from `http://worldtimeapi.org/api/ip.txt`
# time from worldtimeapi.org has millisecond precision, you can use it to compare with your system time:
date -uIns && time curl -s http://worldtimeapi.org/api/ip.txt | sed -n 's/^utc_datetime://p'
date -Ins && time curl -s http://worldtimeapi.org/api/ip.txt | sed -n 's/^datetime://p'
# Append `tee` if you want to see the output from `cut`
# NOTE: You should execute a few times to see which one has smallest execution time.
# in Linux
sudo date -us "$(time curl -Is 1.1 | sed -n 's/^Date://p')"
sudo date -us "$(time curl -Is 1.1 | sed -n 's/^Date://p' | tee /dev/tty)"
sudo date -us "$(time curl -s http://worldtimeapi.org/api/ip.txt | sed -n 's/^utc_datetime://p')"
sudo date -us "$(time curl -s http://worldtimeapi.org/api/ip.txt | sed -n 's/^utc_datetime://p' | tee /dev/tty)"
# in Linux with fish shell
sudo date -us (time curl -Is 1.1 | sed -n 's/^Date://p')
sudo date -us (time curl -Is 1.1 | sed -n 's/^Date://p' | tee /dev/tty)
sudo date -us (time curl -s http://worldtimeapi.org/api/ip.txt | sed -n 's/^utc_datetime://p')
sudo date -us (time curl -s http://worldtimeapi.org/api/ip.txt | sed -n 's/^utc_datetime://p' | tee /dev/tty)
# in MacOS
sudo date -uf "%d %b %Y %T" "$(time curl -Is 1.1 | sed -n 's/Date:\(.*\) GMT/\1/p')"
# in MacOS with fish shell
sudo date -uf "%d %b %Y %T" (time curl -Is 1.1 | sed -n 's/Date:\(.*\) GMT/\1/p')
# If you don't have `curl`, you can use `wget`
time wget -Sq --max-redirect=0 1.1 -o- | sed -n 's/^ Date://p'
# in Windows PowerShell
(cmd /c curl -Is 1.1 | findstr "^Date:").replace("Date:", "") | Get-Date | Set-Date
# in Windows cmd (must use local time, not GMT time)
for /f "delims=" %i in ('curl -s http://worldtimeapi.org/api/ip.txt ^| findstr "^datetime:"') do set DateLine=%i && time %DateLine:~21,11%
# You can use another website instead of 1.1 (if it returns a date header with UTC time).
# WARNING: be careful, some websites have a date header with time in the past.
# Use "time" to measure which one is faster for you.
time curl -Is 1.1
time curl -Is 1.2
# Short domains: https://en.wikipedia.org/wiki/Single-letter_second-level_domain
time curl -Is g.cn
time curl -Is g.co
time curl -Is a.co
time curl -Is t.co
time curl -Is s.co
time curl -Is c.cc
time curl -Is g.gg
time curl -Is t.tt
time curl -Is db.tt
time curl -Is bb.bb
time curl -Is ee.ee
time curl -Is m.me
time curl -Is v.me
time curl -Is t.me
time curl -Is yt.be
time curl -Is x.org
time curl -Is x.com
time curl -Is z.com
time curl -Is fb.com
time curl -Is w.wiki
# Some funny domains from Google https://web.archive.org/web/20170615200243/https://en.wikipedia.org/wiki/List_of_Google_domains
time curl -Is g.gl
time curl -Is goo.gl
time curl -Is gogle.com
time curl -Is gogole.com
time curl -Is gooogle.com
time curl -Is googl.com
time curl -Is googlr.com
time curl -Is googlee.com
time curl -Is ggoogle.com
time curl -Is goolge.com
time curl -Is googel.com
time curl -Is googil.com
time curl -Is igoogle.com
time curl -Is froogle.com
time curl -Is foofle.com
time curl -Is com.google
time curl -Is elgoog.im
time curl -Is 466453.com
time curl -Is abc.xyz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment