Created
March 9, 2012 01:50
-
-
Save skout23/2004571 to your computer and use it in GitHub Desktop.
simple silly daemon to set the tz based on current geoip osx only atm, but easy enough to switch up for other os's
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'geoip' | |
# install rubygems and geoip gem | |
# grab geoip db before | |
# | |
# wget -N -q http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz | |
# gunzip GeoLiteCity.dat.gz in same folder as this script | |
# needs to be run by root or a passwordless sudoer. (I know I know stupid stupid) | |
$GEOIPDB = "GeoLiteCity.dat" | |
# wait time of 2 hours | |
$INTERVAL = (60 * 60 * 2) | |
def get_current_tz | |
begin | |
current_ip = %x|curl -s whatismyip.org| | |
timezone = GeoIP.new($GEOIPDB).timezone(current_ip).to_s | |
return timezone | |
rescue | |
sleep $INTERVAL | |
retry | |
end | |
end | |
def set_current_tz(timezone) | |
begin | |
tz_results = %x|sudo systemsetup -settimezone #{timezone}| | |
return true | |
rescue | |
puts "debug if need to here" | |
exit | |
end | |
end | |
while true | |
tz = get_current_tz | |
set_current_tz(tz) | |
sleep $INTERVAL | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fairly sure I could do this in a bash oneliner too if pressed, but shrug