Rails 3 Custom Time Zones

In Rails 2.x, I did not need to set any time zone information, and the user, no matter what time zone they were in, would get the time specified by the user in their OS.

Now in Rails 3 everything is displayed in UTC. Is it possible to return this behavior to default without having to insert some js-hacks to detect the user's time zone?

Thank!

Chris

+3
timezone ruby-on-rails
May 14 '11 at 17:16
source share
2 answers

I found that if you add localtime to your string, you will get the correct date time ...

Invoice.last.created_at.localtime 
+6
May 24 '11 at 3:48
source share

An easy way to find the time zone from an IP address:

  require 'open-uri' def get_time_zone_from_ip(ip) # get external IP of rails server if running on localhost ip = open("http://ifconfig.me/ip").string.strip if ip == "127.0.0.1" location = open("http://api.hostip.info/get_html.php?ip=#{ip}&position=true") if location.string =~ /Latitude: (.+?)\nLongitude: (.+?)\n/ json = open("http://ws.geonames.org/timezoneJSON?lat=#{$1}&lng=#{$2}") geo = ActiveSupport::JSON.decode(json) return ActiveSupport::TimeZone::MAPPING.index(geo["timezoneId"]) unless geo.empty? end end 

GeoKit seems to be a more reliable option for obtaining coordinates if you want to find the coordinates of a mailing address.

http://geokit.rubyforge.org/index.html

0
Nov 15 2018-11-11T00:
source share



All Articles