Trouble with Time

By this point, everyone has realised that my posts are going to be about Linux and Programming, no matter what the title is. But a man can try to come up with a nice title.

I am not all about Linux and Programming though. For instance, I love to work with hardware and electronics. I also like to combine them both, add a bit of linux and programming, and voila, I have a Robot.

The story starts with one such endeavour, where I was using a Raspberry Pi 3.

#ThanksCBS

Raspberry Pi does not have a hardware clock — a tiny circuit that maintains time even when your computer is shut down. Thus, it uses the network to maintain time.

Digress — Time Standards

Maintaining a standard of time across the globe is important. If, for some reason, International Time Standards magically disappear, there will be chaos all around us. For example, Time Standards are critical for Stock Markets, GPS and even the Web.

The time standard that all of use today — UTC (Coordinated Universal Time) is maintained using a network of Atomic Clocks. Atomic Clocks use the ISO definition of second — something about caesium atom radiations.

How do laymen like us synchronise with these Atomic Clocks?

Enter NTP

NTP (Network Time Protocol) is a networking protocol for clock synchronisation.

NTP has a hierarchical system of sources. The Time Servers on the top layer are directly connected to high precision clocks, such as atomic clocks, and are called Stratum 0 time servers.

The servers that use Stratum 0 servers to synchronise time are themselves Stratum 1 time servers. They are within a few microseconds of the Stratum 0 server that they are synchronising with. This goes on up to Stratum 15. Stratum 16 is considered to be un-synchronised.

For those interested on how NTP does its job — check this out.

Me and My Pi

My problem with Raspberry Pi was simple. In my college, IIT Guwahati, I am behind a Firewall that blocks everything except HTTP(S).

Thus, I cannot use Public NTP servers. I asked the Computer Centre to set up a NTP server, outside of the firewall, for the use of IITG community.

IITG does have a NTP server — but for reasons, they cannot be used in hostels. And they were valid reasons, so I could not argue with our dear Network Administrators either.

I needed a solution, and I needed one badly. So I did what any programmer would do. Close my laptop and go have a coffee.

I was thinking of searching on google for “NTP Alternatives”. There must be some guy who had the same problem and had posted a question on stackoverflow.

Google and Stackoverflow 
HTTP
Not blocked by IITG firewall
Can I ask Google for Time?
Wait a second — HTTP Headers have time, as far as I remember

Throw away the empty cup. I need to curl. Get the joke — Its similar to “I need to hurl”. Never mind — all the network admins are going bonkers over that one. Anyways…,

curl -I https://www.google.co.in

And there, in the second line of the output, below HTTP/1.1 200 OK was,

Date: Thu, 19 Oct 2017 14:27:24 GMT

Oh Yeahhhh… 200 OK it is…

All I need to do is write a small script that will cut out the second line from the output of curl and set it as system time.

Wait a second. Let me google if somebody did that already. I open up chrome and search for “http time synchronization”.

The top result was this beaut— htpdate

From there, It took me 30 minutes to setup time synchronisation on the Pi.

Micro guide on time syncing using htpdate

  1. This is for IITG — For outside, replace the http server with a webserver close to you.
  2. This is for debian based distros — Ubuntu, Raspbian etc. This is because, I use apt-get for installing htpdate.
  3. This guide uses cron to sync time every minute. Instead, you might want to set time during boot and leave it until next boot. For that, check out cron’s documentation. PS — I love arch wiki.

Conclusion(s)

Learnt about NTP and how time is maintained across the globe.

Learnt about htpdate — the poor man’s NTP.

Got the time setup properly on the Pi.

What more can a man ask for? — Only more things to learn.