Sitting in a dark office, after swearing for hours at ATI video cards I noticed time on my PC was incorrect. “No problem” I thought as I started typing ntpdatebut before finishing I realised that our belovedIT department had blocked most of the Internet. Checking the time on a watch or a mobile phone was not an option – I have neither – nor was looking at GKrellM on another PC – that's just lame.
“I wish there was a NTP-over-HTTP protocol” I dreamed sighting. And then, a few curses on the IT department later, I came up with an idea…
If you recall that web servers return their time in response headers solution becomes clear.
httpdate="$(wget --no-cache -S -O /dev/null google.com 2>&1 | sed -n -e 's/ *Date: *//p' -eT -eq)" [ -n "$httpdate" ] && date -s "$httpdate"
Did the trick! (update: Thanks almazfor pointing out problems when connection is unsuccessful).
This has some disadvantages, granted, but works great for situations where millisecond accuracy is not required which is the case on most personal computers – do you really care if it's 10 or 11 seconds after the minute?
It also requites GNU coreutils (for date's -sswitch) (but implementing simple RFC1123 date parseris not that hard), wget (which can however be replaced by other tools including telnet) and sed (which is POSIX utility and also can be replaced by a 13-line C program).
Therefore implementing a complete tool written in C is left as a simple exercise for reader (less ambitious reader may limit themselves to some scripting language with regexpes). :)