Setting timezone from IP address

Since your external IP address shows where in the world you, although I haven't yet worked how you can tell, can this be used in some way to set your timezone?
 
Geolocating from IP address is very unreliable. It depends on cooperation from your ISP. Depending on whether your ISP is big or small, that in turn requires a whole chain of their feeders. For example, we are located in the hills behind the small town of Los Gatos, CA (southwest corner of Silicon Valley). With our old ISP, we would get geolocated into Saratoga, CA (about 20 miles further north). With the phone company being our new ISP, we either get geolocated into Morgan Hill (40 miles), Livermore (80 miles), or somewhere in Southern California (400 miles away).

At least all these addresses are in the same time zone (because the west coast of the american continent has a giant time zone). There are databases of the geographic borders of time zones, but I don't know where to find them.

Also, this can never be 100% reliable, because there are many cases where small territories (counties in the US, small countries in the world) are in a different time zone from surrounding areas, and geolocation is just not accurate enough. Some people also choose to deliberately be in a different time zone. For example, I know of a big company that runs all of its servers in the timezone of the headquarters location.
 
But in theory it should be possible for anyone who can determine which country they are living in where there aren't too many times zones. So the steps involved would be:-

1. Find your external IP address.

2. Find the location of the IP address - country should be sufficient.

3. Lookup country in /usr/share/zoneinfo/zone.tab to get $CONTINENT/$CITY

4 ln -s /usr/share/zoneinfo/$CONTINENT/$CITY /etc/localhost

Not sure how to do (1) and (2) programatically....

Also, sometimes I'm in the UK and sometimes GB which makes life confusing.
 
1. fetch -qo - https://www.myexternalip.com/raw

2. use my sysutils/ipdbtools - see also ipdbtools(1):
pkg install ipdbtools
ipdb-update.sh ftp.ripe.net

ipup 62.157.175.7062.157.175.70 in 62.153.0.0 - 62.159.255.255 in DE
ipup 62.157.175.70 | cut -s -w -f 7DE
 
Code:
whois `fetch -qo - https://www.myexternalip.com/raw` | grep country | awk '{print $2}'

Nearly there....
 
1. Find your external IP address.
That only works in the simple case of a machine that has a single IP connection. While the bulk of all desktop machines are configured that way, it doesn't have to be true. Many servers have multiple interfaces, and multiple IP addresses. Sometimes even more IP addresses than interfaces. Some machines have no outgoing interfaces at all (they are air gapped), or have multiple.

In my previous employer, all our internal machines were proxied. We had at least two outgoing proxies, one in California, one in New York, with a very fast internal network connecting them, and the two of them were load-balanced (so most of the time I used the California one, but if it was down or overloaded, my traffic went via New York). So if you tried to determine your internal IP address, you could get wrong by 3 hours of time zone difference.

But for the simple cases where a machine has only one outbound connection and no proxy, the simplest method is indeed what obsigna posted: There are many web servers that will read your IP address back to you. If you have a web server somewhere, this is trivial to implement, with a 3-line CGI script in python: just print out "os.environ[remote_addr]".

So the programmatic way is to query a server that reads back your address to you.

2. Find the location of the IP address - country should be sufficient.
In some cases that would work by geolocating (from the latitude-longitude coordinate). If you search for "geolocating IP address", there are many services that do that for free. You would have to fetch their page with your IP address from step one programmatically (easy, can be done with fetch or wget), then parse and decode the HTML output (can be done with a HTML DOM parser, I know they are available in python). It would probably take an experienced programmer a few hours to code this so its reliable enough to be production worthy.

The other option is what ShelLuser and obsigna posted: instead of using geolocation, use the registration information of your IP address, through whois.


Also, sometimes I'm in the UK and sometimes GB which makes life confusing.
And those are the example of the special cases that would either need a lot of programming, or have the user check the result and override. What if you are on the Island of Man? How about Gibraltar? Those are part of the UK or of GB, or perhaps not. And they are either in the same time zone or not.
 
Code:
whois `fetch -qo - https://www.myexternalip.com/raw` | grep country | awk '{print $2}'

Nearly there....

Code:
IP=`fetch -qo - https://www.myexternalip.com/raw`
COUNTRY=`whois $IP | grep country | awk '{print $2}'`
ZONE=`grep $COUNTRY zone.tab | awk '{print $3}'`
echo ln -s /usr/share/zoneinfo/$ZONE /etc/localhost

:)
 
And those are the example of the special cases that would either need a lot of programming, or have the user check the result and override. What if you are on the Island of Man? How about Gibraltar? Those are part of the UK or of GB, or perhaps not. And they are either in the same time zone or not.


It was only meant to be an intellectual exercise to see if it could be done... It may come in handy for someone travelling around Europe who might want to have the correct time on their laptop. It's not a serious exercise for a Megacorp.

As far being in the UK goes, I was born in England, but that country now only exists in the sports arena. In Internet terms it sometimes goes by the ID of UK as in TLD, and sometimes as GB as in Timezones.... very confusing for someone who lives here. As for Gibraltar , that has its own Country Code - GI. And is the Isle of Man in the UK? You tell me .
 
Geolocating from IP address is very unreliable.

ralphbsz nailed it. And please keep in mind that ISPs sometimes reallocate their addressing to help achieve various things including route summarization and efficient usage. So you're address can change from time to time. Yeah the geo-updates are usually done around the same time, but also keep in mind, many ISPs associate blocks of addressing to a building or region of buildings. That building could be 50 KMs from you, or depending on their design, could be just on the other side of a time zone if you are unlucky enough. All of these points are debatable, but the end result for many a user is simply: unreliable.

But I have to ask: what problem does this fix for you? If setting your machine by defined time zones works so well, why use something that runs the risk of being unreliable. And if you are an international traveler, then use GMT time zone. ;)
 
Back
Top