#------------------------------------------------------------------------------
# File:         age.config
#
# Description:  Definition for a Composite "Age" tag to calculate age of person
#               in image based on their birthday and the DateTimeOriginal or
#               CreateDate of the image.
#
# Notes:        The birthday date is set via the -userParam option, and is a
#               string containing year, month, day, hour, minute and second.
#               Only the year is required.  By default the format of the
#               returned Age value is exactly the same as the input birthday,
#               with 2 digits for each parameter, however an AgeFormat user
#               parameter may be used to define a printf-style format string
#               for any desired output format (see fourth example below).
#
#               If the specified birthday comes after the image date/time
#               then the returned result is enclosed in brackets to indicate
#               a negative time difference (see last example below).
#
#               If birthday is not given, then the current date/time is assumed
#               (in which case Age returns the age of the photo).
#
# Examples: (based on a DateTimeOriginal of "2005:08:27 18:21:00")
#
#   > exiftool -config age.config -userparam birthday=1990:09:31 -age a.jpg
#   Age                             : 14:10:26
#
#   > exiftool -config age.config -userparam birthday=1990Y09M31D -age a.jpg
#   Age                             : 14Y10M26D
#
#   > exiftool -config age.config -userparam birthday="2000:01:02 03:45" -age a.jpg
#   Age                             : 05:07:25 14:36
#
#   > exiftool -config age.config -userparam birthday=2000:01:02 \
#       -userparam ageformat="%d years" -age a.jpg
#   Age                             : 5 years
#
#   > exiftool -config age.config -userparam birthday=2005:09:26 -age a.jpg
#   Age                             : (00:00:30)
#
# Requires:     ExifTool version 9.90 or later
#
# Revisions:    2017/01/24 - P. Harvey Created
#------------------------------------------------------------------------------

%Image::ExifTool::UserDefined = (
    'Image::ExifTool::Composite' => {
        Age => {
            Desire => {
                0 => 'DateTimeOriginal',
                1 => 'CreateDate',
            },
            ValueConv => q{
                my $bday = $self->Options(UserParam => 'Birthday');
                $bday or ($bday = TimeNow()) =~ s/[-+].*//;
                my $date = $val[0] || $val[1] or return undef;
                my @t0 = ($bday =~ /\d+/g);
                my @t1 = ($date =~ /\d+/g);
                return '<invalid birthday date>' unless $t0[0];
                my ($i, @diff, $sign);
                for ($i=0; $i<6; ++$i) {
                    last unless defined $t0[$i];
                    my $dt = $t1[$i] - $t0[$i];
                    $sign or $sign = ($dt <=> 0);
                    push @diff, $dt * $sign;
                }
                require 'Image/ExifTool/Shift.pl';
                my $base = $sign < 0 ? \@t1 : \@t0;
                my @wrap = (0, 12, DaysInMonth($$base[1]||1,$$base[0]), 24, 60, 60);
                for ($i=$#diff; $i>0; --$i) {
                    $diff[$i] < 0 and $diff[$i] += $wrap[$i], --$diff[$i-1];
                }
                $bday =~ s/\d+/sprintf '%.2d', shift @diff/ge;
                return $sign < 0 ? "($bday)" : $bday;
            },
            PrintConv => q{
                my $fmt = $self->Options(UserParam => 'AgeFormat') or return $val;
                sprintf($fmt, $val =~ /\d+/g);
            },
        },
    },
);

1;  #end
