The Epoch

In Unix and Linux, dates are always represented internally as the number of seconds since January 1, 1970 at 00:00 Greenwich Mean Time (GMT, nowadays UTC, or Coordinated Universal Time ). This original date is sometimes called ʻThe Epoch’. In some situations in shell scripts you have to convert the Unix time into a normal date and vice versa.

Converting dates

A conversion can go in either direction with the date command. With the option -d you specify the date to be displayed (otherwise date will display the current time). An epoch time is simply written as a decimal number preceeded by an @. For example, the 1234567890th second since the epoch occurred in February 2009:

user@linux> date -d @1234567890
Sa 14. Feb 00:31:30 CET 2009

Conversely, you can convert a date into a Unix timestamp by using a +%s as the format specification for date. Thus you will get the current time, for example:

user@linux> date +%s

To convert a particular time, you need to specify it again with -d. Different representations are allowed. For the conversion of 18.12.2008 at 12:34 you specify:

user@linux> date -d '2008-12-18 12:34:00' +%s
1229600040

Don’t forget that the time stamp is always calculated in UTC – as opposed to the written date, which is to be understood relative to your time zone. That is why you get -3600 seconds in Germany when you convert the 1.1.1970 into seconds:

user@linux> date -d '1970-01-01 00:00:00' +%s
-3600

Why? When it was 00:00 on 1.1.1970 in Germany, it was 23:00 the day before in the UK (at Greenwich, in London).

Linux knowledge

These articles were written by the founder of Checkmk many years ago.
They are still valid though and thus we keep them on our website.
Mathias has since then developed a monitoring software called Checkmk

Find out more