Permalink
Browse files

fix time issues using timecop

  • Loading branch information...
clemens committed Oct 3, 2011
1 parent 2e6925c commit 519f5b77f58338b75594eaa90350244e882930c3
Showing with 13 additions and 11 deletions.
  1. +1 −0 delocalize.gemspec
  2. +12 −11 test/delocalize_test.rb
View
@@ -62,4 +62,5 @@ Gem::Specification.new do |s|
s.add_dependency 'rails', '>= 3.0'
s.add_development_dependency 'combustion', '~> 0.3.1'
s.add_development_dependency 'sqlite3', '~> 1.3.4'
+ s.add_development_dependency 'timecop', '~> 0.3.5'
end
View
@@ -7,6 +7,7 @@
class DelocalizeActiveRecordTest < ActiveRecord::TestCase
def setup
Time.zone = 'Berlin' # make sure everything works as expected with TimeWithZone
+ Timecop.freeze(Time.zone.local(2009, 3, 1, 12, 0))
@product = Product.new
end
@@ -36,7 +37,7 @@ def setup
end
test "delocalizes localized datetime with year" do
- time = Time.gm(2009, 3, 1, 11, 0, 0).in_time_zone
+ time = Time.zone.local(2009, 3, 1, 12, 0, 0)
@product.published_at = 'Sonntag, 1. März 2009, 12:00 Uhr'
assert_equal time, @product.published_at
@@ -46,7 +47,7 @@ def setup
end
test "delocalizes localized datetime without year" do
- time = Time.gm(Date.today.year, 3, 1, 11, 0, 0).in_time_zone
+ time = Time.zone.local(Date.today.year, 3, 1, 12, 0, 0)
@product.published_at = '1. März, 12:00 Uhr'
assert_equal time, @product.published_at
@@ -55,15 +56,15 @@ def setup
# TODO can I somehow do this smarter? or should I use another zone w/o DST?
if Time.current.dst?
test "delocalizes localized time (DST)" do
- now = Time.current
- time = Time.gm(now.year, now.month, now.day, 7, 0, 0).in_time_zone
+ now = Date.today
+ time = Time.zone.local(now.year, now.month, now.day, 9, 0, 0)
@product.cant_think_of_a_sensible_time_field = '09:00 Uhr'
assert_equal time, @product.cant_think_of_a_sensible_time_field
end
else
test "delocalizes localized time (non-DST)" do
- now = Time.current
- time = Time.gm(now.year, now.month, now.day, 7, 0, 0).in_time_zone
+ now = Date.today
+ time = Time.zone.local(now.year, now.month, now.day, 8, 0, 0)
@product.cant_think_of_a_sensible_time_field = '08:00 Uhr'
assert_equal time, @product.cant_think_of_a_sensible_time_field
end
@@ -100,12 +101,12 @@ def setup
@product.released_on = '2009/10/19'
assert_equal date, @product.released_on
- time = Time.gm(2009, 3, 1, 11, 0, 0).in_time_zone
+ time = Time.zone.local(2009, 3, 1, 12, 0, 0)
@product.published_at = '2009/03/01 12:00'
assert_equal time, @product.published_at
now = Time.current
- time = Time.gm(now.year, now.month, now.day, 7, 0, 0).in_time_zone
+ time = Time.zone.local(now.year, now.month, now.day, 8, 0, 0)
@product.cant_think_of_a_sensible_time_field = '08:00'
assert_equal time, @product.cant_think_of_a_sensible_time_field
end
@@ -223,7 +224,7 @@ def setup
end
test "shows text field using formatted date and time" do
- @product.published_at = Time.local(2009, 3, 1, 12, 0, 0)
+ @product.published_at = Time.zone.local(2009, 3, 1, 12, 0, 0)
# careful - leading whitespace with %e
assert_dom_equal '<input id="product_published_at" name="product[published_at]" size="30" type="text" value="Sonntag, 1. März 2009, 12:00 Uhr" />',
text_field(:product, :published_at)
@@ -236,14 +237,14 @@ def setup
end
test "shows text field using formatted date and time with format" do
- @product.published_at = Time.local(2009, 3, 1, 12, 0, 0)
+ @product.published_at = Time.zone.local(2009, 3, 1, 12, 0, 0)
# careful - leading whitespace with %e
assert_dom_equal '<input id="product_published_at" name="product[published_at]" size="30" type="text" value=" 1. März, 12:00 Uhr" />',
text_field(:product, :published_at, :format => :short)
end
test "shows text field using formatted time with format" do
- @product.cant_think_of_a_sensible_time_field = Time.local(2009, 3, 1, 9, 0, 0)
+ @product.cant_think_of_a_sensible_time_field = Time.zone.local(2009, 3, 1, 9, 0, 0)
assert_dom_equal '<input id="product_cant_think_of_a_sensible_time_field" name="product[cant_think_of_a_sensible_time_field]" size="30" type="text" value="09:00 Uhr" />',
text_field(:product, :cant_think_of_a_sensible_time_field, :format => :time)
end

0 comments on commit 519f5b7

Please sign in to comment.