如何按整数值更改时区?

How to change timezone by integer value?

我当前的 Time 对象看起来像这样:

2015-01-10 17:13:00.000000000 +0000

虽然我需要它看起来像这样:

2015-01-10 18:13:00.000000000 +0100

我只想设置一个时区偏移值来接收它,而不知道字符串中的时区名称,例如:

my_date.set_timezone_offset(1)

我该怎么做?

DateTime.now.change(offset: "+0100")

Returns 具有提供的偏移量的日期时间

因此,要根据给定的偏移量更改时间,您必须使用 new_offset 方法,它是 DateTime 的一部分(不确定这是否适用于此 Time 对象,如果不是你可以尝试解析它):

time = "2015-01-10 17:13:00.000000000 +0000".to_datetime</pre>
然后使用方法:

time.new_offset("+10:00")</pre>

这将 return 具有提供的偏移量的 DateTime 也会根据该偏移量更改时间。