提交 959707bf 编写于 作者: J Jeremy Kemper

r1335@iwill: jeremy | 2005-06-17 11:41:50 -0700

 Ticket 1458 - distance_of_time_in_words
 r1336@iwill:  jeremy | 2005-06-17 11:44:50 -0700
 Update changelog
 r1337@iwill:  jeremy | 2005-06-17 11:45:44 -0700
 Applied patch.
 r1361@iwill:  jeremy | 2005-06-17 11:48:02 -0700
 Merge changelog


git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1449 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
上级 d808cd45
*SVN* *SVN*
* Correct distance_of_time_in_words for integer arguments and make the second ar
g optional, treating the first arg as a duration in seconds. #1458 [madrobby <t
homas@fesch.at>]
* Fixed query parser to deal gracefully with equal signs inside keys and values #1345 [gorou]. * Fixed query parser to deal gracefully with equal signs inside keys and values #1345 [gorou].
Example: /?sig=abcdef=:foobar=&x=y will pass now. Example: /?sig=abcdef=:foobar=&x=y will pass now.
......
...@@ -13,14 +13,19 @@ module Helpers ...@@ -13,14 +13,19 @@ module Helpers
module DateHelper module DateHelper
DEFAULT_PREFIX = "date" unless const_defined?("DEFAULT_PREFIX") DEFAULT_PREFIX = "date" unless const_defined?("DEFAULT_PREFIX")
# Reports the approximate distance in time between to Time objects. For example, if the distance is 47 minutes, it'll return # Reports the approximate distance in time between to Time objects or integers.
# For example, if the distance is 47 minutes, it'll return
# "about 1 hour". See the source for the complete wording list. # "about 1 hour". See the source for the complete wording list.
#Set <tt>include_seconds</tt> to true if you want more detailed approximations if distance < 1 minute #
def distance_of_time_in_words(from_time, to_time, include_seconds = false) # Integers are interpreted as seconds. So,
from_time = from_time.to_time # <tt>distance_of_time_in_words(50)</tt> returns "less than a minute".
to_time = to_time.to_time #
distance_in_minutes = ((to_time - from_time) / 60).round.abs # Set <tt>include_seconds</tt> to true if you want more detailed approximations if distance < 1 minute
distance_in_seconds = ((to_time - from_time)).round.abs def distance_of_time_in_words(from_time, to_time = 0, include_seconds = false)
from_time = from_time.to_time if from_time.respond_to?(:to_time)
to_time = to_time.to_time if to_time.respond_to?(:to_time)
distance_in_minutes = (((to_time - from_time).abs)/60).round
distance_in_seconds = ((to_time - from_time).abs).round
case distance_in_minutes case distance_in_minutes
when 0..1 when 0..1
......
...@@ -30,6 +30,15 @@ def test_distance_in_words ...@@ -30,6 +30,15 @@ def test_distance_in_words
# test to < from # test to < from
assert_equal "about 4 hours", distance_of_time_in_words(Time.mktime(2004, 3, 7, 1, 20), from) assert_equal "about 4 hours", distance_of_time_in_words(Time.mktime(2004, 3, 7, 1, 20), from)
assert_equal "less than 20 seconds", distance_of_time_in_words(Time.mktime(2004, 3, 6, 21, 41, 38), from, true) assert_equal "less than 20 seconds", distance_of_time_in_words(Time.mktime(2004, 3, 6, 21, 41, 38), from, true)
# test with integers
assert_equal "less than a minute", distance_of_time_in_words(50)
assert_equal "about 1 hour", distance_of_time_in_words(60*60)
# more cumbersome test with integers
assert_equal "less than a minute", distance_of_time_in_words(0, 50)
assert_equal "about 1 hour", distance_of_time_in_words(60*60, 0)
end end
def test_distance_in_words_date def test_distance_in_words_date
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册