开发者

Display UTC based on known timezone

开发者 https://www.devze.com 2023-02-26 01:16 出处:网络
If I 开发者_StackOverflow社区know the Time.zone e.g. London, any ideas on how I can display (GMT+00:00) London

If I 开发者_StackOverflow社区know the Time.zone e.g. London, any ideas on how I can display

(GMT+00:00) London

without assigning

Time.zone = "London"

and then calling

puts Time.zone

Thanks in advance!


You can call:

Time.zone.formatted_offset

Which will:

returns the offset of this time zone as a formatted string, of the format “+HH:MM”.

per: http://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html#method-i-formatted_offset

So if you wanted to format the string, you could do:

"GMT(#{Time.zone.formatted_offset})"


The to_s method provides exactly this output.

Time.zone.to_s
# => "(GMT+00:00) London"
0

精彩评论

暂无评论...
验证码 换一张
取 消