public
Description: Koz's rails git-svn clone
Homepage: http://www.rubyonrails.org
Clone URL: git://github.com/NZKoz/koz-rails.git
TimeWithZone#method_missing: send to utc to advance with dst correctness, 
otherwise send to time. Adding tests for time calculations methods

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9208 
5ecf4fe2-1ee6-0310-87b1-e25e094e27de
gbuesing (author)
Tue Apr 01 23:56:44 -0700 2008
commit  81ff6ac8dbb6f753a78402dbc4d4f54ce4083b03
tree    093617bbe3cdf1b2338c88a23862ff556fd73198
parent  6bae3a7c2676f386062471bdd967b69063b5b577
...
1
2
 
 
3
4
5
...
1
2
3
4
5
6
7
0
@@ -1,5 +1,7 @@
0
 *SVN*
0
 
0
+* TimeWithZone#method_missing: send to utc to advance with dst correctness, otherwise send to time. Adding tests for time calculations methods [Geoff Buesing]
0
+
0
 * Add config.active_support.use_standard_json_time_format setting so that Times and Dates export to ISO 8601 dates. [rick]
0
 
0
 * TZInfo: Removing unneeded TimezoneProxy class [Geoff Buesing]
...
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
...
219
220
221
222
223
224
 
 
 
 
 
 
 
225
226
227
...
144
145
146
 
 
 
 
 
 
 
 
 
147
148
149
150
 
 
 
 
 
 
151
152
153
...
204
205
206
 
 
 
207
208
209
210
211
212
213
214
215
216
0
@@ -144,25 +144,10 @@ module ActiveSupport
0
       end
0
     end
0
     
0
- %w(asctime day hour min mon sec wday yday year to_date).each do |name|
0
- define_method(name) do
0
- time.__send__(name)
0
- end
0
- end
0
- alias_method :ctime, :asctime
0
- alias_method :mday, :day
0
- alias_method :month, :mon
0
-
0
     def usec
0
       time.respond_to?(:usec) ? time.usec : 0
0
     end
0
     
0
- %w(sunday? monday? tuesday? wednesday? thursday? friday? saturday?).each do |name|
0
- define_method(name) do
0
- time.__send__(name)
0
- end
0
- end unless RUBY_VERSION < '1.9'
0
-
0
     def to_a
0
       [time.sec, time.min, time.hour, time.day, time.mon, time.year, time.wday, time.yday, dst?, zone]
0
     end
0
@@ -219,9 +204,13 @@ module ActiveSupport
0
   
0
     # Send the missing method to time instance, and wrap result in a new TimeWithZone with the existing time_zone
0
     def method_missing(sym, *args, &block)
0
- result = utc.__send__(sym, *args, &block)
0
- result = result.in_time_zone(time_zone) if result.acts_like?(:time)
0
- result
0
+ if %w(+ - since ago advance).include?(sym.to_s)
0
+ result = utc.__send__(sym, *args, &block)
0
+ result.acts_like?(:time) ? result.in_time_zone(time_zone) : result
0
+ else
0
+ result = time.__send__(sym, *args, &block)
0
+ result.acts_like?(:time) ? self.class.new(nil, time_zone, result) : result
0
+ end
0
     end
0
     
0
     private
...
393
394
395
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
396
397
398
...
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
0
@@ -393,6 +393,69 @@ class TimeWithZoneTest < Test::Unit::TestCase
0
       assert_equal DateTime.civil(2050).to_f, ActiveSupport::TimeWithZone.new(nil, @time_zone, DateTime.civil(2049,12,31,19)).to_f
0
     end
0
   end
