5

Is it possible to use the same tool to have Ubuntu guess what timezone it is currently in?

This article mentions the feature I'm looking for (just above the second pic): http://www.linuxforu.com/2012/05/ubuntu-12-04-precise-pangolin-review/

3
  • Unfortunately, only one of those questions had an actionable/appropriate answer. I guess 64 is better than 55, but I can select answers just for the sake of improving that.
    – Bryan Agee
    Jun 22, 2012 at 8:16
  • 1
    Many thanks, Brian. When you have a chance, could you perhaps add comments to those questions/answers as to whats missing, increasing the chances that a good answer will show up in the future?
    – ish
    Jun 22, 2012 at 8:20
  • Thanks for the tips--as I have time, I'll go through and update them.
    – Bryan Agee
    Jun 22, 2012 at 9:06

3 Answers 3

6

Ubuntu uses the ubuntu-geoip package, which queries http://geoip.ubuntu.com/.

You can python-geoip to achieve the same thing:

import GeoIP
geo = GeoIP.new(GeoIP.GEOIP_MEMORY_CACHE)
ip = '200.100.50.0'
print geo.country_code_by_addr(ip)

Sadly, it doesn't work with ipv6 yet.

2
  • I was really hoping that someone might know where that bit of code lives, and if it might be called again....
    – Bryan Agee
    Jun 22, 2012 at 9:07
  • 3
    I found the exact query to get xml: wget -O - -q http://geoip.ubuntu.com/lookup
    – Bryan Agee
    Jun 22, 2012 at 9:28
2

I decided to download the ubiquity source and dig for it. Greping timezone found the concerned lines, which issue the command:

wget -O - -q http://geoip.ubuntu.com/lookup

and parse the resulting xml.

Rather than load up python or something else, I just wrote a quick bash script that grabs the xml and yanks the timezone out with sed:

#!/bin/bash

# Fetch timezone from Ubuntu's geoip server
TZ=`wget -O - -q http://geoip.ubuntu.com/lookup | sed -n -e 's/.*<TimeZone>\(.*\)<\/TimeZone>.*/\1/p'`

# Set the current time
cp /usr/share/zoneinfo/$TZ /etc/localtime
3
  • Will that work? I thought /etc/localtime was a binary file? Sure you don't need to do this: Feb 3, 2014 at 8:13
  • Yep; the above code works like a charm, and has been in production for a couple years now.
    – Bryan Agee
    Aug 7, 2014 at 2:13
  • I'm in Melbourne, Australia, and doing this query from my IP address gives me CountryCode=AU, CountryName=Australia, TimeZone=None. So it deduces the country successfully, but not the timezone within the country. Oct 12, 2015 at 6:38
0

I believe that it uses your internet connection & ip address to find that information. When I have installed offline it always asks you to select a timezone.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .