Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby时区偏移问题_Ruby_Timezone - Fatal编程技术网

Ruby时区偏移问题

Ruby时区偏移问题,ruby,timezone,Ruby,Timezone,我需要为某个时间分配一个时区偏移量,以获取指定偏移量的一周中的当前日期。 这不适用于rails,因此我需要一个纯Ruby格式化程序/解析器来完成这项工作 谢谢。返回时区和UTC之间的偏移量(以秒为单位) t = Time.gm(2000,1,1,20,15,1) #=> 2000-01-01 20:15:01 UTC t.gmt_offset #=> 0 l = t.getlocal #=&

我需要为某个时间分配一个时区偏移量,以获取指定偏移量的一周中的当前日期。 这不适用于rails,因此我需要一个纯Ruby格式化程序/解析器来完成这项工作


谢谢。

返回时区和UTC之间的偏移量(以秒为单位)

   t = Time.gm(2000,1,1,20,15,1)   #=> 2000-01-01 20:15:01 UTC
   t.gmt_offset                    #=> 0
   l = t.getlocal                  #=> 2000-01-01 14:15:01 -0600
   l.gmt_offset                    #=> -21600
这就是我发现的:

require 'date'
local = DateTime.now 
new_offset = Rational(0, 24) #put the offset you want as first argument
utc = local.new_offset(new_offset)

但我如何将这次转换为例如偏移量+7?我的问题是我想得到一周中特定偏移量(明确指定)的一天,而不是UTC或机器本地时间。那么,在创建日期/时间对象时,如何分配时区偏移?
#As a string
t = Time.new(2011,6,27,14,10,0, "+07:00")
# or in seconds from UTC
t = Time.new(2011,6,27,14,10,0, 7*60*60)