Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
d6366625
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,发现更多精彩内容 >>
提交
d6366625
编写于
1月 03, 2012
作者:
M
Matt Bridges
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Adding :last_week, :last_month, and :last_year aliases to Time and Date
core extensions
上级
7cfd1bd7
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
80 addition
and
6 deletion
+80
-6
activesupport/lib/active_support/core_ext/date/calculations.rb
...esupport/lib/active_support/core_ext/date/calculations.rb
+7
-0
activesupport/lib/active_support/core_ext/time/calculations.rb
...esupport/lib/active_support/core_ext/time/calculations.rb
+3
-0
activesupport/test/core_ext/date_ext_test.rb
activesupport/test/core_ext/date_ext_test.rb
+24
-0
activesupport/test/core_ext/date_time_ext_test.rb
activesupport/test/core_ext/date_time_ext_test.rb
+16
-0
activesupport/test/core_ext/time_ext_test.rb
activesupport/test/core_ext/time_ext_test.rb
+18
-0
railties/guides/source/active_support_core_extensions.textile
...ties/guides/source/active_support_core_extensions.textile
+12
-6
未找到文件。
activesupport/lib/active_support/core_ext/date/calculations.rb
浏览文件 @
d6366625
...
...
@@ -182,6 +182,13 @@ def prev_week(day = :monday)
result
=
(
self
-
7
).
beginning_of_week
+
DAYS_INTO_WEEK
[
day
]
self
.
acts_like?
(
:time
)
?
result
.
change
(
:hour
=>
0
)
:
result
end
alias
:last_week
:prev_week
# Alias of prev_month
alias
:last_month
:prev_month
# Alias of prev_year
alias
:last_year
:prev_year
# Returns a new Date/DateTime representing the start of the given day in next week (default is :monday).
def
next_week
(
day
=
:monday
)
...
...
activesupport/lib/active_support/core_ext/time/calculations.rb
浏览文件 @
d6366625
...
...
@@ -145,6 +145,7 @@ def years_since(years)
def
prev_year
years_ago
(
1
)
end
alias_method
:last_year
,
:prev_year
# Short-hand for years_since(1)
def
next_year
...
...
@@ -155,6 +156,7 @@ def next_year
def
prev_month
months_ago
(
1
)
end
alias_method
:last_month
,
:prev_month
# Short-hand for months_since(1)
def
next_month
...
...
@@ -199,6 +201,7 @@ def sunday
def
prev_week
(
day
=
:monday
)
ago
(
1
.
week
).
beginning_of_week
.
since
(
DAYS_INTO_WEEK
[
day
].
day
).
change
(
:hour
=>
0
)
end
alias_method
:last_week
,
:prev_week
# Returns a new Time representing the start of the given day in next week (default is :monday).
def
next_week
(
day
=
:monday
)
...
...
activesupport/test/core_ext/date_ext_test.rb
浏览文件 @
d6366625
...
...
@@ -175,6 +175,18 @@ def test_prev_year_in_calendar_reform
assert_equal
Date
.
new
(
1582
,
10
,
4
),
Date
.
new
(
1583
,
10
,
14
).
prev_year
end
def
test_last_year
assert_equal
Date
.
new
(
2004
,
6
,
5
),
Date
.
new
(
2005
,
6
,
5
).
last_year
end
def
test_last_year_in_leap_years
assert_equal
Date
.
new
(
1999
,
2
,
28
),
Date
.
new
(
2000
,
2
,
29
).
last_year
end
def
test_last_year_in_calendar_reform
assert_equal
Date
.
new
(
1582
,
10
,
4
),
Date
.
new
(
1583
,
10
,
14
).
last_year
end
def
test_next_year
assert_equal
Date
.
new
(
2006
,
6
,
5
),
Date
.
new
(
2005
,
6
,
5
).
next_year
end
...
...
@@ -245,6 +257,14 @@ def test_prev_week
assert_equal
Date
.
new
(
2010
,
2
,
27
),
Date
.
new
(
2010
,
3
,
4
).
prev_week
(
:saturday
)
end
def
test_last_week
assert_equal
Date
.
new
(
2005
,
5
,
9
),
Date
.
new
(
2005
,
5
,
17
).
last_week
assert_equal
Date
.
new
(
2006
,
12
,
25
),
Date
.
new
(
2007
,
1
,
7
).
last_week
assert_equal
Date
.
new
(
2010
,
2
,
12
),
Date
.
new
(
2010
,
2
,
19
).
last_week
(
:friday
)
assert_equal
Date
.
new
(
2010
,
2
,
13
),
Date
.
new
(
2010
,
2
,
19
).
last_week
(
:saturday
)
assert_equal
Date
.
new
(
2010
,
2
,
27
),
Date
.
new
(
2010
,
3
,
4
).
last_week
(
:saturday
)
end
def
test_next_week
assert_equal
Date
.
new
(
2005
,
2
,
28
),
Date
.
new
(
2005
,
2
,
22
).
next_week
assert_equal
Date
.
new
(
2005
,
3
,
4
),
Date
.
new
(
2005
,
2
,
22
).
next_week
(
:friday
)
...
...
@@ -265,6 +285,10 @@ def test_prev_month_on_31st
assert_equal
Date
.
new
(
2004
,
2
,
29
),
Date
.
new
(
2004
,
3
,
31
).
prev_month
end
def
test_last_month_on_31st
assert_equal
Date
.
new
(
2004
,
2
,
29
),
Date
.
new
(
2004
,
3
,
31
).
last_month
end
def
test_yesterday_constructor
assert_equal
Date
.
current
-
1
,
Date
.
yesterday
end
...
...
activesupport/test/core_ext/date_time_ext_test.rb
浏览文件 @
d6366625
...
...
@@ -159,6 +159,10 @@ def test_prev_year
assert_equal
DateTime
.
civil
(
2004
,
6
,
5
,
10
),
DateTime
.
civil
(
2005
,
6
,
5
,
10
,
0
,
0
).
prev_year
end
def
test_last_year
assert_equal
DateTime
.
civil
(
2004
,
6
,
5
,
10
),
DateTime
.
civil
(
2005
,
6
,
5
,
10
,
0
,
0
).
last_year
end
def
test_next_year
assert_equal
DateTime
.
civil
(
2006
,
6
,
5
,
10
),
DateTime
.
civil
(
2005
,
6
,
5
,
10
,
0
,
0
).
next_year
end
...
...
@@ -232,6 +236,14 @@ def test_prev_week
assert_equal
DateTime
.
civil
(
2006
,
11
,
15
),
DateTime
.
civil
(
2006
,
11
,
23
,
0
,
0
,
0
).
prev_week
(
:wednesday
)
end
def
test_last_week
assert_equal
DateTime
.
civil
(
2005
,
2
,
21
),
DateTime
.
civil
(
2005
,
3
,
1
,
15
,
15
,
10
).
last_week
assert_equal
DateTime
.
civil
(
2005
,
2
,
22
),
DateTime
.
civil
(
2005
,
3
,
1
,
15
,
15
,
10
).
last_week
(
:tuesday
)
assert_equal
DateTime
.
civil
(
2005
,
2
,
25
),
DateTime
.
civil
(
2005
,
3
,
1
,
15
,
15
,
10
).
last_week
(
:friday
)
assert_equal
DateTime
.
civil
(
2006
,
10
,
30
),
DateTime
.
civil
(
2006
,
11
,
6
,
0
,
0
,
0
).
last_week
assert_equal
DateTime
.
civil
(
2006
,
11
,
15
),
DateTime
.
civil
(
2006
,
11
,
23
,
0
,
0
,
0
).
last_week
(
:wednesday
)
end
def
test_next_week
assert_equal
DateTime
.
civil
(
2005
,
2
,
28
),
DateTime
.
civil
(
2005
,
2
,
22
,
15
,
15
,
10
).
next_week
assert_equal
DateTime
.
civil
(
2005
,
3
,
4
),
DateTime
.
civil
(
2005
,
2
,
22
,
15
,
15
,
10
).
next_week
(
:friday
)
...
...
@@ -247,6 +259,10 @@ def test_prev_month_on_31st
assert_equal
DateTime
.
civil
(
2004
,
2
,
29
),
DateTime
.
civil
(
2004
,
3
,
31
).
prev_month
end
def
test_last_month_on_31st
assert_equal
DateTime
.
civil
(
2004
,
2
,
29
),
DateTime
.
civil
(
2004
,
3
,
31
).
last_month
end
def
test_xmlschema
assert_match
(
/^1880-02-28T15:15:10\+00:?00$/
,
DateTime
.
civil
(
1880
,
2
,
28
,
15
,
15
,
10
).
xmlschema
)
assert_match
(
/^1980-02-28T15:15:10\+00:?00$/
,
DateTime
.
civil
(
1980
,
2
,
28
,
15
,
15
,
10
).
xmlschema
)
...
...
activesupport/test/core_ext/time_ext_test.rb
浏览文件 @
d6366625
...
...
@@ -198,6 +198,10 @@ def test_prev_year
assert_equal
Time
.
local
(
2004
,
6
,
5
,
10
),
Time
.
local
(
2005
,
6
,
5
,
10
,
0
,
0
).
prev_year
end
def
test_last_year
assert_equal
Time
.
local
(
2004
,
6
,
5
,
10
),
Time
.
local
(
2005
,
6
,
5
,
10
,
0
,
0
).
last_year
end
def
test_next_year
assert_equal
Time
.
local
(
2006
,
6
,
5
,
10
),
Time
.
local
(
2005
,
6
,
5
,
10
,
0
,
0
).
next_year
end
...
...
@@ -505,6 +509,16 @@ def test_prev_week
assert_equal
Time
.
local
(
2006
,
11
,
15
),
Time
.
local
(
2006
,
11
,
23
,
0
,
0
,
0
).
prev_week
(
:wednesday
)
end
end
def
test_last_week
with_env_tz
'US/Eastern'
do
assert_equal
Time
.
local
(
2005
,
2
,
21
),
Time
.
local
(
2005
,
3
,
1
,
15
,
15
,
10
).
last_week
assert_equal
Time
.
local
(
2005
,
2
,
22
),
Time
.
local
(
2005
,
3
,
1
,
15
,
15
,
10
).
last_week
(
:tuesday
)
assert_equal
Time
.
local
(
2005
,
2
,
25
),
Time
.
local
(
2005
,
3
,
1
,
15
,
15
,
10
).
last_week
(
:friday
)
assert_equal
Time
.
local
(
2006
,
10
,
30
),
Time
.
local
(
2006
,
11
,
6
,
0
,
0
,
0
).
last_week
assert_equal
Time
.
local
(
2006
,
11
,
15
),
Time
.
local
(
2006
,
11
,
23
,
0
,
0
,
0
).
last_week
(
:wednesday
)
end
end
def
test_next_week
with_env_tz
'US/Eastern'
do
...
...
@@ -662,6 +676,10 @@ def test_prev_month_on_31st
assert_equal
Time
.
local
(
2004
,
2
,
29
),
Time
.
local
(
2004
,
3
,
31
).
prev_month
end
def
test_last_month_on_31st
assert_equal
Time
.
local
(
2004
,
2
,
29
),
Time
.
local
(
2004
,
3
,
31
).
last_month
end
def
test_xmlschema_is_available
assert_nothing_raised
{
Time
.
now
.
xmlschema
}
end
...
...
railties/guides/source/active_support_core_extensions.textile
浏览文件 @
d6366625
...
...
@@ -2858,6 +2858,8 @@ d.next_year # => Wed, 28 Feb 2001
Active Support defines these methods as well for Ruby 1.8.
+prev_year+ is aliased to +last_year+.
h6. +prev_month+, +next_month+
In Ruby 1.9 +prev_month+ and +next_month+ return the date with the same day in the last or next month:
...
...
@@ -2879,6 +2881,8 @@ Date.new(2000, 1, 31).next_month # => Tue, 29 Feb 2000
Active Support defines these methods as well for Ruby 1.8.
+prev_month+ is aliased to +last_month+.
h6. +beginning_of_week+, +end_of_week+
The methods +beginning_of_week+ and +end_of_week+ return the dates for the
...
...
@@ -2924,6 +2928,8 @@ d.prev_week(:saturday) # => Sat, 01 May 2010
d.prev_week(:friday) # => Fri, 30 Apr 2010
</ruby>
+prev_week+ is aliased to +last_week+.
h6. +beginning_of_month+, +end_of_month+
The methods +beginning_of_month+ and +end_of_month+ return the dates for the beginning and end of the month:
...
...
@@ -3134,13 +3140,13 @@ end_of_week (at_end_of_week)
monday
sunday
weeks_ago
prev_week
prev_week
(last_week)
next_week
months_ago
months_since
beginning_of_month (at_beginning_of_month)
end_of_month (at_end_of_month)
prev_month
prev_month
(last_month)
next_month
beginning_of_quarter (at_beginning_of_quarter)
end_of_quarter (at_end_of_quarter)
...
...
@@ -3148,7 +3154,7 @@ beginning_of_year (at_beginning_of_year)
end_of_year (at_end_of_year)
years_ago
years_since
prev_year
prev_year
(last_year)
next_year
</ruby>
...
...
@@ -3310,13 +3316,13 @@ end_of_week (at_end_of_week)
monday
sunday
weeks_ago
prev_week
prev_week
(last_week)
next_week
months_ago
months_since
beginning_of_month (at_beginning_of_month)
end_of_month (at_end_of_month)
prev_month
prev_month
(last_month)
next_month
beginning_of_quarter (at_beginning_of_quarter)
end_of_quarter (at_end_of_quarter)
...
...
@@ -3324,7 +3330,7 @@ beginning_of_year (at_beginning_of_year)
end_of_year (at_end_of_year)
years_ago
years_since
prev_year
prev_year
(last_year)
next_year
</ruby>
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录