如何在Rails中获得utc偏移量?

时间:2023-02-13 15:06:45

I need the utc offset from the timezone specified.

我需要指定时区的utc偏移量。

7 个解决方案

#1


23  

require 'time'

p Time.zone_offset('EST') #=> -18000  #(-5*60*60)

#2


21  

If you need to take daylight savings time into account, you can use something like this:

如果你需要考虑夏令时,你可以使用以下方法:

Time.now.in_time_zone('America/New_York').utc_offset

#3


9  

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

#4


5  

If you have a ActiveSupport::TimeWithZone object, you can call time_zone.utc_offset on it. So, for example, Time.zone.now.time_zone.utc_offset.

如果您有一个ActiveSupport::TimeWithZone对象,您可以调用time_zone。utc_offset。例如,Time.zone.now.time_zone.utc_offset。

EDIT: You can also use the gmt_offset instance method on normal Time objects.

编辑:您还可以在常规时间对象上使用gmt_offset实例方法。

#5


4  

If you are storing a user's timezone as a Time.zone.name string such as 'Eastern Time (US & Canada)' as outlined in http://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html and want to get the timezone offset you can do this:

如果您将用户的时区存储为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

#6


3  

I use Time.zone.utc_offset / 1.hour to get the offset in hours (e.g.: -8 for San Francisco)

我使用Time.zone。utc_offset / 1。以小时计算的偏移量(例如:旧金山的-8)

#7


2  

Just to complete the loop here, right now I use:

为了完成这个循环,现在我用:

@course.start.utc_offset/60/60 in order to get the # of hours needed. Jquery countdown needs just -5 or +3 in their timezone argument.

@course.start。utc_offset/60/60,以便获得所需的小时数。Jquery倒计时在时区参数中只需要-5或+3。

Your welcome google.

你欢迎谷歌。

#1


23  

require 'time'

p Time.zone_offset('EST') #=> -18000  #(-5*60*60)

#2


21  

If you need to take daylight savings time into account, you can use something like this:

如果你需要考虑夏令时,你可以使用以下方法:

Time.now.in_time_zone('America/New_York').utc_offset

#3


9  

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

#4


5  

If you have a ActiveSupport::TimeWithZone object, you can call time_zone.utc_offset on it. So, for example, Time.zone.now.time_zone.utc_offset.

如果您有一个ActiveSupport::TimeWithZone对象,您可以调用time_zone。utc_offset。例如,Time.zone.now.time_zone.utc_offset。

EDIT: You can also use the gmt_offset instance method on normal Time objects.

编辑:您还可以在常规时间对象上使用gmt_offset实例方法。

#5


4  

If you are storing a user's timezone as a Time.zone.name string such as 'Eastern Time (US & Canada)' as outlined in http://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html and want to get the timezone offset you can do this:

如果您将用户的时区存储为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

#6


3  

I use Time.zone.utc_offset / 1.hour to get the offset in hours (e.g.: -8 for San Francisco)

我使用Time.zone。utc_offset / 1。以小时计算的偏移量(例如:旧金山的-8)

#7


2  

Just to complete the loop here, right now I use:

为了完成这个循环,现在我用:

@course.start.utc_offset/60/60 in order to get the # of hours needed. Jquery countdown needs just -5 or +3 in their timezone argument.

@course.start。utc_offset/60/60,以便获得所需的小时数。Jquery倒计时在时区参数中只需要-5或+3。

Your welcome google.

你欢迎谷歌。