1 <!-- subject: {NTP} over {HTTP} -->
2 <!-- date: 2010-01-16 01:27:45 -->
3 <!-- tags: ntp over http, ntp http proxy, time synchronisation, ntp, http -->
4 <!-- categories: Articles, Techblog -->
6 <p>Sitting in a dark office, after swearing for hours at ATI video cards
7 I noticed time on my computer was incorrect. ‘No problem,’ I thought as
8 I started typing
<code>ntpdate
</code>. That’s where it struck me that our
9 <em>beloved
</em> IT department had blocked most of the Internet. Checking the
10 time on a watch or a mobile phone was not an option — I had neither — nor was
11 looking at GKrellM on another PC — that’s lame.
13 <p>‘I wish there was a NTP-over-HTTP protocol’ I sighed. And then I realised
18 <p>If you recall that web servers include their time in response headers
19 solution becomes clear.
22 httpdate=
"$(wget --no-cache -S -O /dev/null google.com 2>&1 |
23 sed -n -e 's/ *Date: *//p' -eT -eq)"
24 [ -n
"$httpdate" ] && date -s
"$httpdate"</pre>
28 <p>It won’t guarantee sub-second accuracy, but will usually work to withing
29 a few seconds. It also requites GNU coreutils
30 (for
<code>date
</code>’s
<code>-s
</code> switch) (but
31 implementing
<a href=
"https://gist.github.com/278534">simple RFC1123 date
32 parser
</a> is not that hard), wget (which can however be replaced by other
33 tools including telnet) and sed (which is POSIX utility and also can be
34 replaced by
<a href=
"https://gist.github.com/278539">a
13-line C program
</a>).
36 <p>Implementing a complete tool in C, C++, Rust or similar is left as a simple
37 exercise for the reader. (Less ambitious reader may limit themselves to some
38 scripting language with regexes).
41 <!-- date: 2012-07-05 15:05:17 -->
44 <p>Improved version to ignore proxy cache servers:
47 date -s
"$(wget --no-cache -S -O /dev/null google.com 2>&1 | \
48 sed -n -e '/ *Date: */ {' -e s///p -e q -e '}')"</pre>
50 <p>And set http_proxy env. variable if you have a proxy server in your way, as:
52 <pre>http_proxy=http://youruser:yourpassword@proxyip:proxyport;export http_proxy
</pre>
54 <p>For instance the whole thing could be:
57 http_proxy=http://user:password@
192.168.1.5:
3128
59 date -s
"$(wget --no-cache -S -O /dev/null google.com 2>&1 | \
60 sed -n -e '/ *Date: */ {' -e s///p -e q -e '}')"</pre>
63 <!-- date: 2012-07-05 22:34:06 -->
65 <!-- nick_url: http://mina86.com -->
67 <p>Looks good, thanks, even thought I'd get rid of the export not to pollute the environment as so:
70 http_proxy=http://youruser:yourpassword@proxyip:proxyport \
71 wget --no-cache -S -O /dev/null google.com 2>&1 | \
72 sed -n -e '/ *Date: */ {' -ep -eq -e '}')"</pre>
75 <!-- date: 2012-08-17 11:23:52 -->
78 <p>If no connection is reset time at
00:
00:
00
81 <!-- date: 2013-05-14 21:21:08 -->
83 <!-- nick_url: http://mina86.com -->
86 <p>If no connection is reset time at
00:
00:
00.
89 <p>True. To solve that one would have to check the output. I’ll update