Search code examples
unix-timestampaix

Alternative date command option in AIX


I want to convert specific date to timestamp in AIX. Following command is working in GNU/LINUX flavor. Can someone please help me to get it done in AIX as well?

Command working on GNU/LINUX:

Command -> date -d"Nov 14 02:31" "+%s"

Output -> 1542162660


Solution

  • You might do something like this with Perl, if you have POSIX::strptime Example program (totimestamp.pl):

    #!/usr/bin/perl
    
    use strict;
    use POSIX ("tzset", "mktime");
    use POSIX::strptime;
    
    POSIX::tzset ();
    
    my $ARGC= scalar (@ARGV);
    my $tstamp;
    
    if ($ARGC < 1) {
        $tstamp= time ();
    
    } else {
        my $tstr= $ARGV[0];
        my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst);
        ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = 
        POSIX::strptime($tstr, "%b %d %H:%M:%S %Y");
    
        $tstamp= POSIX::mktime ($sec, $min, $hour, $mday, $mon, $year);
    }
    
    printf ("%d\n", $tstamp);
    

    Usage:

    perl ./totimestamp.pl "Nov 16 14:40:00 2018"
    1542375600