Monday, May 20, 2024
61
rated 0 times [  67] [ 6]  / answers: 1 / hits: 24485  / 12 Years ago, fri, september 7, 2012, 12:00:00

I set the time zone of our site on Heroku to Pacific Standard Time (PST) using:



heroku config:add TZ=America/Los_Angeles


Times for users are now always in PST--whether or not they are in the PST time zone.



What's the best way to get the user's actual time zone (i.e. the time zone of where they are physically located)?



I'm guessing that this can be solved using Rails (or Javascript?), as opposed to Heroku.


More From » ruby-on-rails

 Answers
35

There are two ways to do this.




  • Indeed, you can use javascript to fetch their current time/timezone. There is the possibility that the user's computer time is not set correctly, in which case the time zone you display will not be correct.



    Because you are using Rails, a recommended way is to get javascript already bundled as a gem, like detect_timezone_rails. This makes it easy to install (because it is all bundled automatically in the asset pipeline.


  • You can use the IP address to infer their country and time zone. The danger in this case is that a user may be using a proxy. Also, while the IP address generally has city-level resolution, it may be more or less accurate, which may in rare cases give the wrong time zone.



    Using the IP address, you can get their approximate city and latitude/longitude. There are many gems that can do this on Ruby Toolbox, eg. geocoder. With the latitude/longitude, you can get the time zone using a gem like timezone.




You can also use one of the above, and allow the user to manually change their time zone on your website (either storing this setting in a database if registered, or as a cookie on their browser). This is useful in case you got the timezone wrong.


[#83184] Thursday, September 6, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lucianod

Total Points: 667
Total Questions: 106
Total Answers: 92

Location: Jordan
Member since Thu, Aug 5, 2021
3 Years ago
lucianod questions
;