How to Convert an epoch Timestamp to a Human Readable?

In Linux the epoch timestamp is also known as the Unix timestamp. It calculates the time in seconds that have elapsed from January 1, 1970, at 00(hours):00(minutes):00(seconds) UTC. The epoch timestamp varies according to the operating system i.e Windows uses January 1, 1601, and VMS(Virtual Memory System) uses November 17, 1858. 

The epoch timestamp is difficult to read as it is defined in terms of whole seconds. It requires the conversion in a desired date and time format that is human-readable. 

This guide will illustrate how to convert an epoch timestamp to a human-readable format in Linux.

Get epoch Timestamp

Execute the “date” command with the addition of “%s” format specifier to display the current date and time into epoch time i.e in “seconds”:

$ date +%s

The above epoch time can not be understood by the user. Let’s see how the Linux built-in command line tools can convert this time into the desired readable format.

Method 1: Using the “date” Command

The “date” is the built-in command line utility that displays the current date and time of the system and also sets it manually. As it can display the epoch time and can also convert it to a human-readable format. 

To convert the epoch to human readable, specify the “@” with the seconds and use the “-d” flag with the “date” command in this way:

$ date -d @1678443845

The above command has converted the epoch timestamp into human-readable format. The “EST” represents the current system time zone in the output.

Change Timezone

The user can also change the time zone, i.e., UTC while the conversion of epoch timestamp by using the “-u” flag: 

$ date -ud @1678443845

The time zone has now changed to “UTC(Universal Time Coordinated)”.

Method 2: Using the “awk” strftime Format

The “awk” command searches the text pattern from the file and performs specific operations on it like add, replace, and print. 

In addition, It uses the “starftime ” to convert the epoch time in a specific format that is humanly readable:

$ awk 'BEGIN { print strftime("%Y-%m-%d %H:%M:%S", 1678443845); }'

In the above command:

  • BEGIN: Specifies the set of instructions to perform the defined task, i.e., print the epoch time into the human-readable format.
  • print: Display the specified timestamp in “%Y-%m-%d %H:%M:%S” format after conversion.
  • %Y-%m-%d %H:%M:%S: 24-hours Timestamp format, i.e., “Y(year)”, “m(month)”, “d(day of a month)”, “H(hour)”, “M(minutes)”, “S(seconds)”
  • 1678443845: Epoch time stamp in seconds.

The epoch time has been changed.

Method 3: Using the “echo” With “perl” localtime() Function

The “echo” command generally takes the argument as a statement and prints it on the terminal. It can also be used with the “perl” scripting language. The “perl” uses the built-in function called the Date and time module to handle time-related data.

Concatenate both the “echo” command and “perl” oneliner script to convert an epoch timestamp to a human-readable format:

$ echo 1678443845 | perl -pe 's/(\d+)/localtime()/e'

The above command is described here:

  • echo: Specifies the epoch time i.e in seconds.
  • perl: Denotes pre-defined date and time module.
  • p: Places a print loop around the command.
  • e: flag passes the perl script as a parameter i.e perl oneliner rather than a separate script file.
  • s/(\d+)/localtime()/e‘: Regular expression searches for the specified date and replace it with the local time of the system.
  • localtime(): Perl function that returns the current date and time of the system without passing any argument:

The epoch time has been successfully converted into the current date and time of the system that is humanly readable.

Conclusion

In Linux, the epoch timestamp can be easily converted to human-readable format using the “date”, and the “awk” command having starftime format. The “localtime()” function with the “echo” command is also beneficial to convert the epoch timestamp into the current date and time of the system which is human-readable.

This guide has covered all possible methods to convert an epoch timestamp to a human-readable format.