Testing ActiveSupport::TimeWithZone objects for equality

ぃ、小莉子 提交于 2019-12-06 21:41:22

问题


Can someone explain how d1 is greater than d2? They are the same damn dates (or atleast that is how they look to me).

Loading development environment (Rails 3.0.8)
ruby-1.9.2-p180 :001 > d1 = Event.first.updated_at
 => Thu, 22 Sep 2011 02:24:28 PDT -07:00 
ruby-1.9.2-p180 :002 > d2 = Time.zone.parse("2011-09-22T02:24:28-07:00")
 => Thu, 22 Sep 2011 02:24:28 PDT -07:00 
ruby-1.9.2-p180 :003 > d1.class
 => ActiveSupport::TimeWithZone 
ruby-1.9.2-p180 :004 > d2.class
 => ActiveSupport::TimeWithZone 
ruby-1.9.2-p180 :005 > d1 > d2
 => true 
ruby-1.9.2-p180 :006 > 

With regards to my specific application needs ... I have an iOS app that makes a request to my Rails application passing a JSON object that, amongst other items, includes NSDates in the format of "2011-09-22T02:24:28-07:00." I'm attempting to compare that datetime with the "updated_at" which is of type ActiveSupport::TimeWithZone.

Thanks - wg


回答1:


You will find that the updated_at attribute in your Event model has a higher precision than seconds.

Try outputting the milliseconds part of your respective time objects:

puts d1.usec
puts d2.usec

Chances are the former will be > 0 since it was set automatically when the object was persisted, while the latter will equal 0 since you did not specify any milliseconds in the string from which you parsed it.



来源:https://stackoverflow.com/questions/7512876/testing-activesupporttimewithzone-objects-for-equality

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!