When should `DateTime.now.utc` vs. `Time.current.utc` be used in Rails?

后端 未结 1 1909
花落未央
花落未央 2020-12-09 17:13

In Rails, I\'m a little confused on the guidance between when to use DateTime.now.utc and Time.current. There seem to be differing opinions inside the framework about which

1条回答
  •  天命终不由人
    2020-12-09 17:40

    I think you should use .current instead of .now.

    The difference of .current and .now is .now use the server's timezone, while .current use what the Rails environment is set to. If it's not set, then .current will be same as .now.

    Time.current

    Returns Time.zone.now when Time.zone or config.time_zone are set, otherwise just returns Time.now.

    Datetime.current

    Returns Time.zone.now.to_datetime when Time.zone or config.time_zone are set, otherwise returns Time.now.to_datetime.

    0 讨论(0)
提交回复
热议问题