Why does Unix have its own concept of time? What is the epoch and what’s the Y2038 problem?
Unix time is a means of representing a specific date and time, used by Linux, macOS, and many other interoperable systems. It’s so widespread, that you’re probably using it without being aware of it. Once you understand Unix time, though, you’ll spot it in many contexts. Several tools can help you to work with Unix time.
What Is the Purpose of Unix Time?
Unix time is a count of total seconds since a fixed time and date. It’s a date/time (or timestamp) format that looks different from the human-readable dates and times we’re used to. This is purely for efficiency reasons. It takes a lot less space to store a single number representing seconds than it does to store separate values for the year, month, hour, etc.
Of course, in modern terms, the space difference isn’t all that much at all. But consider that Unix originated in the late 1960s when available storage was far smaller. Timestamps are also used a lot, so their storage adds up. For example, every file has three timestamps associated with it.
The format is pretty much impossible to translate in your head unless you’re a mathematical genius. But it still has some advantages over more readable alternatives such as Wed, 21 Oct 2015 07:28:00 GMT. You can order two Unix timestamps very easily, at a glance. It’s also usually quicker to work out the difference between two timestamps. This is particularly true for dates close together, such as on adjacent days.
About the Epoch
So, Unix time is a total count of seconds since a specific point in time. But what is that point in time? It’s 00:00:00 UTC on 1st January 1970. This is often referred to as the Unix Epoch. Programmers chose this date for the epoch out of convenience since it was the closest round date when they invented Unix time.
You may well have seen this date when something has gone wrong. It’s clearly a bug, but one that looks very strange when it results in a date from before the time many of us were born! It’s completely understandable, though, when you know about Unix time. If any system is trying to display a timestamp that doesn’t have any value, it will often translate to 0 and result in the exact epoch date.
The Unix Time Data Format
Strictly speaking, there isn’t one. The original data type was a 32-bit integer, and this often remains the case, even in much more powerful systems.
This data type allows the value to store a total of 2^32 seconds, which is just over 136 years. This value is typically signed, meaning that it can be negative or positive. So, it usually represents 68 years on either side of the epoch i.e. 1902-2038.
This is still a limited period, of course. But the primary use of the timestamp format was for concepts such as file modification. The need was very much to represent time close to the present, rather than ancient history or far into the future. Even for applications such as calendars, there is rarely a need to represent dates more than a few decades into the future.
But that doesn’t mean this limited time span is without problems…
The Year 2038 Problem
The Y2K Bug (one of the worst programming mistakes in history) affected computer systems that stored years as two-digit values. When the year 2000 came around, such systems treated it as if it were 1900. In the event, this wasn’t as catastrophic as feared, mainly because many people spent a lot of time and effort in advance, preparing for it.
If you were paying attention in the previous section, you might have spotted a similar problem that could affect Unix time. Well, Unix time does have a data problem of its own: the Y2k38 problem. (It’s often referred to as a problem, not a bug; maybe we’ve become more optimistic since the year 2000!) When Unix time literally runs out in 2038, systems will treat new dates either as 1902 or 1970. Or possibly they’ll just fail altogether.
At least this problem won’t hit us at the stroke of midnight on New Year’s Eve. The final second of 32-bit Unix time will fall on the 19th of March. In the eventuality, we will likely upgrade most systems by 2038 or they’ll be obsolete by then anyway.
Some Useful Timestamp Resources
The Epoch Converter site is possibly the most comprehensive timestamp converter available. It begins by displaying the current Unix time—in real-time—and adds almost every imaginable feature on top of that. Its main use is for converting between timestamps and human-readable dates, in both directions.
Dan’s Tools is a huge collection of useful web apps, one of which is a timestamp converter. It’s more basic, but has a very clean presentation and is easy to use.
Time.is presents another, even more minimalistic look. It shows time in a number of formats, including Unix time. It includes the current time in its page title which is useful.
Using Unix Time With Command Line Tools
On Linux and macOS, the date program is the core utility for dealing with date/time, including Unix timestamps. Called without any arguments, it returns the current date/time in a human-readable format:
$ date
Wed Feb 10 12:28:30 GMT 2021
If you need the current date/time in Unix time, use the +%s argument:
$ date +%s
1612960114
You can convert from a human-readable date into a timestamp using the -d flag if your version of date supports it. Most Linux versions should, by default:
$ date -d "Jan 2 1970" +%s
82800
On macOS, date is a different program, which requires a different set of flags:
$ date -j -f "%b %d %Y %T" "Jan 02 1970 00:00:00" "+%s"
82800
Going in the other direction, you can convert from a Unix timestamp using the -r flag:
$ date -r 1600000000
Sun 13 Sep 2020 13:26:40 BST
Some other programs use the %s format to deal with Unix time. For example, if you want to show the modification date of a file in Unix time, with the Linux version of ls, you can use the following:
$ ls -l --time-style=+%s index.tmp.html
-rw-r--r-- 1 ubuntu ubuntu 17862 1521649818 index.tmp.html
How to Use Unix Time in Programming Languages
PHP has the time() function which returns the current Unix timestamp. Its date() function takes a timestamp as its second argument:
$ php -r 'echo date("Y-m-d", time());'
2021-02-11
JavaScript approaches things in an interesting way. It has a Date.now() method to get the number of milliseconds since the Unix epoch. Of course, you can divide this by 1,000 and round the result to give the equivalent Unix time in seconds:
> Math.floor(Date.now() / 1000)
1613083012
Understanding Unix Time
Unix time is a simple concept that crops up in many places. Once you understand it, you might find it quite useful, when calculating time differences, for example. You can also recognise when it may be the cause of certain bugs.
Concepts such as the epoch and timestamps are an important part of getting started with Linux. To find out more about essentials such as ls, check out our guide to basic Linux commands.