#!/usr/bin/perl
use strict;
use warnings;
use Time::Piece;

# Set the target timestamp
#my $target_date_str = $ARGV[0];
my $target_date_str = shift or die "Usage: $0 <'%Y-%m-%d %H:%M:%S' time stamp>";
my $target_time = Time::Piece->strptime($target_date_str, '%Y-%m-%d %H:%M:%S');

# Open rpm -qa --last to get packages sorted by installation time [2]
open(my $rpm_fh, "-|", "rpm -qa --qf '%{VERSION}-%{NAME} %{INSTALLTIME:date}\\n' --last") or die "$0: Cannot run rpm command: $!";

while (my $line = <$rpm_fh>) {
    chomp($line);
  
    # Example format: name-version-release Day Mon DD HH:MM:SS YYYY
    if ($line =~ /^(.*?)\s+(\w{3}\s+\w{3}\s+\d{1,2}\s+\d{2}:\d{2}:\d{2}\s+\d{4})$/) {

        my $pkg  = $1;
        my $date = $2;
       
        # Parse the installation date
        my $pkg_time = Time::Piece->strptime($date, '%a %b %d %H:%M:%S %Y');
    
        if ($pkg_time < $target_time) {
            close ($rpm_fh);
            exit(0);
        }
        
        # Print if the package was installed after the target time
        $pkg =~ /^(.*?)-[^-]+?-[^-]+?\.[^.]+?$/;
        my $short_name = $1;
        print "$short_name\n";
    }
}
close($rpm_fh);

