views:

457

answers:

4

Hi, I want to write app where user can point any place on map (not only cities) and get timezone in that place.

What data structure (app will not have Internet connectivity) and algorithm should I use? Where I can obtain needed data (I wont more accuracy then divining map into 24 rectangles)?

I will write my app in Java ME.

+3  A: 

There are a number of web services that can do this for you (for example GeoNames has a great API). But if you don't have an Internet connection, then that's not something you're going to find directly in Java ME's standard libraries.

You could do something close, though: Store the coordinates of the cities corresponding to each time zone, then do a Voronoi tessellation so that you have the areas which are closest to each city. Then, when your users click on a particular geographic area, you simply map that point to the right section of the tessellation, and presto -- you've got the nearest city, which in turn determines the right time zone.

More sophisticated approaches are possible, but they also require significantly larger memory structures, which I assume is a constraint if you are running Java ME. This is a good compromise between space and speed.

John Feminella
But be a bit careful, there are, for example, places in northern France closer to London than to Paris and places in Scotland closer to Stavanger than to Edinburgh, so the nearest city approach doesn't guarantee the right answer.
High Performance Mark
Right. The key would be to only have a representative city for each country/time-zone. For example, you don't need six cities to represent France, just one (e.g. Paris) for the time zone it's in (Central European Time). That increases the size of each Voronoi region and raises the chance of a correct answer.
John Feminella
But a polygon containing Paris and the rest of France will not be the Voronoi region of Paris. Sure, use a polygonal representation for each time zone (or each country) but they won't be Voronoi regions.
High Performance Mark
+6  A: 

Given that time zones are based on political entities rather than simply a physical lat/lon computation, I would create a data structure that mapped polygons over lat/lon coordinates into political entities (country and province/state) and then have a separate structure that mapped political entities and current date into timezone offset.

That way you not only avoid redundancy, but also:

  1. You can display DST reference information independently of a specific set of coordinates, and
  2. When some country changes the rules for when daylight saving time begins and ends, you have one place to make the update.

However, given the highly irregular shape of some borders, you'll need a fairly large data structure for accuracy, depending on the resolution of your input and/or display.

joel.neely
Thanks a lot. I think this is correct direction. I think the hardest element will be extract timezones data from GiS some system and convert it to usable format.
Maciek Sawicki
A: 

H

Well, if accuracy is not a requirement, why bother with a data structure ? Write a function which, given a longitude, returns the offset, expressed in hours, from the Greenwich meridian.

And if this doesn't work for you, I'd go with Joel Neely's answer.

Regards

Mark

High Performance Mark
Thank You. I would like to take account of national boundaries, so I prefer Joel Neely's answer.
Maciek Sawicki
+1  A: 

Joel Neely's answer is good, but be aware this is a really tricky problem for political reasons. So for disputed areas like Kashmir or Tibet you could offend people by the decision you make.

Also, if you want to then use timezone information to compute time changes it gets even trickier, as decisions about whether Daylight saving time is used, and the date it changes can change with only 2 weeks notice. See: http://www.timeanddate.com/news/time/argentina-dst-2009-2010.html

The polygon information can be bought at http://www.worldtimeserver.com/time_zone_guide/ if you are interested. Disclaimer - I haven't bought this information, so don't know how good it is.

Nick Fortescue