ActiveSupport::TimeZone list of strings for offset

Shauno picture Shauno · Apr 9, 2013 · Viewed 8.2k times · Source

I've followed Ryan Bate's railscast #106 http://railscasts.com/episodes/106-time-zones-revised which adds a time_zone string to the User model. The string is from the dropdown:

= f.time_zone_select :time_zone, ActiveSupport::TimeZone.us_zones

I am trying to query all of the Users that share a common time zone offset (so I can send them an email at a particular time everyday).

User.where(:time_zone => ['Guadalajara','Central America','Mexico City', 'Monterrey', 'Saskatchewan'])

I have not been able to get a list of the time zone names for a particular offset. I've been tinkering with TzInfo and ActiveSupport::TimeZone.zones_map with limited success. I feel like the zones_map should be able to satisfy my needs, but I haven't been able to get the data I want from it (although I see it in there!).

Am I taking the wrong approach here? I was thinking I could store the GMT offset on the user instead, and query that way?

My google-fu has let me down on this one.

Answer

Shauno picture Shauno · Apr 9, 2013

i've figured it out --

tz_names = ActiveSupport::TimeZone.zones_map.values.collect{|z| z.name if z.utc_offset == offset}.compact