Ruby 午夜 UTC 的时间对象

Ruby Time object for midnight UTC

我需要能够在 Rails 中创建 UTC 时间对象,但无论我尝试什么,它最终都会成为本地时间对象 converted 到 UTC。

application.rb

config.time_zone = "UTC"

例子
我尝试为 2017 年新年的午夜创建一个时间对象:

Time.new(year, month, 1, 0, 0, 0)

=> 2017-03-01 00:00:00 -0500

Time.new(year, month, 1, 0, 0, 0).in_time_zone

=> 2017 年 3 月 1 日,星期三 05:00:00 UTC +00:00

Time.new(year, month, 1, 0, 0, 0).in_time_zone('UTC')

=> 2017 年 3 月 1 日,星期三 05:00:00 UTC +00:00

我认为以下可能会满足您的要求:

Time.new(year, month, 1, 0, 0, 0, "+00:00")

the documentation for more details.

您应该可以使用:

Time.utc(year, month, 1, 0, 0, 0)

或者只是

Time.utc(year, month, 1)