Skip to content
This repository
Browse code

respect nsec in TimeWithZone

when we pass fractional usec to Time methods we should use Rational
instead of Float because of accuracy problem

    Time.local(2011,6,12,23,59,59,999999.999).nsec
    # => 999999998

    Time.local(2011,6,12,23,59,59,Rational(999999999, 1000)).nsec
    # => 999999999
  • Loading branch information...
commit dcdde7da481e11660634278a8004175a1ce20f39 1 parent e2e5136
Vasiliy Ermolovich authored May 18, 2012
8  activesupport/lib/active_support/core_ext/time/calculations.rb
@@ -253,7 +253,7 @@ def end_of_day
253 253
       :hour => 23,
254 254
       :min => 59,
255 255
       :sec => 59,
256  
-      :usec => 999999.999
  256
+      :usec => Rational(999999999, 1000)
257 257
     )
258 258
   end
259 259
 
@@ -268,7 +268,7 @@ def end_of_hour
268 268
     change(
269 269
       :min => 59,
270 270
       :sec => 59,
271  
-      :usec => 999999.999
  271
+      :usec => Rational(999999999, 1000)
272 272
     )
273 273
   end
274 274
 
@@ -288,7 +288,7 @@ def end_of_month
288 288
       :hour => 23,
289 289
       :min  => 59,
290 290
       :sec  => 59,
291  
-      :usec => 999999.999
  291
+      :usec => Rational(999999999, 1000)
292 292
     )
293 293
   end
294 294
   alias :at_end_of_month :end_of_month
@@ -321,7 +321,7 @@ def end_of_year
321 321
       :hour  => 23,
322 322
       :min   => 59,
323 323
       :sec   => 59,
324  
-      :usec  => 999999.999
  324
+      :usec  => Rational(999999999, 1000)
325 325
     )
326 326
   end
327 327
   alias :at_end_of_year :end_of_year
2  activesupport/lib/active_support/time_with_zone.rb
@@ -337,7 +337,7 @@ def get_period_and_ensure_valid_local_time
337 337
       end
338 338
 
339 339
       def transfer_time_values_to_utc_constructor(time)
340  
-        ::Time.utc_time(time.year, time.month, time.day, time.hour, time.min, time.sec, time.respond_to?(:usec) ? time.usec : 0)
  340
+        ::Time.utc_time(time.year, time.month, time.day, time.hour, time.min, time.sec, time.respond_to?(:nsec) ? Rational(time.nsec, 1000) : 0)
341 341
       end
342 342
 
343 343
       def duration_of_variable_length?(obj)
4  activesupport/test/core_ext/date_ext_test.rb
@@ -374,14 +374,14 @@ def test_beginning_of_day_when_zone_is_set
374 374
   end
375 375
 
376 376
   def test_end_of_day
377  
-    assert_equal Time.local(2005,2,21,23,59,59,999999.999), Date.new(2005,2,21).end_of_day
  377
+    assert_equal Time.local(2005,2,21,23,59,59,Rational(999999999, 1000)), Date.new(2005,2,21).end_of_day
378 378
   end
379 379
 
380 380
   def test_end_of_day_when_zone_is_set
381 381
     zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
382 382
     with_env_tz 'UTC' do
383 383
       with_tz_default zone do
384  
-        assert_equal zone.local(2005,2,21,23,59,59,999999.999), Date.new(2005,2,21).end_of_day
  384
+        assert_equal zone.local(2005,2,21,23,59,59,Rational(999999999, 1000)), Date.new(2005,2,21).end_of_day
385 385
         assert_equal zone, Date.new(2005,2,21).end_of_day.time_zone
386 386
       end
387 387
     end
66  activesupport/test/core_ext/time_ext_test.rb
@@ -109,49 +109,49 @@ def test_beginning_of_quarter
109 109
   end
110 110
 
111 111
   def test_end_of_day
112  
-    assert_equal Time.local(2007,8,12,23,59,59,999999.999), Time.local(2007,8,12,10,10,10).end_of_day
  112
+    assert_equal Time.local(2007,8,12,23,59,59,Rational(999999999, 1000)), Time.local(2007,8,12,10,10,10).end_of_day
113 113
     with_env_tz 'US/Eastern' do
