ste*_*lag 31
require 'time'
p Time.zone_offset('EST') #=> -18000 #(-5*60*60)
Run Code Online (Sandbox Code Playgroud)
lee*_*lee 31
如果您需要考虑夏令时,可以使用以下内容:
Time.now.in_time_zone('America/New_York').utc_offset
Run Code Online (Sandbox Code Playgroud)
col*_*ick 13
Time.now.in_time_zone('Europe/London').formatted_offset
# => "+01:00"
Run Code Online (Sandbox Code Playgroud)
对于一个可以说更有用的字符串......
my_date_time = DateTime.new(2011, 3, 29, 20)
#=> Tue, 29 Mar 2011 20:00:00 +0000
#will return the same time but with another offset
my_date_time.change(:offset => "+0100")
#=> Tue, 29 Mar 2011 20:00:00 +0100
#will return time for other offset
my_date_time.in_time_zone(-1)
#=> Tue, 29 Mar 2011 19:00:00 -0100
Run Code Online (Sandbox Code Playgroud)
如果你有一个ActiveSupport::TimeWithZone对象,你可以调用time_zone.utc_offset它.所以,例如,Time.zone.now.time_zone.utc_offset.
编辑:您还可以gmt_offset在普通Time对象上使用实例方法.
如果您将用户的时区存储为Time.zone.name字符串,如http://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html中'Eastern Time (US & Canada)'所述,并且想要获取时区偏移量,则可以执行以下操作:
ActiveSupport::TimeZone.new(user.time_zone).utc_offset / 3600
# => -5
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
32290 次 |
| 最近记录: |