Showing posts with label time. Show all posts
Showing posts with label time. Show all posts

Wednesday, October 9, 2019

How to Convert Epoch Seconds To the Current Time in linux?

Linux / UNIX: Convert Epoch Seconds To the Current Time

how you can obtain the UNIX epoch time (number of seconds since 1970-01-01
00:00:00 UTC) using the Linux bash "date" command. It also shows how you can
convert a UNIX epoch time to a human readable time.

Obtain UNIX epoch time using bash

Obtaining the UNIX epoch time using bash is easy. Use the build-in date command and
instruct it to output the number of seconds since 1970-01-01 00:00:00 UTC. You can do this
by passing a format string as parameter to the date command. The format string for UNIX
epoch time is '%s'.


Print Current UNIX Time
Type the following command to display the seconds since the epoch:

Syntax: date +%s

date +%s
1569569962


Convert Epoch To Current Time
Type the command:

date -d @Epoch

date -d @1569569962
date -d "1970-01-01 1569569962 sec GMT"

Sample outputs:

Fri Sep 27 13:09:22 IST 2019

To convert number of seconds back to a more readable form, use a command like this:

date -d @1569569962 +"%d-%m-%Y %T %z"


Using AWK



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

Output:

2019-09-27 13:09:22

Using Perl

perl -e 'print scalar(localtime(1569569962)), "\n"'

Output:

Fri Sep 27 13:09:22 2019