Why does Unix have its own time concept? What is the epoch and what is 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 are likely to be using it without even realizing it. Once you understand the Unix era, you will see it in many contexts. Various tools can help you get started with Unix time.
What is the purpose of Unix Time?
The Unix time is the total number of seconds since a fixed time and date. It is a date / time (or timestamp) format that is different from human readable dates and times. This is purely for reasons of efficiency. Storing a single number representing seconds takes up much less storage space than storing separate values for year, month, hour, etc.
In modern terms, of course, the difference in space is not that great. Keep in mind, however, that Unix emerged in the late 1960s when the available memory was much smaller. Timestamps are also commonly used so their memory adds up. For example, three time stamps are assigned to each file.
The format is hard to translate in your head unless you are a math genius. But it still has some advantages over more readable alternatives like Wed Oct 21, 2015 7:28:00 AM GMT. You can easily order two Unix timestamps at a glance. It is also usually quicker to tell the difference between two timestamps. This is especially true for data that are close together, e.g. B. on neighboring days.
About the epoch
So the Unix time is a total of seconds since a given point in time. But what is this point in time? It is 00:00:00 UTC on January 1, 1970. This is often referred to as the Unix era. Programmers chose this date for the epoch for convenience, as it was the closest round date to when they invented Unix time.
You may have seen this date when something went wrong. It's clearly a mistake, but one that looks very strange when it leads to a date ahead of the time when many of us were born! However, it's completely understandable if you know the Unix era. Often times, when a system tries to display a timestamp that has no value, it translates to 0 and leads to the exact epoch date.
The Unix time data format
In fact, there aren't any. The original data type was a 32-bit integer, and this often remains the case in much more powerful systems.
With this data type, the value can store a total of 2 ^ 32 seconds, which corresponds to a little over 136 years. This value is usually signed, which means it can be negative or positive. So it usually represents 68 years on either side of the epoch; H. 1902-2038.
This is of course still a limited period of time. However, the timestamp format was mainly used for concepts such as modifying files. It was very important to put time close to the present and not in ancient history or far in the future. Even for applications like calendars, there is seldom a need to display dates more than a few decades into the future.
But that doesn't mean that this limited period of time is without problems …
The year 2038 problem
The Y2K bug (one of the worst programming errors in history) affected computer systems that had stored years of double-digit values. When the year 2000 came, such systems treated it as they did 1900. In this case, it was not as catastrophic as feared, mainly because many people had put a lot of time and effort into preparing for it beforehand.
The 10 worst coding mistakes in history
In its storied past, coding has also wreaked havoc. Instances of a little bad code caused disaster on a larger scale. The following are 10 of the worst programming mistakes in history.
If you paid attention in the previous section, you may have encountered a similar problem that can affect Unix time. Well, the Unix era had its own data problem: the Y2k38 problem. (It's often referred to as a problem, not a bug. Perhaps we've become more optimistic since 2000!) When Unix time literally runs out in 2038, systems will treat new dates as either 1902 or 1970. I will just fail completely.
At least this problem won't hit us at midnight on New Years Eve. The last second of 32-bit Unix time will fall on March 19th. If so, we'll likely update most systems by 2038 or they'll be out of date by then anyway.
Some useful timestamp resources
The Epoch Converter site is possibly the most comprehensive timestamp converter available. First, the current Unix time is displayed in real time and almost every imaginable function is added. The main application is to convert between timestamps and readable data in both directions.
Dan & # 39; s Tools is a huge collection of useful web apps, one of which is a timestamp converter. It is simpler, but has a very clear presentation and is easy to use.
Time.is presents another, even more minimalist look. It shows the time in a number of formats, including Unix time. It has the current time in its page title which is useful.
On Linux and MacOS, the date Program is the main utility for dealing with date and time, including Unix timestamps. Called with no arguments and returns the current date and time in a human-readable format:
Wed February 10 12:28:30 GMT 2021
If you need the current date / time in Unix time, use the +% s Argument:
$ date +% s
Use to convert from a human readable date to a timestamp -d if your version of date supports it. Most Linux versions should by default:
$ date -d "January 2, 1970" +% s
On macOS date is another program that requires a different set of flags:
$ date -j -f% b% d% Y% T January 02, 1970 00:00:00 +% s
If you're going the other way, you can convert using a Unix timestamp -r Flag:
$ date -r 1600000000
Sun 13 Sep 2020 13:26:40 BST
Some other programs use the % s Format for the Unix era. For example, if you want to see the modified date of a file in Unix time using the Linux version of lsyou can use:
$ ls -l –time-style = +% s index.tmp.html
-rw-r – r– 1 ubuntu ubuntu 17862 1521649818 index.tmp.html
Use of Unix time in programming languages
PHP has the Time() Function that returns the current Unix timestamp. It is Date() The function takes a timestamp as the second argument:
$ php -r & # 39; echo date ("Y-m-d", time ()); & # 39;
> Math.floor (Date.now () / 1000)
Understand Unix time
Unix time is a simple concept that comes up in many places. Once you understand, you may find it very useful, for example when calculating time differences. You can also see when this can be the cause of certain errors.
Concepts like the epoch and timestamp are an important part of getting started with Linux. To learn more about the essentials, such as lsCheck out our guide to basic Linux commands.
9 Basic Commands for Getting Started with Linux
Would you like to familiarize yourself with Linux? Start with these basic Linux commands to learn standard computing tasks.
About the author
(27 articles published)
Bobby is a technology enthusiast who has worked as a software developer for nearly two decades. He loves gaming, works as the Reviews Editor at Switch Player Magazine and is immersed in all aspects of online publishing and web development.
From Bobby Jack
Subscribe to our newsletter
Subscribe to our newsletter for tech tips, reviews, free e-books, and exclusive deals!
One more step …!
Please confirm your email address in the email we just sent you.