Snippet: Get the timezone for a geo location with Groovy and Google

I have a list (table) of airport locations, but missing the timezone information (as used in Java, eg. ‘America/Los_Angeles’ which is the timezone Id in java.util.Timezone). We could use the geonames dump to retrieve the timezone from the cityname, but the cityname for some airports might not be unique or distinct, so I rather use the geographical location which I have for each airport and use a webservice to get the timzone for a specific location. We can use the geonames webservice (commercial usage allowed, but you should give credit) or the Google Timezone API (which is experimental and restricted to 2500 calls a day) .

We use Groovy to retrieve this information, using the browser you get this JSON reply for :
https://maps.googleapis.com/maps/api/timezone/json?location=39.6034810,-119.6822510&timestamp=1331161200&sensor=false

JSON

We put this into a Groovy Script to read the timeZoneId:

#!/usr/bin/env groovy
import groovyx.net.http.*
import static groovyx.net.http.ContentType.*

def timezone = new RESTClient('https://maps.googleapis.com/maps/api/timezone/json?location=39.6034810,-119.6822510&timestamp=1331161200&sensor=false')
def resp = timezone.get( contentType : JSON)

println resp.data.timeZoneName + " - " + resp.data.timeZoneId

We need the HTTP Builder library.
Please note the httpbuilder library has some dependencise ! Put all into your .groovy/lib folder.

  • commons-beanutils-1.8.3.jar
  • commons-codec-1.6.jar
  • commons-collections-3.2.1.jar
  • commons-lang3-3.1.jar
  • commons-logging-1.1.1.jar
  • fluent-hc-4.2.2.jar
  • http-builder-0.6.jar
  • httpclient-4.2.2.jar
  • httpclient-cache-4.2.2.jar
  • httpcore-4.2.2.jar
  • httpmime-4.2.2.jar
  • json-lib-2.4-jdk15.jar
  • xml-resolver-1.2.jar

Result:

Pacific Standard Time - America/Los_Angeles

Now we can iterate through our locations and complete the data.

Alternative with geonames:

geonames JSON

#!/usr/bin/env groovy
import groovyx.net.http.*
import static groovyx.net.http.ContentType.*

def timezone2 = new RESTClient('http://api.geonames.org/timezoneJSON?lat=47.01&lng=10.2&username=xxxxx')
def resp2 = timezone2.get( contentType : JSON)
println resp2.data.countryName + " - " + resp2.data.timezoneId
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s