Search code examples
perltimezonecoordinates

Look up time zone from latitude and longitude using Perl


I'm looking for a way to use Perl to get the time zone corresponding to a given latitude and longitude. Any program that runs on Linux would be okay too.

I have pictures with time in UTC and GPS coordinates and I want to use them on a website with the local time and date.

I found the Geo::Location::TimeZone module on CPAN which looks like it could do the job, but it doesn't work and I'm still trying to figure out why.


Solution

  • Depending on the amount of lookups you need to do and the terms of use, the Google Time Zone service looks trivial to use.

    use strict;
    use warnings;
    
    use LWP::Simple;
    use JSON::Any;
    
    my ($lat, $long) = (39.6034810,-119.6822510);
    my $apikey = 'whatever';
    my $time = time();
    
    my $location = JSON::Any->jsonToObj(get("https://maps.googleapis.com/maps/api/timezone/json?location=$lat,$long&timestamp=$time&sensor=false&key=$apikey"));
    say $location->{timeZoneId};    # America/Los_Angeles
    say $location->{timeZoneName};  # Pacific Standard Time