Linux epoch time

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 date and 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
Linux epoch time
Linux epoch time
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software N/A
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – 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.

  1. 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
  2. If you’d like to get the time for your system’s currently configured time zone, you can omit the -u option.
    $ date -d @1464872499
    Thu 02 Jun 2016 09:01:39 AM EDT
  3. To convert a date to epoch timestamp, use the following syntax.
    $ date -d '06/02/2016 09:01:39' +"%s"

    Note that the -d option 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 -u option hasn’t been specified.

  4. You could also use this format. Feel free to omit the day of the week (Thursday in this case), or not. Notice the -u option is unnecessary here since UTC is explicitly mentioned.
    $ date -d 'Thu 02 Jun 2016 01:01:39 PM UTC' +"%s"
  5. To retrieve the current epoch time from our system, use the the following date command.
    $ date +"%s"
  6. 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

Closing Thoughts

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.