Skip to content

Instantly share code, notes, and snippets.

@chrisroos
Created March 28, 2011 10:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisroos/890257 to your computer and use it in GitHub Desktop.
Save chrisroos/890257 to your computer and use it in GitHub Desktop.
A test that attempts to ensure we don't run into problems with daylight savings. Credit for the test goes to James Mead.
require 'test_helper'
class TimeZoneTest < ActiveSupport::TestCase
should "only use instances of TimeWithZone, not instances of Time" do
directories = Dir.glob(File.join(Rails.root, '**', '*.rb'))
matching_files = directories.select do |filename|
match = false
File.open(filename) do |file|
match = file.grep(%r{Time\.(now|utc)}).any?
end
match
end
assert_equal [], matching_files - [File.expand_path(__FILE__)], %{Avoid issues with daylight-savings time by always building instances of TimeWithZone and not Time. Use methods like Time.zone.now, Time.zone.parse, n.days.ago, m.hours.from_now, etc in preference to methods like Time.now, Time.utc, etc.}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment