0

I have a RoR application where I store locations using gmaps4rails. Based on the address of the location, I'd like to guess the UTC offset ("+00:00", "-05:00", ..). Is it possible or I have to ask users (and trust them) to set the correct timezone offset?

By the way, I know that there are some places where the timezone offset may change during the year. In this case, I should use the zone name instead of the offset. Is there a gem that helps to deal with these issues?

Thank you,

Mahdi
  • 75
  • 1
  • 13
  • 1
    Have you checked [timezone gem](https://github.com/panthomakos/timezone) ? – Yevgeniy Anfilofyev Aug 26 '13 at 12:07
  • What kind of address do you have? IP addresses? Coordinates? Postal addresses? – sawa Aug 26 '13 at 12:09
  • I'm not sure about how to extract timezone from address but to convert time to different timezone; rails has method `in_time_zone`. So you could do like `Time.now.in_time_zone('Tokyo')` – j03w Aug 26 '13 at 12:13
  • possible duplicate of [How to get a time zone from a location?](http://stackoverflow.com/questions/16086962/how-to-get-a-time-zone-from-a-location) – Matt Johnson-Pint Aug 26 '13 at 12:56
  • Thank you Matt for the link, it is very useful. I've tried the google_timezone gem which asks Google Timezone Web Service and it answers to all my needs. The addresses I use are latitude/longitude. `in_time_zone` will be helpful too. – Mahdi Sep 01 '13 at 10:35

2 Answers2

0

Geolocate the address to lat/lon coordinates using any number of web services for this. Then see the options in How to get a time zone from a location using latitude and longitude coordinates? The Ruby gem that Yevgeniy mentioned is listed there, along with other options.

Don't try to get an offset for the location - it could change periodically for DST. Get the time zone instead, such as Asia/Singapore or America/New_York. Then use that to determine the offset for a particular point in time.

See "Time Zone != Offset" in the timezone tag wiki.

Community
  • 1
  • 1
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
-2

Even if you get the solution, it is better to ask them from user. If user in the US wants to see his/her datetime according to timezone in Singapore (because that is his/her homeown), he/she should be shown it like that.

techvineet
  • 5,041
  • 2
  • 30
  • 28
  • Who said it was for the user? He asked how to get the time zone of a location. Perhaps he just wants to know what time it is at a customer's location based on their address. The use case is valid. – Matt Johnson-Pint Aug 26 '13 at 14:02
  • The address I store is a place where the user and his/her friends should meet. So, the best solution in this case is to use the local time. – Mahdi Sep 01 '13 at 10:32
  • @techvineet, your alternative is interesting but it does not really answer the question. Thank you for your comment. – Mahdi Sep 02 '13 at 22:19
  • @techvineet, sorry for you but I don't have enough reputation to vote down! – Mahdi Sep 10 '13 at 20:05