Android Question Time difference based on latitude and longitude

khwarizmi

Active Member
Licensed User
Longtime User
Hi all

How can I use the GNSS library to obtain the time in another country, taking into account the time in my country based on latitude and longitude?
 

Computersmith64

Well-Known Member
Licensed User
Longtime User
Upvote 0

khwarizmi

Active Member
Licensed User
Longtime User
Thank you, Computersmith64 for help, but it seems that the existing solutions depend on the use of the Internet, and some users of my application have weak Internet connections for long periods of time, so I decided to calculate the time difference based on latitude and longitude, as each longitude represents a difference of 4 minutes. I want a solution based on this idea.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
OK - so if you know what the time is based on the current longitude of the device (from the device's clock & GPS position) & you know the relationship between longitude & time, you should be able to calculate the time at another longitude via something like:

Device time @ current_longitude +/- time_diff between current_longitude & desired_longitude

Or something... no? Without a more descriptive use-case it's hard to know what you're trying to achieve - but if it's simply that the user wants to figure out the time in a different location to where they currently are (& the have the lat/lon for the different location), that should work.

- Colin.
 
Last edited:
Upvote 0

khwarizmi

Active Member
Licensed User
Longtime User
I am afraid that there are factors other than the difference between longitude and latitude. The solution must be based on the time zone. I am looking for a way to find the time zone without referring to the Internet or the user’s device, only via GPS.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
Actually, that link I gave you has a heap of offline options too - maybe you didn't read down the list far enough.

I suppose you could just create a database with all the global timezones & their relative boundary coordinates. Out of interest, do you have any ideas yourself?

- Colin.
 
Upvote 0

derez

Expert
Licensed User
Longtime User
Long time ago I made a solution, but can't find it now.
Take a picture of world time zones, like this :

Clear all the legends so there are only colored areas.
Create a transformation from lat long to the pixel on the map.
Read the color of the pixel , use it as a key in a map object to get the time zone. You have to color some zones so there are no similar colors.
 
Upvote 0

RichardN

Well-Known Member
Licensed User
Longtime User
@khwarizmi The only thing you can calculate from Lat/Long is Local Mean Time (LMT) based on Longitude offset from UTC..... That is midday when the sun reaches it's zenith at any point on the earth. LMT is only useful for calculating Sunrise/Sunset and other nautical/celestial events.

Some countries have adopted local time offsets from GMT in one hour steps, some X hours and 30 mins, some X hours and 15 minutes. Many countries adopt Daylight Savings Time in winter, but the dates vary between countries. Some countries do not change the clocks at all. China, for instance, spans 60°+ of longitude which gives a LMT span of 4 hours, yet observes GMT+8 across the whole country all year round. (1 hour LMT change = 15° of longitude)

There is probably a database somewhere where you can query a Lat/Long position and find out what country you are in. You could compile a database of countries with their GMT offsets and DST changing dates but as countries vary their DST dates all the time, keeping the database maintained would be a nightmare!

I take your point about internet availability but to me the Google Time Zone API seems like the only workable solution.

Time Zone API
 
Upvote 0

khwarizmi

Active Member
Licensed User
Longtime User
I found some JSON files that could do the trick at that link, but if there are no exact matching values then rounding might be a problem, so I decided that a solution using sites that provide the time difference was a must.
 
Upvote 0

khwarizmi

Active Member
Licensed User
Longtime User
I've also found mathematical formulas that do a similar job.
 
Upvote 0

khwarizmi

Active Member
Licensed User
Longtime User
Certainly, but it seems that the only practical solution is to use ready-made online services.
 
Upvote 0

emexes

Expert
Licensed User
Long time ago I made a solution, but can't find it now.
Yeah, I remember a similar discussion, pretty sure in this forum, maybe a year or two ago. Will look for it now. Wish me luck. ?

Take a picture of world time zones, like this :
Effectively that map image is a visual table to translate latitude-longitude points to timezones, with about 230,000 entries.
 
Upvote 0

emexes

Expert
Licensed User
I hadn't even imagined these sorts of issues, but I did enjoy the bit about moving islands:
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
There is a github project (quite old now) that uses reverse-geolocation and a data file to determine the closest country to the supplied lat/lng coords.
It has 2 problems
1, The size of the data file is massive (1.6GB)
2, It doesn't tell you the timezone.

You would probably need to set a locality from the country code and determine the timezone offset from that.

Here's the link to the project reverse-geolocation
 
Last edited:
Upvote 0

emexes

Expert
Licensed User
The size of the data file is massive (1.6GB)

I was thinking that an 8-bit grayscale image where pixel value 128+x represented timezone +15x minutes would work, and when I found this image which is 2880 wide ie 360 degrees x 8 pixels/degree, I thought: this is looking good.

But it isn't quite usable. The vertical scale is different, the oceans are not "timezoned", and it is "polluted" with black text and lines which complicates getting the timezone information for those pixels.

Also it is solar-vs-local time offset rather than local-vs-zulu time offset, but that's not a deal-killer.

This image is 1.3 MB but by the time you got rid of the black and changed the rest from 24-bit RGB to 8-bit grayscale, I'm thinking it'd shrink to ~300 kB.



from www.reddit.com/r/MapPorn/comments/hsmc4a/time_zone_map_showing_each_regions_difference
 
Last edited:
Upvote 0

khwarizmi

Active Member
Licensed User
Longtime User
Thank you for enriching the discussion and for the valuable information, emexes
Thanks to all
From all of the above, I should use the services of ready-made specialized websites.
I have chosen the site: https://timezonedb.com/
Even if the user uses a VPN, the system will not be fooled, because the latitude and longitude values are not taken from the Internet, but rather using the GNSS library, knowing that my application relates to exams for students in different countries and they must sit for the electronic exam at the same time.
 
Last edited:
Upvote 0

emexes

Expert
Licensed User
sit for the electronic exam at the same time
It might be useful to bear in mind that the ticks Long value returned by DateTime.Now is the same everywhere around the world at the same time.


some users of my application have weak Internet connections for long periods of time
I'm not sure how your process could electronically enforce and audit the "exam at the same time" requirement, without an internet connection.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…