Portable way of translating epoch time

echo $(date +%s) | awk '{ print strftime("%c", $2"-"$3"-"$NF"/"$4); }'

The above command only seems to work on newer versions of awk or systems with gawk installed.

how can i translate the epoch time into a human readable format using a portable method?

also,

date -d@$epochtime 

does not work on systems like AIX. so thats not portable.

I'd feel somewhat surprised if above would print anything reasonable. date +%s (WHY the echo , BTW ?) prints sth. like 1489436283 , so in the following awk , NF is 1, and all the fields referenced are undefined.

Recent shells ( bash , ksh , ...) offer a printf builtin with the %(...)T format specifier:

printf "%(%F %T)T\n", $ET
2017-03-13 21:23:02
2 Likes

I typically recommend using perl for a portable solution to this type of problem, most OS have it installed by default:

$ perl -e 'use POSIX; print strftime($ARGV[0]."\n", localtime(time() + $ARGV[1]));' '%c' $((10 * 60 * 60))
Tue, Mar 14, 2017  2:45:50 AM
3 Likes

Hi.

The command found here General Purpose Date Script can be used like:

date.pl -d "@$(date +%s)"

producing:

2017-03-14 10:52:23

Thank Corona688 if you use it.

My name for it is date.pl -- here are some details:

date.pl  GNU date work-alike. (what)
Path    : ~/bin/date.pl
Version : - ( local: RepRev 1.13, ~/bin/date.pl, 2016-09-11 )
Length  : 378 lines
Type    : Perl script, ASCII text executable
Shebang : #!/usr/bin/env perl
Help    : probably available with --help
Home    : http://www.unix.com/tips-tutorials/239167-general-purpose-date-script.html
Modules : (for perl codes)
 POSIX  1.38_03
 strict 1.08
 warnings       1.23
 constant       1.31

This is one of the codes in my new system toolbox that I carry with me to new environments.

Best wishes ... cheers, drl