Treat UTC times nicer

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@732 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
上级 6ff54f70
......@@ -11,9 +11,9 @@ def seconds_since_midnight
# Returns a new Time where one or more of the elements have been changed according to the +options+ parameter. The time options
# (hour, minute, sec, usec) reset cascadingly, so if only the hour is passed, then minute, sec, and usec is set to 0. If the hour and
# minute is passed, then sec and usec is set to 0.
def change(options, time_factory_method = :local)
def change(options)
::Time.send(
time_factory_method,
self.utc? ? :utc : :local,
options[:year] || self.year,
options[:month] || self.month,
options[:mday] || self.mday,
......@@ -37,21 +37,19 @@ def since(seconds)
end
# Returns a new Time representing the time a number of specified months ago
def months_ago(months, time_factory_method = :local)
def months_ago(months)
if months >= self.month
change({ :year => self.year - 1, :month => 12 }, time_factory_method).months_ago(months - self.month)
change(:year => self.year - 1, :month => 12).months_ago(months - self.month)
else
change({ :year => self.year, :month => self.month - months }, time_factory_method)
change(:year => self.year, :month => self.month - months)
end
end
def months_since(months, time_factory_method = :local)
def months_since(months)
if months + self.month > 12
change({ :year => self.year + 1, :month => 1 }, time_factory_method).months_since(
months - (self.month == 1 ? 12 : (self.month + 1))
)
change(:year => self.year + 1, :month => 1).months_since(months - (self.month == 1 ? 12 : (self.month + 1)))
else
change({ :year => self.year, :month => self.month + months }, time_factory_method)
change(:year => self.year, :month => self.month + months)
end
end
......@@ -85,16 +83,6 @@ def yesterday
def tomorrow
self.since(1.day)
end
# Returns a new Time of the current day at 9:00 (am) in the morning
def in_the_morning(time_factory_method = :local)
change({:hour => 9}, time_factory_method)
end
# Returns a new Time of the current day at 14:00 in the afternoon
def in_the_afternoon(time_factory_method = :local)
change({:hour => 14}, time_factory_method)
end
end
end
end
......
......@@ -62,23 +62,22 @@ def test_tomorrow
assert_equal Time.local(2005,2,23,10,10,10), Time.local(2005,2,22,10,10,10).tomorrow
assert_equal Time.local(2005,3,2,10,10,10), Time.local(2005,2,28,10,10,10).tomorrow.tomorrow
end
def test_in_the_morning
assert_equal Time.local(2005,2,22,9), Time.local(2005,2,22,15,15,10).in_the_morning
assert_equal Time.local(2005,2,22,9), Time.local(2005,2,22,3,15,10).in_the_morning
end
def test_in_the_morning
assert_equal Time.local(2005,2,22,14), Time.local(2005,2,22,15,15,10).in_the_afternoon
assert_equal Time.local(2005,2,22,14), Time.local(2005,2,22,3,15,10).in_the_afternoon
end
def test_change
assert_equal Time.local(2006,2,22,15,15,10), Time.local(2005,2,22,15,15,10).change(:year => 2006)
assert_equal Time.local(2005,6,22,15,15,10), Time.local(2005,2,22,15,15,10).change(:month => 6)
assert_equal Time.local(2012,9,22,15,15,10), Time.local(2005,2,22,15,15,10).change(:year => 2012, :month => 9)
assert_equal Time.local(2005,2,22,16), Time.local(2005,2,22,15,15,10).change(:hour => 16)
assert_equal Time.local(2005,2,22,16,45), Time.local(2005,2,22,15,15,10).change(:hour => 16, :min => 45)
assert_equal Time.local(2005,2,22,15,45), Time.local(2005,2,22,15,15,10).change(:min => 45)
assert_equal Time.local(2005,2,22,16), Time.local(2005,2,22,15,15,10).change(:hour => 16)
assert_equal Time.local(2005,2,22,16,45), Time.local(2005,2,22,15,15,10).change(:hour => 16, :min => 45)
assert_equal Time.local(2005,2,22,15,45), Time.local(2005,2,22,15,15,10).change(:min => 45)
end
def test_utc_change
assert_equal Time.utc(2006,2,22,15,15,10), Time.utc(2005,2,22,15,15,10).change(:year => 2006)
assert_equal Time.utc(2005,6,22,15,15,10), Time.utc(2005,2,22,15,15,10).change(:month => 6)
assert_equal Time.utc(2012,9,22,15,15,10), Time.utc(2005,2,22,15,15,10).change(:year => 2012, :month => 9)
assert_equal Time.utc(2005,2,22,16), Time.utc(2005,2,22,15,15,10).change(:hour => 16)
assert_equal Time.utc(2005,2,22,16,45), Time.utc(2005,2,22,15,15,10).change(:hour => 16, :min => 45)
assert_equal Time.utc(2005,2,22,15,45), Time.utc(2005,2,22,15,15,10).change(:min => 45)
end
end
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册