Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix issues with using date library under ruby 1.9.3

  • Loading branch information...
commit 0ce43c4b4d9af77abcf2d11a05b496138059507a 1 parent 145224e
David Doan authored
View
23 lib/core_ext.rb
@@ -1,20 +1,3 @@
-##
-# Extend Time class to offset the time that 'now' returns. This
-# provides the opening to "warp time" for any tests checking for
-# time-based limitations. Perhaps one needs to check hourly
-# limits, common time borders like midnight, etc.
-if !Time.respond_to?(:real_now) # assures there is no infinite looping when aliasing #now
- Time.class_eval do
- class << self
- attr_accessor :testing_offset
-
- alias_method :real_now, :now
- def now
- real_now - testing_offset
- end
- alias_method :new, :now
-
- end
- end
-end
-Time.testing_offset = 0
+require File.join File.dirname(__FILE__), 'core_ext/time'
+require File.join File.dirname(__FILE__), 'core_ext/date'
+require File.join File.dirname(__FILE__), 'core_ext/date_time'
View
13 lib/core_ext/date.rb
@@ -0,0 +1,13 @@
+require 'date'
+
+# Extend Date class to generate 'today' from modified Time.now
+Date.class_eval do
+ class << self
+
+ def today
+ Time.now.to_date
+ end
+
+ end
+end
+
View
12 lib/core_ext/date_time.rb
@@ -0,0 +1,12 @@
+require 'date'
+
+# Extend DateTime class to generate 'now' from modified Time.now
+DateTime.class_eval do
+ class << self
+
+ def now
+ Time.now.to_datetime
+ end
+
+ end
+end
View
20 lib/core_ext/time.rb
@@ -0,0 +1,20 @@
+##
+# Extend Time class to offset the time that 'now' returns. This
+# provides the opening to "warp time" for any tests checking for
+# time-based limitations. Perhaps one needs to check hourly
+# limits, common time borders like midnight, etc.
+if !Time.respond_to?(:real_now) # assures there is no infinite looping when aliasing #now
+ Time.class_eval do
+ class << self
+ attr_accessor :testing_offset
+
+ alias_method :real_now, :now
+ def now
+ real_now - testing_offset
+ end
+ alias_method :new, :now
+
+ end
+ end
+end
+Time.testing_offset = 0
View
23 test/time_warp_test.rb
@@ -93,4 +93,27 @@ def date.to_time
assert_equal 0, Time.now.utc.min
end
end
+
+ def test_pretend_now_with_date
+ # Date.today returns the current date in local time, not UTC
+ # use local time to test this instead
+
+ pretend_now_is(Time.local(2008,"jul",25,6,15)) do #=> Fri Jul 25 06:15:00 2008 (local)
+ assert_equal 2008, Date.today.year
+ assert_equal 7, Date.today.month
+ assert_equal 25, Date.today.day
+ end
+ end
+
+ def test_pretend_now_with_date_time
+ pretend_now_is(Time.utc(2008,"jul",25,6,15)) do #=> Fri Jul 25 06:15:00 UTC 2008
+ date_time = DateTime.now.new_offset(0) # UTC DateTime
+
+ assert_equal 2008, date_time.year
+ assert_equal 7, date_time.month
+ assert_equal 25, date_time.day
+ assert_equal 6, date_time.hour
+ assert_equal 15, date_time.min
+ end
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.