Epoch time is a convention used on Unix and Linux systems that many applications rely on for calculating time between dates and other similar functions. Some Linux commands like
perl have an epoch option built in. An epoch denotes the number of seconds that have passed since the date January 1st, 1970.
In case you’re wondering, there’s nothing special about this date. It’s merely an arbitrary date that was agreed upon some time ago and is used for convenience. In this tutorial, we will show you how you can convert dates and times to and from Unix epoch time.
In this tutorial you will learn:
- What is Unix epoch time?
- How to convert dates to and from Unix epoch time
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux distro|
|Other||Privileged access to your Linux system as root or via the
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
What is Linux epoch time?
As explained above, epoch time can be used as a base of reference, which is 00:00:00 UTC on January 1, 1970. From this date, applications can assign a timestamp based on the number of seconds something occurred before or after it.
These timestamps are great for calculation, but hardly usable for people. It’d take a machine to know that
Thu 02 Jun 2016 12:59:59 PM UTC translates to
1464872399. Thus epoch time is a function you are more likely to find in a Bash script or code for a program.
Epoch time conversion examples
See the following commands that you can run in your own terminal to convert dates and times to and from Unix epoch time.
- To convert a timestamp to a human-readable date, use the following command syntax and substitute your own timestamp in place of the one in this example. Note that this will return the time in UTC.
$ date -ud @1464872499 Thu 02 Jun 2016 01:01:39 PM UTC
- If you’d like to get the time for your system’s currently configured time zone, you can omit the
$ date -d @1464872499 Thu 02 Jun 2016 09:01:39 AM EDT
- To convert a date to epoch timestamp, use the following syntax.
$ date -d '06/02/2016 09:01:39' +"%s" 1464872499
Note that the
-doption is expecting you to specify the date in American format (MM/DD/YYY). The command also assumes we’re using our current time zone since the
-uoption hasn’t been specified.
- You could also use this format. Feel free to omit the day of the week (Thursday in this case), or not. Notice the
-uoption is unnecessary here since UTC is explicitly mentioned.
$ date -d 'Thu 02 Jun 2016 01:01:39 PM UTC' +"%s" 1464872499
- To retrieve the current epoch time from our system, use the the following
$ date +"%s" 1653511975
- We can also utilize epoch time in Perl. The following command converts an epoch date to a human readable date.
$ perl -e 'print scalar(localtime(1284763671)), "\n"' Fri Sep 17 18:47:51 2010
In this tutorial, you learned about epoch time and how it is utilized on a Linux system. You also saw how to convert a human readable date to epoch time, and how to convert epoch seconds to a human readable date and time. Knowing this will allow you to perform calculations between two dates as well as similar functions from the Linux command line.