{"id":6660,"date":"2014-04-01T14:48:31","date_gmt":"2014-04-01T11:48:31","guid":{"rendered":"http:\/\/railsware.com\/blog\/?p=6660"},"modified":"2020-12-28T19:24:27","modified_gmt":"2020-12-28T16:24:27","slug":"time-comparison-in-ruby","status":"publish","type":"post","link":"https:\/\/railsware.com\/blog\/time-comparison-in-ruby\/","title":{"rendered":"Time comparison in Ruby"},"content":{"rendered":"I believe every ruby developer has faced time comparison issue at least once and tests are one of the possible areas where it may happen. RSpec message like this may confuse:\r\n<pre class=\"lang:ruby decode:true\">confirmed_at should have been changed to Mon, 24 Mar 2014 09:47:22 UTC, \r\nbut is now Mon, 24 Mar 2014 09:47:22 UTC<\/pre>\r\nWhat&#8217;s wrong?\r\n<h2>Investigation<\/h2>\r\nLet&#8217;s create two <code>Time<\/code> objects:\r\n<pre class=\"lang:ruby decode:true\">&gt; t1, t2 = Time.now, Time.now\r\n=&gt; [2014-03-27 23:19:18 UTC, 2014-03-27 23:19:18 UTC]<\/pre>\r\nAt the first glance, they are equal. But it\u2019s not true:\r\n<pre class=\"lang:ruby decode:true\">&gt; t1 == t2\r\n=&gt; false<\/pre>\r\nThe issue occurs because ruby <code>Time<\/code> makes comparison with fractions of seconds. We may use <code>to_f<\/code> method to see the difference between <code>t1<\/code> and <code>t2<\/code>:\r\n<pre class=\"lang:ruby decode:true\">&gt; t1.to_f\r\n=&gt; 1395955158.547284\r\n&gt; t2.to_f\r\n=&gt; 1395955158.547298<\/pre>\r\nor <code>to_r<\/code> method to get rational numbers:\r\n<pre class=\"lang:ruby decode:true\">&gt; t1.to_r\r\n=&gt; (348988789636821\/250000)\r\n&gt; t2.to_r\r\n=&gt; (697977579273649\/500000)<\/pre>\r\n<h2>Summary<\/h2>\r\nIf you don&#8217;t care about milliseconds, you may compare timestamps using <code>to_i<\/code> method:\r\n<pre class=\"lang:ruby decode:true\">&gt; t1.to_i == t2.to_i\r\n=&gt; true<\/pre>\r\nor make a new <code>Time<\/code> object from timestamps to have better RSpec failure messages:\r\n<pre class=\"lang:ruby decode:true\">&gt; Time.at(t1.to_i) == Time.at(t2.to_i)\r\n=&gt; true<\/pre>\r\nBut even better to use fixed times in our tests.","protected":false},"excerpt":{"rendered":"<p>I believe every ruby developer has faced time comparison issue at least once and tests are one of the possible areas where it may happen. RSpec message like this may confuse: confirmed_at should have been changed to Mon, 24 Mar 2014 09:47:22 UTC, but is now Mon, 24 Mar 2014 09:47:22 UTC What&#8217;s wrong? Investigation&#8230;<\/p>\n","protected":false},"author":44,"featured_media":9468,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[3],"tags":[],"coauthors":["Olexander Paladiy"],"class_list":["post-6660","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development"],"acf":[],"aioseo_notices":[],"categories_data":[{"name":"Engineering","link":"https:\/\/railsware.com\/blog?category=development"}],"post_thumbnails":"https:\/\/railsware.com\/blog\/wp-content\/themes\/railsware\/vendors\/images\/article-thumbnail-default.jpg","amp_enabled":true,"_links":{"self":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts\/6660","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/users\/44"}],"replies":[{"embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/comments?post=6660"}],"version-history":[{"count":25,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts\/6660\/revisions"}],"predecessor-version":[{"id":13334,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts\/6660\/revisions\/13334"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/media\/9468"}],"wp:attachment":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/media?parent=6660"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/categories?post=6660"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/tags?post=6660"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/coauthors?post=6660"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}