Rails timezones VS MySQL timezones
In my Rails (3.0) application, I use timezones using the helper time_zone_select(). It generates timezone names like "(GMT+01:00) Paris"...
But these names are different from those in MySQL, where the same timezone would be "Europe/Paris".
How can I convert Rails timezones to MySql ones ?
The tricky part is that for several reasons I have to use the default Rails helper, and in my models I have to use the MySQL timezones. So I can't use Ruby timezones in my models, and I can't use a list of MySQL timezones instead of the Rails helper... I really need a way to convert from the first to the latter.
Any idea?
Edit: I'm currently trying to use ActiveSupport::TimeZone[(ruby_timezone)].tzinfo.name, is there a开发者_Go百科 better way?
Okay, I finally found a way :
ActiveSupport::TimeZone[ruby_timezone].tzinfo.name
Romain suggested there may be some Rails timezones not supported by MySQL, so I tested the following in the console, and the answer is: they all work :)
ActiveSupport::TimeZone.all.each do |ruby_timezone|
mysql_timezone = ActiveSupport::TimeZone[ruby_timezone.name].tzinfo.name
puts ActiveRecord::Base.connection.execute("select convert_tz('2011-01-01 00:00:00', 'UTC', '#{mysql_timezone}')").first
end
end
If a timezone was not supported by MySQL, it would have returned nil.
I don't know if there is a better way, but at least it works :)
精彩评论