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.
We must start by finding the difference in longitude (or degrees) of the two places. We do this by adding the two numbers. Then, divide by the 15 degrees that occurs in one hour and this will give you the time difference between two locations through the International Date Line.
To find the time zone in hours of a particular location, you can take the longitude -- in degrees -- and divide it by 15. So, for example, 75° E would be 75/15 which equals 5. That translates to the time zone being 5 hours ahead of UTC or GMT time, which can also be labeled as UTC+5. Let's do another one.
The world is divided into 24 time zones. The course of one day is broken down to the seconds and calculated to define the correct time of a particular place. However, it is not that easy. The 24 time zones, created in accordance to each hour of the day, are theoretically drawn vertically like longitudes over the globe.
The rotation of the Earth means that time zones are dictated by the lines of longitude connecting the two poles.
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:
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With