Convert epoch time to real time AIX v7

I have read many OLD posts about how to do this including doing the date -d xx. This does not work in AIX.

I'm trying to make a short ksh script that will take input from command line like this:
./lintime 1707858293

Then provide me the human readable output like "date" does:
Tue Feb 13 16:16:24 CST 2024

Based on researching, I thought that using $1, $2..etc would read the variables in from the command line. Using that, my coding attempt was as follows:

cat ./lintime
#!/usr/bin/ksh

echo $(perl -e 'print scalar(localtime($1)), "\n";')

When this is ran from above command it gives the following incorrect output:

./lintime 1707858293
Wed Dec 31 18:00:00 1969

But when I run the perl script only, it prints out correctly

perl -e 'print scalar(localtime(1707858293)), "\n";'
Tue Feb 13 15:04:53 2024

Just need help figuring this out.

check out the ksh documentation for printf , an example for you start with below (from a non AIX system, but from within ksh shell), adjust the format to meet your specifics

ksh
printf "%(%Y-%m-%d %H:%M:%S)T\n" "#1707858293"
2024-02-13 21:04:53

this is a good reference ksh date musings , should give you all you need to know

ksh might be ksh88.
Run the printf "%( )T" magic in ksh93 !

it's been awhile since my AIX days, but... a couple of REALLY old cliff notes on the subject - not ksh93 dependent:

# here's my current EPOCH time
currentEpoch=$(/usr/bin/truss /usr/bin/date 2>&1 | /usr/bin/awk '/^time/ {print $NF}')

# here we convert EPOCH back to human readable format
back2Human=$(echo "0t${currentEpoch}=Y" | /usr/bin/adb)

Things might have gotten better/easier since then...

1 Like

@Kentlee65 , apologies, having read your email again , i see you were after 'fixing' an issue with the script, :frowning:

the following should fix your issue

cat lintime
#!/usr/bin/ksh

perl -e 'print scalar(localtime($ARGV[0])), "\n";' "$1"

# now execute

./lintime 1707858293
Tue Feb 13 21:04:53 2024
2 Likes

You don't need the
echo $( )
indirection.
But your mistake was to expect that the shell evaluates a $1 in a 'string in ticks'.
It would do it in a "string in quotes", but that would conflict with many things in perl.

The suggested $ARGV[0] in perl (in ' ' ) and a given $1 argument (in " ") is the proper solution.
Using perl -l a print puts a trailing "\n" (and input lines are chomp ed):

perl -le 'print scalar(localtime($ARGV[0]))' "$1"

Now you could even put this(!) shell code in " " and let the shell evaluate the $1

perl -le "print scalar(localtime($1))"

But it remains a hack, imagine $1 would have a ( character...

$ uname -s
AIX
$ printf "%(%Y-%m-%d %H:%M:%S)T\n" "1707858293"
2024-02-13 22:04:53

Nice one !!!

Added just for fun

$ printf "%(%Y-%m-%d %H:%M:%S)T\n" 2147483647
2038-01-19 04:14:07
$ printf "%(%Y-%m-%d %H:%M:%S)T\n" 2147483648
1901-12-13 21:45:52

For a long time, AIX has included the commands ksh and ksh93. Aix ksh is ksh88?
Have you tested command ksh93? AIX has ksh = ksh88 and also ksh93
Location ksh93, I'm not sure. My guess /usr/bin/ksh93. printf T option is published in ksh93, not in ksh88.

ksh93
printf "%(%Y-%m-%d %H:%M:%S)T\n" "#1707858293"
2024-02-13 21:04:53
# need updated ksh93 before using date > 2038-01-19 ...
$ /bin/ksh93 -c 'printf "%(%Y-%m-%d %H:%M:%S)T\n" 2147483648'
/bin/ksh93[1]: printf: warning: invalid argument of type T
2024-02-14 15:08:11
$ /bin/ksh -c 'printf "%(%Y-%m-%d %H:%M:%S)T\n" 2147483648'
(%m-0 %M:%S)T
$ /bin/bash -c 'printf "%(%Y-%m-%d %H:%M:%S)T\n" 2147483648'
1901-12-13 21:45:52
$
/bin/ksh93
printf "%(%s)T\n" "2024-02-14 15:08:11"
# epoc = 1707916091
printf "%(%Y-%m-%d %H:%M:%S)T\n" "#1707916091"
# 2024-02-14 15:08:11

okay, thanks, no hash in bash, hash in ksh93. but still not over 2038

$ /bin/ksh93 -c 'printf "%(%Y-%m-%d %H:%M:%S)T\n" \#2147483648'
1970-01-01 01:00:02
$ /bin/bash -c 'printf "%(%Y-%m-%d %H:%M:%S)T\n" 2147483648'
1901-12-13 21:45:52

funny difference of 2 years between bash and ksh93

The Y2038 problem is a 32bit overflow.
Bash - date, working around the 2038 bug on 32bit LINUX system - Unix & Linux Stack Exchange
But is somehow even on a 64bit Linux. Maybe 2038 will be the end of Linux :stuck_out_tongue_winking_eye:

But perl eats it all. Will exist until mankind will have wiped out itself :stuck_out_tongue_winking_eye:

2 Likes

just the end of time surely, *nix will persist :pray:

2 Likes