Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
297359bc
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
297359bc
编写于
2月 25, 2012
作者:
E
Elliot Winkler
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
distance_of_time_in_words: Add/tweak comments to understand time intervals better
上级
d2f995ed
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
32 addition
and
18 deletion
+32
-18
actionpack/lib/action_view/helpers/date_helper.rb
actionpack/lib/action_view/helpers/date_helper.rb
+12
-7
actionpack/test/template/date_helper_test.rb
actionpack/test/template/date_helper_test.rb
+20
-11
未找到文件。
actionpack/lib/action_view/helpers/date_helper.rb
浏览文件 @
297359bc
...
...
@@ -96,13 +96,18 @@ def distance_of_time_in_words(from_time, to_time = 0, include_seconds_or_options
else
locale
.
t
:x_minutes
,
:count
=>
1
end
when
2
..
44
then
locale
.
t
:x_minutes
,
:count
=>
distance_in_minutes
when
45
..
89
then
locale
.
t
:about_x_hours
,
:count
=>
1
when
90
..
1439
then
locale
.
t
:about_x_hours
,
:count
=>
(
distance_in_minutes
.
to_f
/
60.0
).
round
when
1440
..
2519
then
locale
.
t
:x_days
,
:count
=>
1
when
2520
..
43199
then
locale
.
t
:x_days
,
:count
=>
(
distance_in_minutes
.
to_f
/
1440.0
).
round
when
43200
..
86399
then
locale
.
t
:about_x_months
,
:count
=>
(
distance_in_minutes
.
to_f
/
43200.0
).
round
when
86400
..
525599
then
locale
.
t
:x_months
,
:count
=>
(
distance_in_minutes
.
to_f
/
43200.0
).
round
when
2
...
45
then
locale
.
t
:x_minutes
,
:count
=>
distance_in_minutes
when
45
...
90
then
locale
.
t
:about_x_hours
,
:count
=>
1
# 90 mins up to 24 hours
when
90
...
1440
then
locale
.
t
:about_x_hours
,
:count
=>
(
distance_in_minutes
.
to_f
/
60.0
).
round
# 24 hours up to 42 hours
when
1440
...
2520
then
locale
.
t
:x_days
,
:count
=>
1
# 42 hours up to 30 days
when
2520
...
43200
then
locale
.
t
:x_days
,
:count
=>
(
distance_in_minutes
.
to_f
/
1440.0
).
round
# 30 days up to 60 days
when
43200
...
86400
then
locale
.
t
:about_x_months
,
:count
=>
(
distance_in_minutes
.
to_f
/
43200.0
).
round
# 60 days up to 365 days
when
86400
...
525600
then
locale
.
t
:x_months
,
:count
=>
(
distance_in_minutes
.
to_f
/
43200.0
).
round
else
if
from_time
.
acts_like?
(
:time
)
&&
to_time
.
acts_like?
(
:time
)
fyear
=
from_time
.
year
...
...
actionpack/test/template/date_helper_test.rb
浏览文件 @
297359bc
...
...
@@ -21,7 +21,7 @@ def to_param
def
assert_distance_of_time_in_words
(
from
,
to
=
nil
)
to
||=
from
# 0..1 with :include_seconds => true
# 0..1
minute
with :include_seconds => true
assert_equal
"less than 5 seconds"
,
distance_of_time_in_words
(
from
,
to
+
0
.
seconds
,
:include_seconds
=>
true
)
assert_equal
"less than 5 seconds"
,
distance_of_time_in_words
(
from
,
to
+
4
.
seconds
,
:include_seconds
=>
true
)
assert_equal
"less than 10 seconds"
,
distance_of_time_in_words
(
from
,
to
+
5
.
seconds
,
:include_seconds
=>
true
)
...
...
@@ -35,7 +35,7 @@ def assert_distance_of_time_in_words(from, to=nil)
assert_equal
"1 minute"
,
distance_of_time_in_words
(
from
,
to
+
60
.
seconds
,
:include_seconds
=>
true
)
assert_equal
"1 minute"
,
distance_of_time_in_words
(
from
,
to
+
89
.
seconds
,
:include_seconds
=>
true
)
# 0..1 with :include_seconds => false
# 0..1
minute
with :include_seconds => false
assert_equal
"less than a minute"
,
distance_of_time_in_words
(
from
,
to
+
0
.
seconds
,
:include_seconds
=>
false
)
assert_equal
"less than a minute"
,
distance_of_time_in_words
(
from
,
to
+
4
.
seconds
,
:include_seconds
=>
false
)
assert_equal
"less than a minute"
,
distance_of_time_in_words
(
from
,
to
+
5
.
seconds
,
:include_seconds
=>
false
)
...
...
@@ -48,44 +48,53 @@ def assert_distance_of_time_in_words(from, to=nil)
assert_equal
"1 minute"
,
distance_of_time_in_words
(
from
,
to
+
59
.
seconds
,
:include_seconds
=>
false
)
assert_equal
"1 minute"
,
distance_of_time_in_words
(
from
,
to
+
60
.
seconds
,
:include_seconds
=>
false
)
assert_equal
"1 minute"
,
distance_of_time_in_words
(
from
,
to
+
89
.
seconds
,
:include_seconds
=>
false
)
# First case 0..1
# Note that we are including a 30-second boundary around the interval we
# want to test. For instance, "1 minute" is actually 30s to 1m29s. The
# reason for doing this is simple -- in `distance_of_time_to_words`, when we
# take the distance between our two Time objects in seconds and convert it
# to minutes, we round the number. So 29s gets rounded down to 0m, 30s gets
# rounded up to 1m, and 1m29s gets rounded down to 1m. A similar thing
# happens with the other cases.
# First case 0..1 minute
assert_equal
"less than a minute"
,
distance_of_time_in_words
(
from
,
to
+
0
.
seconds
)
assert_equal
"less than a minute"
,
distance_of_time_in_words
(
from
,
to
+
29
.
seconds
)
assert_equal
"1 minute"
,
distance_of_time_in_words
(
from
,
to
+
30
.
seconds
)
assert_equal
"1 minute"
,
distance_of_time_in_words
(
from
,
to
+
1
.
minutes
+
29
.
seconds
)
# 2
..44
# 2
minutes up to 45 minutes
assert_equal
"2 minutes"
,
distance_of_time_in_words
(
from
,
to
+
1
.
minutes
+
30
.
seconds
)
assert_equal
"44 minutes"
,
distance_of_time_in_words
(
from
,
to
+
44
.
minutes
+
29
.
seconds
)
# 45
..89
# 45
minutes up to 90 minutes
assert_equal
"about 1 hour"
,
distance_of_time_in_words
(
from
,
to
+
44
.
minutes
+
30
.
seconds
)
assert_equal
"about 1 hour"
,
distance_of_time_in_words
(
from
,
to
+
89
.
minutes
+
29
.
seconds
)
# 90
..1439
# 90
minutes up to 24 hours
assert_equal
"about 2 hours"
,
distance_of_time_in_words
(
from
,
to
+
89
.
minutes
+
30
.
seconds
)
assert_equal
"about 24 hours"
,
distance_of_time_in_words
(
from
,
to
+
23
.
hours
+
59
.
minutes
+
29
.
seconds
)
#
1440..2519
#
24 hours up to 42 hours
assert_equal
"1 day"
,
distance_of_time_in_words
(
from
,
to
+
23
.
hours
+
59
.
minutes
+
30
.
seconds
)
assert_equal
"1 day"
,
distance_of_time_in_words
(
from
,
to
+
41
.
hours
+
59
.
minutes
+
29
.
seconds
)
#
2520..43199
#
42 hours up to 30 days
assert_equal
"2 days"
,
distance_of_time_in_words
(
from
,
to
+
41
.
hours
+
59
.
minutes
+
30
.
seconds
)
assert_equal
"3 days"
,
distance_of_time_in_words
(
from
,
to
+
2
.
days
+
12
.
hours
)
assert_equal
"30 days"
,
distance_of_time_in_words
(
from
,
to
+
29
.
days
+
23
.
hours
+
59
.
minutes
+
29
.
seconds
)
#
43200..86399
#
30 days up to 60 days
assert_equal
"about 1 month"
,
distance_of_time_in_words
(
from
,
to
+
29
.
days
+
23
.
hours
+
59
.
minutes
+
30
.
seconds
)
assert_equal
"about 1 month"
,
distance_of_time_in_words
(
from
,
to
+
44
.
days
+
23
.
hours
+
59
.
minutes
+
29
.
seconds
)
assert_equal
"about 2 months"
,
distance_of_time_in_words
(
from
,
to
+
44
.
days
+
23
.
hours
+
59
.
minutes
+
30
.
seconds
)
assert_equal
"about 2 months"
,
distance_of_time_in_words
(
from
,
to
+
59
.
days
+
23
.
hours
+
59
.
minutes
+
29
.
seconds
)
#
86400..525599
#
60 days up to 365 days
assert_equal
"2 months"
,
distance_of_time_in_words
(
from
,
to
+
59
.
days
+
23
.
hours
+
59
.
minutes
+
30
.
seconds
)
assert_equal
"12 months"
,
distance_of_time_in_words
(
from
,
to
+
1
.
years
-
31
.
seconds
)
# >
525599
# >
= 365 days
assert_equal
"about 1 year"
,
distance_of_time_in_words
(
from
,
to
+
1
.
years
-
30
.
seconds
)
assert_equal
"about 1 year"
,
distance_of_time_in_words
(
from
,
to
+
1
.
years
+
3
.
months
-
1
.
day
)
assert_equal
"over 1 year"
,
distance_of_time_in_words
(
from
,
to
+
1
.
years
+
6
.
months
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录