114  
-      assert_equal Time.local(2007,4,2,23,59,59,999999.999), Time.local(2007,4,2,10,10,10).end_of_day, 'start DST'
115  
-      assert_equal Time.local(2007,10,29,23,59,59,999999.999), Time.local(2007,10,29,10,10,10).end_of_day, 'ends DST'
  114
+      assert_equal Time.local(2007,4,2,23,59,59,Rational(999999999, 1000)), Time.local(2007,4,2,10,10,10).end_of_day, 'start DST'
  115
+      assert_equal Time.local(2007,10,29,23,59,59,Rational(999999999, 1000)), Time.local(2007,10,29,10,10,10).end_of_day, 'ends DST'
116 116
     end
117 117
     with_env_tz 'NZ' do
118  
-      assert_equal Time.local(2006,3,19,23,59,59,999999.999), Time.local(2006,3,19,10,10,10).end_of_day, 'ends DST'
119  
-      assert_equal Time.local(2006,10,1,23,59,59,999999.999), Time.local(2006,10,1,10,10,10).end_of_day, 'start DST'
  118
+      assert_equal Time.local(2006,3,19,23,59,59,Rational(999999999, 1000)), Time.local(2006,3,19,10,10,10).end_of_day, 'ends DST'
  119
+      assert_equal Time.local(2006,10,1,23,59,59,Rational(999999999, 1000)), Time.local(2006,10,1,10,10,10).end_of_day, 'start DST'
120 120
     end
121 121
   end
122 122
 
123 123
   def test_end_of_week
124  
-    assert_equal Time.local(2008,1,6,23,59,59,999999.999), Time.local(2007,12,31,10,10,10).end_of_week
125  
-    assert_equal Time.local(2007,9,2,23,59,59,999999.999), Time.local(2007,8,27,0,0,0).end_of_week #monday
126  
-    assert_equal Time.local(2007,9,2,23,59,59,999999.999), Time.local(2007,8,28,0,0,0).end_of_week #tuesday
127  
-    assert_equal Time.local(2007,9,2,23,59,59,999999.999), Time.local(2007,8,29,0,0,0).end_of_week #wednesday
128  
-    assert_equal Time.local(2007,9,2,23,59,59,999999.999), Time.local(2007,8,30,0,0,0).end_of_week #thursday
129  
-    assert_equal Time.local(2007,9,2,23,59,59,999999.999), Time.local(2007,8,31,0,0,0).end_of_week #friday
130  
-    assert_equal Time.local(2007,9,2,23,59,59,999999.999), Time.local(2007,9,01,0,0,0).end_of_week #saturday
131  
-    assert_equal Time.local(2007,9,2,23,59,59,999999.999), Time.local(2007,9,02,0,0,0).end_of_week #sunday
  124
+    assert_equal Time.local(2008,1,6,23,59,59,Rational(999999999, 1000)), Time.local(2007,12,31,10,10,10).end_of_week
  125
+    assert_equal Time.local(2007,9,2,23,59,59,Rational(999999999, 1000)), Time.local(2007,8,27,0,0,0).end_of_week #monday
  126
+    assert_equal Time.local(2007,9,2,23,59,59,Rational(999999999, 1000)), Time.local(2007,8,28,0,0,0).end_of_week #tuesday
  127
+    assert_equal Time.local(2007,9,2,23,59,59,Rational(999999999, 1000)), Time.local(2007,8,29,0,0,0).end_of_week #wednesday
  128
+    assert_equal Time.local(2007,9,2,23,59,59,Rational(999999999, 1000)), Time.local(2007,8,30,0,0,0).end_of_week #thursday
  129
+    assert_equal Time.local(2007,9,2,23,59,59,Rational(999999999, 1000)), Time.local(2007,8,31,0,0,0).end_of_week #friday
  130
+    assert_equal Time.local(2007,9,2,23,59,59,Rational(999999999, 1000)), Time.local(2007,9,01,0,0,0).end_of_week #saturday
  131
+    assert_equal Time.local(2007,9,2,23,59,59,Rational(999999999, 1000)), Time.local(2007,9,02,0,0,0).end_of_week #sunday
132 132
   end
133 133
 
134 134
   def test_end_of_hour
135  
-    assert_equal Time.local(2005,2,4,19,59,59,999999.999), Time.local(2005,2,4,19,30,10).end_of_hour
  135
+    assert_equal Time.local(2005,2,4,19,59,59,Rational(999999999, 1000)), Time.local(2005,2,4,19,30,10).end_of_hour
136 136
   end
137 137
 
