CompositeTag: "OldestDateTime" modification

Started by sx8999, June 15, 2024, 02:35:50 AM

Previous topic - Next topic

sx8999

Hi all,
I got this very useful CompositeTag from this forum here which helps me with old files which have been created, copied around, modified etc - to still be able to find the oldest datetime:

%Image::ExifTool::UserDefined = (
    'Image::ExifTool::Composite' => {
        # Select oldest date from a number of date tags
        OldestDateTime => {
            Desire => {
                0 => 'FileModifyDate',
                1 => 'MDItemFSContentChangeDate',
                2 => 'FileCreateDate',
                3 => 'MDItemFSCreationDate',
                4 => 'ModifyDate',
                5 => 'CreateDate',
                6 => 'DateTimeCreated',
                7 => 'DateTimeOriginal',
                8 => 'CreationDate',
            },
            ValueConv => q{
                my $oldest = undef;
                for my $date (@val) {
                    $date =~ s/[+-]\d{2}:\d{2}$//; # Strip TimeZone
                    if ($date && (!$oldest || $date lt $oldest)) {
                        $oldest = $date;
                    }
                }
                return $oldest;
            },
        },
    },
);

1;

Unfortunately I recognized one UseCase problem:

When transfering files from Android to Windows using a WiFi FTP Server app, the File-Creation/Modification dates are modified and show the Time as "00", see this screenshot for such a file:



Therefore, when using the OldestDateTime CompositeTag, it will change all files to 00s - which is not what I want.

Is there a way to NOT use an older date in case it is older, but has the same date/hours/minutes but just the seconds are set to "00" (i.e. just a few seconds older)?

In the code itself, I guess instead of "$date lt $oldest" I would need to split the string in two pieces: One piece containing date+hours+minutes and a second string just the seconds.

EDIT: I think I got it - what do you think? In my test cases it seems to run fine...

%Image::ExifTool::UserDefined = (
    'Image::ExifTool::Composite' => {
        # Select oldest date from a number of date tags
        OldestDateTime => {
            Desire => {
        0 => 'DateTimeCreated',
                1 => 'DateTimeOriginal',
                2 => 'CreationDate',
                3 => 'ModifyDate',
                4 => 'CreateDate',
        5 => 'MDItemFSCreationDate',
        6 => 'FileModifyDate',
                7 => 'MDItemFSContentChangeDate',
                8 => 'FileCreateDate',
            },
            ValueConv => q{
                my $oldest = undef;
                my $oldestDateHoursMinutes = undef;
        my $oldestSecs = undef;
        my $dateHoursMinutes = undef;
        my $dateSecs = undef;
        my $regex = qr/(.*?):(\d{2})\s*$/mp;
                for my $date (@val) {
                    $date =~ s/[+-]\d{2}:\d{2}$//; # Strip TimeZone
                    # Trim leading and trailing whitespace
                    $date =~ s/^\s+|\s+$//g;
                    # Split the string into two parts: before the seconds, and the seconds
                    # This regular expression matches the entire datetime, capturing two groups:
                    # 1. Everything up to the seconds (.*?) lazily, ensuring it stops before the last colon
                    # 2. The seconds (\d{2}) specifically
                    if ($date =~ /$regex/g) {
                        $dateHoursMinutes = $1; # Everything before the seconds
                        $dateSecs = $2;      # Just the seconds
                       
                        if ($date && (!$oldest || $dateHoursMinutes lt $oldestDateHoursMinutes || ($dateHoursMinutes eq $oldestDateHoursMinutes && $dateSecs ne "00" && $dateSecs lt $oldestSecs))) {
                            $oldest = $date;
                            $oldestDateHoursMinutes = $dateHoursMinutes;
                            $oldestSecs = $dateSecs;
                        }
                    }
                }
                return $oldest;
            },
        },
    },
);

1;

StarGeek

I don't see any obvious errors.  Keep testing and as long as it work, great.
* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).