css: set PRE’s max-width so it doesn’t stretch the viewport
[mina86.com.git] / posts / ntp-over-http.en.html
blob4c5d8cf7e694a44c5e1121f2b9873708ca21185b
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
14 there was…
16 <!-- FULL -->
18 <p>If you recall that web servers include their time in response headers
19 solution becomes clear.
21 <pre>
22 httpdate="$(wget --no-cache -S -O /dev/null google.com 2&gt;&amp;1 |
23 sed -n -e 's/ *Date: *//p' -eT -eq)"
24 [ -n "$httpdate" ] && date -s "$httpdate"</pre>
26 <p>Did the trick!
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">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).
40 <!-- COMMENT -->
41 <!-- date: 2012-07-05 15:05:17 -->
42 <!-- nick: Anonim -->
44 <p>Improved version to ignore proxy cache servers:
46 <pre>
47 date -s "$(wget --no-cache -S -O /dev/null google.com 2&gt;&amp;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:
56 <pre>
57 http_proxy=http://user:password@192.168.1.5:3128
58 export http_proxy
59 date -s "$(wget --no-cache -S -O /dev/null google.com 2&gt;&amp;1 | \
60 sed -n -e '/ *Date: */ {' -e s///p -e q -e '}')"</pre>
62 <!-- COMMENT -->
63 <!-- date: 2012-07-05 22:34:06 -->
64 <!-- nick: mina86 -->
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:
68 <pre>
69 date -s "$(
70 http_proxy=http://youruser:yourpassword@proxyip:proxyport \
71 wget --no-cache -S -O /dev/null google.com 2&gt;&amp;1 | \
72 sed -n -e '/ *Date: */ {' -ep -eq -e '}')"</pre>
74 <!-- COMMENT -->
75 <!-- date: 2012-08-17 11:23:52 -->
76 <!-- nick: almaz -->
78 <p>If no connection is reset time at 00:00:00
80 <!-- COMMENT -->
81 <!-- date: 2013-05-14 21:21:08 -->
82 <!-- nick: mina86 -->
83 <!-- nick_url: http://mina86.com -->
85 <blockquote>
86 <p>If no connection is reset time at 00:00:00.
87 </blockquote>
89 <p>True. To solve that one would have to check the output. I’ll update
90 the entry.