Skip to content

Instantly share code, notes, and snippets.

@dpk
Created July 5, 2012 20:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dpk/3056263 to your computer and use it in GitHub Desktop.
Save dpk/3056263 to your computer and use it in GitHub Desktop.
Current time in location of IP address.
#!/usr/bin/env ruby
require 'geoip'
require 'tzinfo'
geoip = GeoIP.new('GeoLiteCity.dat')
location = geoip.city ARGV[0]
if location.nil?
puts Time.now.utc.to_datetime.iso8601
exit 100
end
tz = TZInfo::Timezone.get location.timezone
offset = tz.current_period.utc_total_offset
absoff = offset.abs / 60
pn = (offset < 0 ? '-' : '+')
hoff = absoff / 60
moff = absoff % 60
ctime = tz.current_time_and_period.first
puts DateTime.new(ctime.year, ctime.month, ctime.day, ctime.hour, ctime.min, ctime.sec, "#{pn}#{hoff}:#{moff}").iso8601
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment