Permalink
Browse files

Added Base.default_timezone accessor that determines whether to use T…

…ime.local (using :local) or Time.utc (using :utc) when pulling dates and times from the database. This is set to :local by default.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@271 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
dhh committed Dec 28, 2004
1 parent 8a9b998 commit 60de8c110829cb7f2df1d312ad2898633e896c24
View
@@ -1,5 +1,8 @@
*SVN*
+* Added Base.default_timezone accessor that determines whether to use Time.local (using :local) or Time.utc (using :utc) when pulling dates
+ and times from the database. This is set to :local by default.
+
* Added the possibility for adapters to overwrite add_limit! to implement a different limiting scheme than "LIMIT X" used by MySQL, PostgreSQL, and SQLite.
* Fixed that the const_missing autoload assumes the requested constant is set by require_association and calls const_get to retrieve it.
@@ -221,6 +221,11 @@ def self.inherited(child) #:nodoc:
cattr_accessor :pluralize_table_names
@@pluralize_table_names = true
+ # Determines whether to use Time.local (using :local) or Time.utc (using :utc) when pulling dates and times from the database.
+ # This is set to :local by default.
+ cattr_accessor :default_timezone
+ @@default_timezone = :local
+
# When turned on (which is default), all associations are included using "load". This mean that any change is instant in cached
# environments like mod_ruby or FastCGI. When set to false, "require" is used, which is faster but requires server restart to
# reflect changes.
@@ -220,21 +220,21 @@ def string_to_time(string)
return string if Time === string
time_array = ParseDate.parsedate(string).compact
# treat 0000-00-00 00:00:00 as nil
- Time.local(*time_array) rescue nil
+ Time.send(Base.default_timezone, *time_array) rescue nil
end
- def string_to_dummy_time(string)
- return string if Time === string
- time_array = ParseDate.parsedate(string)
- # pad the resulting array with dummy date information
- time_array[0] = 2000; time_array[1] = 1; time_array[2] = 1;
- Time.local(*time_array) rescue nil
- end
-
+ def string_to_dummy_time(string)
+ return string if Time === string
+ time_array = ParseDate.parsedate(string)
+ # pad the resulting array with dummy date information
+ time_array[0] = 2000; time_array[1] = 1; time_array[2] = 1;
+ Time.send(Base.default_timezone, *time_array) rescue nil
+ end
+
def extract_limit(sql_type)
$1.to_i if sql_type =~ /\((.*)\)/
end
-
+
def simplified_type(field_type)
case field_type
when /int/i
@@ -327,7 +327,16 @@ def test_default_values
assert_equal 1, topic.approved
assert_nil topic.last_read
end
-
+
+ def test_utc_as_time_zone
+ Topic.default_timezone = :utc
+ attributes = { "bonus_time" => "5:42:00AM" }
+ topic = Topic.find(1)
+ topic.attributes = attributes
+ assert_equal Time.utc(2000, 1, 1, 5, 42, 0), topic.bonus_time
+ Topic.default_timezone = :local
+ end
+
def test_default_values_on_empty_strings
topic = Topic.new
topic.approved = nil

0 comments on commit 60de8c1

Please sign in to comment.