测试 ActiveSupport::TimeWithZone 对象是否相等 [英] Testing ActiveSupport::TimeWithZone objects for equality

查看:39
本文介绍了测试 ActiveSupport::TimeWithZone 对象是否相等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能解释一下 d1 是如何大于 d2 的?它们是同一个该死的日期(或者至少在我看来它们是这样的).

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 > 

关于我的特定应用程序需求......我有一个 iOS 应用程序向我的 Rails 应用程序发出请求,传递一个 JSON 对象,其中包括2011-09-22T02:24"格式的 NSDates:28-07:00."我正在尝试将该日期时间与 ActiveSupport::TimeWithZone 类型的updated_at"进行比较.

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.

谢谢-wg

推荐答案

你会发现你的Event模型中的updated_at属性的精度比秒还高.

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

前者可能 > 0,因为它是在持久化对象时自动设置的,而后者将等于 0,因为您没有在解析它的字符串中指定任何毫秒.

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.

这篇关于测试 ActiveSupport::TimeWithZone 对象是否相等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