0
+
0
+ def test_change
0
+ assert_equal "Fri, 31 Dec 1999 19:00:00 EST -05:00", @twz.inspect
0
+ assert_equal "Mon, 31 Dec 2001 19:00:00 EST -05:00", @twz.change(:year => 2001).inspect
0
+ assert_equal "Wed, 31 Mar 1999 19:00:00 EST -05:00", @twz.change(:month => 3).inspect
0
+ assert_equal "Wed, 03 Mar 1999 19:00:00 EST -05:00", @twz.change(:month => 2).inspect
0
+ assert_equal "Wed, 15 Dec 1999 19:00:00 EST -05:00", @twz.change(:day => 15).inspect
0
+ assert_equal "Fri, 31 Dec 1999 06:00:00 EST -05:00", @twz.change(:hour => 6).inspect
0
+ assert_equal "Fri, 31 Dec 1999 19:15:00 EST -05:00", @twz.change(:min => 15).inspect
0
+ assert_equal "Fri, 31 Dec 1999 19:00:30 EST -05:00", @twz.change(:sec => 30).inspect
0
+ end
0
+
0
+ def test_advance
0
+ assert_equal "Fri, 31 Dec 1999 19:00:00 EST -05:00", @twz.inspect
0
+ assert_equal "Mon, 31 Dec 2001 19:00:00 EST -05:00", @twz.advance(:years => 2).inspect
0
+ assert_equal "Fri, 31 Mar 2000 19:00:00 EST -05:00", @twz.advance(:months => 3).inspect
0
+ assert_equal "Tue, 04 Jan 2000 19:00:00 EST -05:00", @twz.advance(:days => 4).inspect
0
+ assert_equal "Sat, 01 Jan 2000 01:00:00 EST -05:00", @twz.advance(:hours => 6).inspect
0
+ assert_equal "Fri, 31 Dec 1999 19:15:00 EST -05:00", @twz.advance(:minutes => 15).inspect
0
+ assert_equal "Fri, 31 Dec 1999 19:00:30 EST -05:00", @twz.advance(:seconds => 30).inspect
0
+ end
0
+
0
+ def beginning_of_year
0
+ assert_equal "Fri, 31 Dec 1999 19:00:00 EST -05:00", @twz.inspect
0
+ assert_equal "Fri, 01 Jan 1999 00:00:00 EST -05:00", @twz.beginning_of_year.inspect
0
+ end
0
+
0
+ def end_of_year
0
+ assert_equal "Fri, 31 Dec 1999 19:00:00 EST -05:00", @twz.inspect
0
+ assert_equal "Fri, 31 Dec 1999 23:59:59 EST -05:00", @twz.end_of_year.inspect
0
+ end
0
+
0
+ def beginning_of_month
0
+ assert_equal "Fri, 31 Dec 1999 19:00:00 EST -05:00", @twz.inspect
0
+ assert_equal "Fri, 01 Dec 1999 00:00:00 EST -05:00", @twz.beginning_of_month.inspect
0
+ end
0
+
0
+ def end_of_month
0
+ assert_equal "Fri, 31 Dec 1999 19:00:00 EST -05:00", @twz.inspect
0
+ assert_equal "Fri, 31 Dec 1999 23:59:59 EST -05:00", @twz.end_of_month.inspect
0
+ end
0
+
0
+ def beginning_of_day
0
+ assert_equal "Fri, 31 Dec 1999 19:00:00 EST -05:00", @twz.inspect
0
+ assert_equal "Fri, 31 Dec 1999 00:00:00 EST -05:00", @twz.beginning_of_day.inspect
0
+ end
0
+
0
+ def end_of_day
0
+ assert_equal "Fri, 31 Dec 1999 19:00:00 EST -05:00", @twz.inspect
0
+ assert_equal "Fri, 31 Dec 1999 23:59:59 EST -05:00", @twz.end_of_day.inspect
0
+ end
0
+
0
+ def test_since
0
+ assert_equal "Fri, 31 Dec 1999 19:00:01 EST -05:00", @twz.since(1).inspect
0
+ end
0
+
0
+ def test_ago
0
+ assert_equal "Fri, 31 Dec 1999 18:59:59 EST -05:00", @twz.ago(1).inspect
0
+ end
0
+
0
+ def test_seconds_since_midnight
0
+ assert_equal 19 * 60 * 60, @twz.seconds_since_midnight
0
+ end
0
 end
0
 
0
 class TimeWithZoneMethodsForTimeAndDateTimeTest < Test::Unit::TestCase

Comments

    No one has commented yet.