138 138
   def test_end_of_month
139  
-    assert_equal Time.local(2005,3,31,23,59,59,999999.999), Time.local(2005,3,20,10,10,10).end_of_month
140  
-    assert_equal Time.local(2005,2,28,23,59,59,999999.999), Time.local(2005,2,20,10,10,10).end_of_month
141  
-    assert_equal Time.local(2005,4,30,23,59,59,999999.999), Time.local(2005,4,20,10,10,10).end_of_month
  139
+    assert_equal Time.local(2005,3,31,23,59,59,Rational(999999999, 1000)), Time.local(2005,3,20,10,10,10).end_of_month
  140
+    assert_equal Time.local(2005,2,28,23,59,59,Rational(999999999, 1000)), Time.local(2005,2,20,10,10,10).end_of_month
  141
+    assert_equal Time.local(2005,4,30,23,59,59,Rational(999999999, 1000)), Time.local(2005,4,20,10,10,10).end_of_month
142 142
   end
143 143
 
144 144
   def test_end_of_quarter
145  
-    assert_equal Time.local(2007,3,31,23,59,59,999999.999), Time.local(2007,2,15,10,10,10).end_of_quarter
146  
-    assert_equal Time.local(2007,3,31,23,59,59,999999.999), Time.local(2007,3,31,0,0,0).end_of_quarter
147  
-    assert_equal Time.local(2007,12,31,23,59,59,999999.999), Time.local(2007,12,21,10,10,10).end_of_quarter
148  
-    assert_equal Time.local(2007,6,30,23,59,59,999999.999), Time.local(2007,4,1,0,0,0).end_of_quarter
149  
-    assert_equal Time.local(2008,6,30,23,59,59,999999.999), Time.local(2008,5,31,0,0,0).end_of_quarter
  145
+    assert_equal Time.local(2007,3,31,23,59,59,Rational(999999999, 1000)), Time.local(2007,2,15,10,10,10).end_of_quarter
  146
+    assert_equal Time.local(2007,3,31,23,59,59,Rational(999999999, 1000)), Time.local(2007,3,31,0,0,0).end_of_quarter
  147
+    assert_equal Time.local(2007,12,31,23,59,59,Rational(999999999, 1000)), Time.local(2007,12,21,10,10,10).end_of_quarter
  148
+    assert_equal Time.local(2007,6,30,23,59,59,Rational(999999999, 1000)), Time.local(2007,4,1,0,0,0).end_of_quarter
  149
+    assert_equal Time.local(2008,6,30,23,59,59,Rational(999999999, 1000)), Time.local(2008,5,31,0,0,0).end_of_quarter
150 150
   end
151 151
 
152 152
   def test_end_of_year
153  
-    assert_equal Time.local(2007,12,31,23,59,59,999999.999), Time.local(2007,2,22,10,10,10).end_of_year
154  
-    assert_equal Time.local(2007,12,31,23,59,59,999999.999), Time.local(2007,12,31,10,10,10).end_of_year
  153
+    assert_equal Time.local(2007,12,31,23,59,59,Rational(999999999, 1000)), Time.local(2007,2,22,10,10,10).end_of_year
  154
+    assert_equal Time.local(2007,12,31,23,59,59,Rational(999999999, 1000)), Time.local(2007,12,31,10,10,10).end_of_year
155 155
   end
156 156
 
157 157
   def test_beginning_of_year
@@ -828,32 +828,32 @@ def test_case_equality
828 828
   end
829 829
 
830 830
   def test_all_day
831  
-    assert_equal Time.local(2011,6,7,0,0,0)..Time.local(2011,6,7,23,59,59,999999.999), Time.local(2011,6,7,10,10,10).all_day
  831
+    assert_equal Time.local(2011,6,7,0,0,0)..Time.local(2011,6,7,23,59,59,Rational(999999999, 1000)), Time.local(2011,6,7,10,10,10).all_day
832 832
   end
833 833
 
834 834
   def test_all_day_with_timezone
835 835
     beginning_of_day = ActiveSupport::TimeWithZone.new(nil, ActiveSupport::TimeZone["Hawaii"], Time.local(2011,6,7,0,0,0))
836  
-    end_of_day = ActiveSupport::TimeWithZone.new(nil, ActiveSupport::TimeZone["Hawaii"], Time.local(2011,6,7,23,59,59,999999.999))
  836
