Skip to content

Commit

Permalink
Make String.to_time respect timezones
Browse files Browse the repository at this point in the history
  • Loading branch information
Nate Mueller committed Jun 3, 2011
1 parent f064664 commit f449da5
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ def ord
# Form can be either :utc (default) or :local. # Form can be either :utc (default) or :local.
def to_time(form = :utc) def to_time(form = :utc)
return nil if self.blank? return nil if self.blank?
d = ::Date._parse(self, false).values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction).map { |arg| arg || 0 } d = ::Date._parse(self, false).values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction, :offset).map { |arg| arg || 0 }
d[6] *= 1000000 d[6] *= 1000000
::Time.send("#{form}_time", *d) ::Time.send("#{form}_time", *d[0..6]) - d[7]
end end


def to_date def to_date
Expand Down
1 change: 1 addition & 0 deletions activesupport/test/core_ext/string_ext_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ def test_string_to_time
assert_equal Time.local(2005, 2, 27, 23, 50, 19, 275038), "2005-02-27T23:50:19.275038".to_time(:local) assert_equal Time.local(2005, 2, 27, 23, 50, 19, 275038), "2005-02-27T23:50:19.275038".to_time(:local)
assert_equal DateTime.civil(2039, 2, 27, 23, 50), "2039-02-27 23:50".to_time assert_equal DateTime.civil(2039, 2, 27, 23, 50), "2039-02-27 23:50".to_time
assert_equal Time.local_time(2039, 2, 27, 23, 50), "2039-02-27 23:50".to_time(:local) assert_equal Time.local_time(2039, 2, 27, 23, 50), "2039-02-27 23:50".to_time(:local)
assert_equal Time.utc(2039, 2, 27, 23, 50), "2039-02-27 22:50 -0100".to_time
assert_nil "".to_time assert_nil "".to_time
end end


Expand Down

0 comments on commit f449da5

Please sign in to comment.