#------------------------------------------------------------------------------
# File:         time_zone.config
#
# Description:  ExifTool config file to return time zone from an image
#
# Notes:        The Composite:TimeZone tag defined here attempts to determine
#               the time zone for an image.  If possible, the camera time zone
#               is used, if that does not exist, the time zone for
#               DateTimeOriginal is returned, but if this can't be determined
#               then the time zone for CreateDate and then ModifyDate are used.
#               If this all does not result in a time zone, the timezone of the
#               TimeCreated tag is used
#
# Usage:        exiftool -config time_zone.config -timezone FILE
#
# Requires:     ExifTool version 7.74 or later
#
# Revisions:    2016/10/03 - P. Harvey Created
#               2016/12/12 - H. Baan Corrected tag name, added support for
#                            camera time zone/daylight savings info in
#                            MakerNotes
#               2017/01/05 - H. Baan Handle case were GPSDateStamp is missing
#               2017/01/06 - H. Baan Use QuickTime:CreationDate if available
#------------------------------------------------------------------------------

%Image::ExifTool::UserDefined = (
    'Image::ExifTool::Composite' => {
        TimeZone => {
            Desire => {
                0 => 'DateTimeOriginal',
                1 => 'CreateDate',
                2 => 'ModifyDate',
                3 => 'GPSDateStamp',
                4 => 'GPSTimeStamp',
                5 => 'OffsetTimeOriginal',  # (DateTimeOriginal)
                6 => 'OffsetTimeDigitized', # (CreateDate)
                7 => 'OffsetTime',          # (ModifyDate)
                8 => 'TimeZoneOffset',      # (ModifyDate)
                9 => 'MakerNotes:TimeZone',
                10 => 'MakerNotes:DaylightSavings',
                11 => 'QuickTime:CreationDate',
                12 => 'TimeCreated',
            },
            RawConv => q{
                return TimeZoneString($val[9] + ($val[10] ? 60 : 0)) if defined $val[9];
                return $val[5] if $val[5];
                return $val[6] if $val[6];
                return $val[7] if $val[7];
                if (defined $val[8]) {
                    my $tzh = $val[8];
                    $tzh =~ s/ .*//;
                    return TimeZoneString($tzh * 60);
                }
                if (defined $val[4]) {
                    my $loc = $val[0] || $val[1] || $val[2];
                    if ($loc) {
                        my @loc = split /[: ]/, $loc;
                        my @gmt = split /[: ]/, ($val[3]||"$loc[0]:$loc[1]:$loc[2]") . " $val[4]";
                        my $tzm = 15 * sprintf("%.0f", (GetTimeZone([@loc[5,4,3,2]], [@gmt[5,4,3,2]])) / 15);
                        $tzm -= 1440 if $tzm > 840;
                        $tzm += 1440 if $tzm < -720;
                        return TimeZoneString($tzm);
                    }
                }
                if (($val[11] && $val[11] =~ /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}([-+]\d{2})(\d{2})/) or
                    ($val[12] && $val[12] =~ /\d{6}([-+]\d{2})(\d{2})/))
                {
                    return TimeZoneString($1 * 60 + $2);
                }
                return undef;
            },
        },
    },
);

1;  #end
