4

I need to convert unix timestamp (number of seconds since 1970, e.g. 1381260225 is Tue Oct 8 19:23:45 GMT 2013) into a standard human-readable format.

I could get some answers by googling but those were not a suitable solution for me because I don't use Linux, I use an AIX 6.1 machine with ksh88. AIX does not have GNU utilities.

  • 1
    How amenable are you to a C-program? – ChuckCottrill Oct 15 '13 at 21:16
  • 1
    @ChuckCottrill: The AIX machine which I use is not having C compiler. – ramp Oct 16 '13 at 9:40
  • 1
    You say you don't have the GNU utilities, what does which perl return on the server? Also, give that relies on your path and we don't know what that is, how about ls -l /usr/bin/perl. – EightBitTony Apr 9 '14 at 7:58
4

If you were on Linux, you could simply use:

date -d @1381260225

Or you could use gawk:

echo "1381260225" | gawk '{print strftime("%c",$1)}'

Or Python:

python -c "import datetime; print  datetime.datetime.fromtimestamp(1381865497)"

Or Perl:

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

However none of these solutions are available on a stock AIX installation. All of these tools (GNU coreutils, GNU awk, Perl, Python) are available as separate packages as part of the AIX toolbox for Linux applications.

  • 1
    date -d is a GNU-ism. The equivalent for OpenBSD would be date -r 1381260225. And I'm not even sure that is standard, because Solaris doesn't even have an equivalent of -d or -r. Solaris only seems to be able to set or display the current time. – kurtm Oct 15 '13 at 19:41
  • 1
    @kurtm AIX date does not seem to have -r either, see here. I think RahulPatil's awk solution will be the most portable. – terdon Oct 15 '13 at 19:44
  • @terdon I concur that the awk solution is probably the most portable. I figured AIX wouldn't support it either, but don't have any AIX machines I can check. – kurtm Oct 15 '13 at 19:45
  • 2
    @kurtm You can look up AIX man pages online. None of these methods work on AIX, not even the awk method as awk doesn't have strftime (it's a GNU extension). – Gilles Oct 15 '13 at 21:48
  • 1
    For info: AIX comes with perl, I'm pretty sure it's installed by default. You don't need to use the AIX toolbox for Linux to install it, it's in the base image. – EightBitTony Apr 9 '14 at 7:55
3

You can probably fall back to Perl:

perl -MPOSIX=strftime -e 'print strftime("%Y-%m-%d %T", localtime($ARGV[0])), "\n"' 1381260225
2013-10-08 15:23:45
  • 1
    +1 but if I'm not mistaken, AIX does not have Perl by default and I don't know whether POSIX::strftime will be installed even if it does. Can these be installed without root access on AIX? – terdon Oct 15 '13 at 19:28
  • @terdon is python default install in AIX ? – Rahul Patil Oct 15 '13 at 19:29
  • @RahulPatil I doubt it (but don't really know). However, both perl and python can be downloaded from here. – terdon Oct 15 '13 at 19:32
  • @RahulPatil If something doesn't have perl by default, it's not likely to have python by default. – kurtm Oct 15 '13 at 19:38
  • @RahulPatil&tredon: The machine I use does not have either Perl or Python at the same time I do not have admin access to the same. – ramp Oct 16 '13 at 9:29
1
# echo 1381260225 | python -c 'import sys; import time; print time.ctime( float( sys.stdin.read() ) )'
Wed Oct  9 02:23:45 2013
  • 1
    A Python answer has already been given. AIX does not have Python installed by default. – Gilles Oct 16 '13 at 9:12
  • My one is better. Compare number of letters. – polar bear on the white snow Oct 16 '13 at 12:33
1

A bit more perlish:

echo 1397028688|perl -l12ne 'print scalar localtime $_'
Wed Apr  9 09:31:28 2014
1

AIX has ksh93, so:

  • local time (here mainland Britain):

    $ ksh93 -c 'printf "%(%Y-%m-%d %T %z)T\n" "#$1"' ksh93 1381260225
    2013-10-08 20:23:45 +0100
    
  • GMT/UTC

    $ TZ=GMT0 ksh93 -c 'printf "%(%Y-%m-%d %T %Z)T\n" "#$1"' ksh93 1381260225
    2013-10-08 19:23:45 GMT
    
-3

what about something like this ??

echo "`date +"%y%m%d%H%M%S"`"
  • 1
    You meant echo "$(date +"%y%m%d%H%M%S")" but it only outputs the Unix timestamp of the current date. The question was about converting a specified Unix timestamp to a human-readable date string. – telcoM Sep 19 '18 at 9:11
  • You're using backticks so the double quotes don't nest. Consider using the more modern style $( ... ) instead. – roaima Sep 19 '18 at 11:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.