#------------------------------------------------------------------------------
# File:         local_time.config
#
# Description:  Determine local time based on GPS location and GPSDateTime
#
# Notes:        1. Requires that the API TimeZone option functions properly
#                  using an IANA time zone name (like "America/New_York"), so
#                  this may not work on Windows systems.
#
#               2. Time is based on GPSDateTime preferentially, otherwise
#                  DateTimeOriginal then CreateDate assuming the system time
#                  zone unless the corresponding OffsetTime tag exists.
#
# Usage:        exiftool -config local_time.config -localtime FILE
#
# Options:      The user parameter LocalTag may be set to the name of the
#               date/time tag to use, case insensitive with optional group
#               name(s).  For example add this to the command to use
#               SubSecDateTimeOriginal as the basis for LocalTime:
#
#                 -userparam localtag=subsecdatetimeoriginal
#
# Requires:     ExifTool version 13.56 or later
#
# Revisions:    2025-10-19 - P. Harvey Created
#               2026-04-02 - PH Preserve decimal seconds up to 6 digits
#               2026-04-08 - PH Added LocalTag feature
#------------------------------------------------------------------------------

%Image::ExifTool::UserDefined = (
    'Image::ExifTool::Composite' => {
        LocalTime => {
            Groups => { 2 => 'Time' },
            Require => {
                0 => 'GPSLatitude',
                1 => 'GPSLongitude',
            },
            Desire => {
                2 => 'GPSDateTime',
                3 => 'SubSecDateTimeOriginal',
                4 => 'SubSecCreateDate',
                5 => 'DateTimeOriginal',
                6 => 'CreateDate',
            },
            ValueConv => q{
                require Image::ExifTool::Geolocation;
                my ($loc) = Image::ExifTool::Geolocation::Geolocate("$val[0],$val[1]");
                return undef unless $loc;
                my $pref = $self->Options(UserParam => 'LocalTag');
                my $time;
                if ($pref) {
                    my $num = { gpsdatetime => 2,
                                subsecdatetimeoriginal => 3,
                                subseccreatedate => 4,
                                datetimeoriginal => 5,
                                createdate => 6 }->{lc $pref};
                    if ($num) {
                        $time = $val[$num];
                    } else {
                        $time = $self->GetValue($pref);
                    }
                } else {
                    $time = $val[2] || $val[3] || $val[4] || $val[5] || $val[6];
                }
                return undef unless $time;
                my $secs = Image::ExifTool::GetUnixTime($time, 1);
                return undef unless $secs;
                my @info = Image::ExifTool::Geolocation::GetEntry($$loc[0]);
                $self->Options(TimeZone => $info[5]);
                my $localTime = Image::ExifTool::ConvertUnixTime($secs, 1, -6);
                $self->Options(TimeZone => undef);
                return $localTime;
            },
        },
    },
);

1; #end

