date hack

The computers at our institute don’t have NTP installed, and the time
is rarely synchronized. The date on the computer I am writing this post
on is about half an hour behind. So here’s how you can get the correct
date from the terminal using wget, sed and the good ol’ date:


#!/bin/sh
# Prints the date.

x="$(wget -q -O - http://tycho.usno.navy.mil/cgi-bin/timer.pl | grep UTC | cut -c 5- | sed s/\,//g)"
date -d "${x}"

Put this file somewhere in your $PATH, make it executable and run it. It’ll print the
date in the local timezone. Here’s how it works:

  1. It gets the date from http://tycho.usno.navy.mil/cgi-bin/timer.pl and dumps it on the
    terminal.
  2. It gets the first line in UTC using grep
  3. It cuts out the
    .
  4. and removes the comma which causes date to choke
  5. Finally date -d converts the date into the local timezone and displays it.

I’ve this script running with watch -n 5 to give the date every five seconds in the terminal.

Advertisement

One thought on “date hack

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s