+    end_of_day = ActiveSupport::TimeWithZone.new(nil, ActiveSupport::TimeZone["Hawaii"], Time.local(2011,6,7,23,59,59,Rational(999999999, 1000)))
837 837
 
838  
-    assert_equal beginning_of_day.inspect, ActiveSupport::TimeWithZone.new(Time.local(2011,6,7,10,10,10), ActiveSupport::TimeZone["Hawaii"]).all_day.begin.inspect
839  
-    assert_equal end_of_day.inspect, ActiveSupport::TimeWithZone.new(Time.local(2011,6,7,10,10,10), ActiveSupport::TimeZone["Hawaii"]).all_day.end.inspect
  838
+    assert_equal beginning_of_day, ActiveSupport::TimeWithZone.new(Time.local(2011,6,7,10,10,10), ActiveSupport::TimeZone["Hawaii"]).all_day.begin
  839
+    assert_equal end_of_day, ActiveSupport::TimeWithZone.new(Time.local(2011,6,7,10,10,10), ActiveSupport::TimeZone["Hawaii"]).all_day.end
840 840
   end
841 841
 
842 842
   def test_all_week
843  
-    assert_equal Time.local(2011,6,6,0,0,0)..Time.local(2011,6,12,23,59,59,999999.999), Time.local(2011,6,7,10,10,10).all_week
844  
-    assert_equal Time.local(2011,6,5,0,0,0)..Time.local(2011,6,11,23,59,59,999999.999), Time.local(2011,6,7,10,10,10).all_week(:sunday)
  843
+    assert_equal Time.local(2011,6,6,0,0,0)..Time.local(2011,6,12,23,59,59,Rational(999999999, 1000)), Time.local(2011,6,7,10,10,10).all_week
  844
+    assert_equal Time.local(2011,6,5,0,0,0)..Time.local(2011,6,11,23,59,59,Rational(999999999, 1000)), Time.local(2011,6,7,10,10,10).all_week(:sunday)
845 845
   end
846 846
 
847 847
   def test_all_month
848  
-    assert_equal Time.local(2011,6,1,0,0,0)..Time.local(2011,6,30,23,59,59,999999.999), Time.local(2011,6,7,10,10,10).all_month
  848
+    assert_equal Time.local(2011,6,1,0,0,0)..Time.local(2011,6,30,23,59,59,Rational(999999999, 1000)), Time.local(2011,6,7,10,10,10).all_month
849 849
   end
850 850
 
851 851
   def test_all_quarter
852  
-    assert_equal Time.local(2011,4,1,0,0,0)..Time.local(2011,6,30,23,59,59,999999.999), Time.local(2011,6,7,10,10,10).all_quarter
  852
+    assert_equal Time.local(2011,4,1,0,0,0)..Time.local(2011,6,30,23,59,59,Rational(999999999, 1000)), Time.local(2011,6,7,10,10,10).all_quarter
853 853
   end
854 854
 
855 855
   def test_all_year
856  
-    assert_equal Time.local(2011,1,1,0,0,0)..Time.local(2011,12,31,23,59,59,999999.999), Time.local(2011,6,7,10,10,10).all_year
  856
+    assert_equal Time.local(2011,1,1,0,0,0)..Time.local(2011,12,31,23,59,59,Rational(999999999, 1000)), Time.local(2011,6,7,10,10,10).all_year
857 857
   end
858 858
 
859 859
   protected
8  activesupport/test/core_ext/time_with_zone_test.rb
@@ -80,6 +80,14 @@ def test_to_json_with_use_standard_json_time_format_config_set_to_true
80 80
     ActiveSupport.use_standard_json_time_format = old
81 81
   end
82 82
 
  83
+  def test_nsec
  84
+    local     = Time.local(2011,6,7,23,59,59,Rational(999999999, 1000))
  85
+    with_zone = ActiveSupport::TimeWithZone.new(nil, ActiveSupport::TimeZone["Hawaii"], local)
  86
+
  87
+    assert_equal local.nsec, with_zone.nsec
  88
+    assert_equal with_zone.nsec, 999999999
  89
+  end
  90
+
83 91
   def test_strftime
84 92
     assert_equal '1999-12-31 19:00:00 EST -0500', @twz.strftime('%Y-%m-%d %H:%M:%S %Z %z')
85 93
   end

0 notes on commit dcdde7d

Please sign in to comment.
Something went wrong with that request. Please try again.