Comparing identical DateTime objects in ruby — why are these two DateTime.now's not equal?

后端 未结 4 840
后悔当初
后悔当初 2021-01-13 12:55

In a rails app, I have model code of the form:

def do_stuff(resource)
  models = Model.where(resource: resource)
  operated_at = DateTime.now

  models.each         


        
4条回答
  •  星月不相逢
    2021-01-13 13:32

    DateTime.now doesn't work for comparison with Database datetime object.

    Example:

    1. If you have the server running in Pacific Time Zone, you will expect to get the

      DateTime.now --> Fri, 25 Oct 2013 15:28:21 -0800

    (Daylight Saving Time in October supposed to be -7 hour from UTC, but somehow the datetime.now always give you -8 from UTC)

    1. I think if you use Time.zone.now instead of DateTime.now will give you the correct comparison.

      Time.zone.now --> Fri, 25 Oct 2013 15:27:17 PDT -07:00

提交回复
热议问题