【问题标题】:Rails compare Time with TimeWithZoneRails 比较 Time 和 TimeWithZone
【发布时间】:2016-09-12 16:43:57
【问题描述】:

在我的 Rails 应用程序中,我将数据库中的 dateTime.now 进行比较:

Event.date_start >= Time.now

但是Event.date_startActiveSupport::TimeWithZone

一切正常,但我想知道比较 TimeTimeWithZone 是否正确,如果不是这样,该怎么做?

【问题讨论】:

    标签: ruby-on-rails date time


    【解决方案1】:

    最佳做法是不要在 Rails 应用程序中使用 Time.now。请改用Time.current。它也返回ActiveSupport::TimeWithZone

    【讨论】:

    • 谢谢。 Time.currentconfig/application.rb 中使用 config.time_zone ?
    • Time.currentTime.zone.now 相同 :)
    • 完全是@BartekGładys,但它增加了更少的 ABC 噪音(1 对 2 方法调用):)
    • 好的,Time.zone.now 使用 config.time_zone。谢谢;)
    【解决方案2】:

    Time.now 也适用于TimeZone。看看这个:

    Time.zone.now
    #=> Mon, 12 Sep 2016 16:51:54 UTC +00:00
    
    ExampleModel.date_start
    #=> Tue, 28 Jun 2016 12:39:26 UTC +00:00
    
    Time.zone
    #=> #<ActiveSupport::TimeZone:0x0055de104e3ce0 @name="UTC", @utc_offset=nil, @tzinfo=#<TZInfo::TimezoneProxy: Etc/UTC>, @current_period=#<TZInfo::TimezonePeriod: nil,nil,#<TZInfo::TimezoneOffset: 0,0,UTC>>>>
    

    你可以不受限制地比较这个:)

    【讨论】:

      猜你喜欢
      • 2022-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-03
      • 1970-01-01
      相关资源
      最近更新 更多