Search code examples
bashdateunixunix-timestampaix

Convert date string to date epoch UNIX AIX


I'm working with a AIX 5.3 version OS server, and I have to deploy a shell that I did in Linux (RedHat), but I have a big problem when I try to manipulate some timestamps.

In Linux I use:

`Start="Thu Mar 28 16:49:20 2019"` 
`date -d "$Start" +%s`

to calculate the seconds since 1970 that corresponds to the date, but in UNIX AIX 5.3 I get the following error:

date: Not a recognized flag: d
Usage: date [-u] [+"Field Descriptors"]

How can I do this in UNIX AIX? I need to calculate the seconds since 1970 from a specific date (date format: Fri Nov 25 02:11:53 2011)


Solution

  • Convert date to epoch on AIX with Perl:

    #!/usr/bin/perl
    
    # Usage: mkepoch yyyy mm dd HH MM SS
    
    use Time::Local;
    ($yyyy, $mm, $dd, $HH, $MM, $SS)=@ARGV;
    $tm=timelocal($SS, $MM, $HH, $dd, $mm - 1, $yyyy);
    print "$tm\n";
    

    Usage:

    chmod u+x ./mkepoch
    Start="2019 03 28 16 49 20"
    ./mkepoch $Start
    

    Output:

    1553788160
    

    Source: convert a specific date to unix timestamp