Get back to “Jump to”

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 ntpdate but before finishing I realised that our beloved IT 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 almaz for 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 -s switch) (but implementing simple RFC1123 date parser is 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). :)

Comments Atom feed with comments

Get back to “Jump to”


  • Added at2012/07/05, 15:05

(Komentarz zmodyfikowany 05.07.2012 o 22:31)

Improved version to ignore proxy cache servers:

date -s "$(wget --no-cache -S -O /dev/null google.com 2>&1 | \
    sed -n -e '/  *Date: */ {' -e s///p -e q -e '}')"

And set http_proxy env. variable if you have a proxy server in your way, as:

http_proxy=http://youruser:yourpassword@proxyip:proxyport;export http_proxy

For instance the whole thing could be:

export http_proxy
date -s "$(wget --no-cache -S -O /dev/null google.com 2>&1 | \
    sed -n -e '/  *Date: */ {' -e s///p -e q -e '}')"


  • Added at2012/07/05, 22:34

Looks good, thanks, even thought I'd get rid of the export not to pollute the environment as so:

date -s "$(
    http_proxy=http://youruser:yourpassword@proxyip:proxyport \
    wget --no-cache -S -O /dev/null google.com 2>&1 | \
    sed -n -e '/  *Date: */ {' -ep -eq -e '}')"


  • Added at2012/08/17, 11:23

If no connection is reset time at 00:00:00


  • Added at2013/05/14, 21:21

If no connection is reset time at 00:00:00.

True. To solve that one would have to check the output. I'll update the entry.

Due to excessive spam, guest comments has been disabled on this entry. If you have Jogger account, please log in. Otherwise, you can always mail the comment to mina86@mina86.com or send it via xmpp to mina86@jabber.org. Sorry for the inconvenience.

End of the page, get back to “Jump to”.