Skip to content

Instantly share code, notes, and snippets.

@skout23
Created March 9, 2012 01:50
Show Gist options
  • Save skout23/2004571 to your computer and use it in GitHub Desktop.
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
#!/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
@skout23
Copy link
Author

skout23 commented Mar 9, 2012

Fairly sure I could do this in a bash oneliner too if pressed, but shrug

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment