如何将时区全名转换为 tz 缩写? [英] How to translate timezone full names to tz abbreviations?
本文介绍了如何将时区全名转换为 tz 缩写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 Rails 3.x 应用程序中,我需要显示时区缩写(EST、PST、CST 等)而不是完整的时区名称.我已经看到许多讨论似乎解决了这个问题,但方式过于冗长.
In a Rails 3.x app I need to display time zone abbreviations (EST, PST, CST, etc.) rather than the full time zone name. I've seen a number of discussions that seem to address the issue, but in overly verbose manners.
是否有可以用来正确映射它们的 gem 或非常简洁的方法来处理这个问题?
推荐答案
对于当前时区:
Time.zone.now.strftime('%Z')
或获取时区列表:
ActiveSupport::TimeZone.us_zones.map do |zone|
Time.now.in_time_zone(zone).strftime('%Z')
end
它有效,但似乎仍然应该有更好的方法......
It works, but it still seems like there should be a better way...
这篇关于如何将时区全名转换为 tz 缩写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文