Ruby on rails 如何将UTC时间转换为rails 4中配置的时区
我从服务器收到一个时间字符串,我想将该字符串解析或转换为配置的时区(在我的例子中为东京时区):Ruby on rails 如何将UTC时间转换为rails 4中配置的时区,ruby-on-rails,ruby,time,Ruby On Rails,Ruby,Time,我从服务器收到一个时间字符串,我想将该字符串解析或转换为配置的时区(在我的例子中为东京时区): 但它会返回意外的输出:Fri,2016年5月27日09:00:00 JST+09:00如果解析的日期时间是UTC时间,在解析之前显式添加UTC时区: Fri, 27 May 2016 09:00:00 JST +09:00 # this parses the time as local time: Time.zone.parse("2016-05-27T09:00:00.0000000")
- 但它会返回意外的输出:
如果解析的日期时间是UTC时间,在解析之前显式添加UTC时区:Fri,2016年5月27日09:00:00 JST+09:00
Fri, 27 May 2016 09:00:00 JST +09:00
请注意datetime字符串后面的“Z”,这意味着这是UTC时区中的datetime。您已保存了我的日期(y)# this parses the time as local time: Time.zone.parse("2016-05-27T09:00:00.0000000") # => Fri, 27 May 2016 09:00:00 JST +09:00 # this parses the time as UTC and converts to local time: Time.zone.parse("2016-05-27T09:00:00.0000000Z") # => Fri, 27 May 2016 18:00:00 JST +09:00
Time.zone.parse("2016-05-27T09:00:00.0000000")
Fri, 27 May 2016 09:00:00 JST +09:00
# this parses the time as local time: Time.zone.parse("2016-05-27T09:00:00.0000000") # => Fri, 27 May 2016 09:00:00 JST +09:00 # this parses the time as UTC and converts to local time: Time.zone.parse("2016-05-27T09:00:00.0000000Z") # => Fri, 27 May 2016 18:00:00 JST +09:00