This seems to be doing the trick for me (for most of the timezones I've tested).
All the code I put here are methods in the controller (in my case, in ApplicationController
).
def request_location
if Rails.env.test? || Rails.env.development?
Geocoder.search("your.public.ip.here").first
else
request.location
end
end
def get_time_zone
time_zone = request_location.data["time_zone"]
return ActiveSupport::TimeZone::MAPPING.key(time_zone) || "UTC"
end
Of course, you should substitute your.public.ip.here
for your actual public ip, or something similar. I put an IP here so that the response that Geocoder gives has the same format as the one from the request.
I'm happy to hear comments on the code.