Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
7a3e5ac2
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,发现更多精彩内容 >>
提交
7a3e5ac2
编写于
9月 01, 2014
作者:
A
Agis-
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Time#change can now change nanoseconds (:nsec)
Closes #16392.
上级
75e06a19
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
25 addition
and
6 deletion
+25
-6
activesupport/CHANGELOG.md
activesupport/CHANGELOG.md
+5
-0
activesupport/lib/active_support/core_ext/time/calculations.rb
...esupport/lib/active_support/core_ext/time/calculations.rb
+13
-6
activesupport/test/core_ext/time_ext_test.rb
activesupport/test/core_ext/time_ext_test.rb
+7
-0
未找到文件。
activesupport/CHANGELOG.md
浏览文件 @
7a3e5ac2
*
Time#change can now change nanoseconds (:nsec) as a higher-precision
alternative to microseconds (:usec).
*Agis Anastasooulos*
*
`MessageVerifier.new`
raises an appropriate exception if the secret is
`nil`
.
This prevents
`MessageVerifier#generate`
from raising a cryptic error later on.
...
...
activesupport/lib/active_support/core_ext/time/calculations.rb
浏览文件 @
7a3e5ac2
...
...
@@ -64,11 +64,12 @@ def seconds_until_end_of_day
# Returns a new Time where one or more of the elements have been changed according
# to the +options+ parameter. The time options (<tt>:hour</tt>, <tt>:min</tt>,
# <tt>:sec</tt>, <tt>:usec</tt>) 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. The +options+ parameter takes a hash with any of these
# keys: <tt>:year</tt>, <tt>:month</tt>, <tt>:day</tt>, <tt>:hour</tt>, <tt>:min</tt>,
# <tt>:sec</tt>, <tt>:usec</tt>.
# <tt>:sec</tt>, <tt>:usec</tt>, <tt>:nsec</tt>) reset cascadingly, so if only
# the hour is passed, then minute, sec, usec and nsec is set to 0. If the hour
# and minute is passed, then sec, usec and nsec is set to 0. The +options+
# parameter takes a hash with any of these keys: <tt>:year</tt>, <tt>:month</tt>,
# <tt>:day</tt>, <tt>:hour</tt>, <tt>:min</tt>, <tt>:sec</tt>, <tt>:usec</tt>
# <tt>:nsec</tt>. Path either <tt>:usec</tt> or <tt>:nsec</tt>, not both.
#
# Time.new(2012, 8, 29, 22, 35, 0).change(day: 1) # => Time.new(2012, 8, 1, 22, 35, 0)
# Time.new(2012, 8, 29, 22, 35, 0).change(year: 1981, day: 1) # => Time.new(1981, 8, 1, 22, 35, 0)
...
...
@@ -80,7 +81,13 @@ def change(options)
new_hour
=
options
.
fetch
(
:hour
,
hour
)
new_min
=
options
.
fetch
(
:min
,
options
[
:hour
]
?
0
:
min
)
new_sec
=
options
.
fetch
(
:sec
,
(
options
[
:hour
]
||
options
[
:min
])
?
0
:
sec
)
new_usec
=
options
.
fetch
(
:usec
,
(
options
[
:hour
]
||
options
[
:min
]
||
options
[
:sec
])
?
0
:
Rational
(
nsec
,
1000
))
if
new_nsec
=
options
[
:nsec
]
raise
ArgumentError
,
"Can't change both :nsec and :usec at the same time:
#{
options
.
inspect
}
"
if
options
[
:usec
]
new_usec
=
Rational
(
new_nsec
,
1000
)
else
new_usec
=
options
.
fetch
(
:usec
,
(
options
[
:hour
]
||
options
[
:min
]
||
options
[
:sec
])
?
0
:
Rational
(
nsec
,
1000
))
end
if
utc?
::
Time
.
utc
(
new_year
,
new_month
,
new_day
,
new_hour
,
new_min
,
new_sec
,
new_usec
)
...
...
activesupport/test/core_ext/time_ext_test.rb
浏览文件 @
7a3e5ac2
...
...
@@ -387,6 +387,8 @@ def test_change
assert_equal
Time
.
local
(
2005
,
1
,
2
,
11
,
6
,
0
,
0
),
Time
.
local
(
2005
,
1
,
2
,
11
,
22
,
33
,
44
).
change
(
:min
=>
6
)
assert_equal
Time
.
local
(
2005
,
1
,
2
,
11
,
22
,
7
,
0
),
Time
.
local
(
2005
,
1
,
2
,
11
,
22
,
33
,
44
).
change
(
:sec
=>
7
)
assert_equal
Time
.
local
(
2005
,
1
,
2
,
11
,
22
,
33
,
8
),
Time
.
local
(
2005
,
1
,
2
,
11
,
22
,
33
,
44
).
change
(
:usec
=>
8
)
assert_equal
Time
.
local
(
2005
,
1
,
2
,
11
,
22
,
33
,
8
),
Time
.
local
(
2005
,
1
,
2
,
11
,
22
,
33
,
2
).
change
(
:nsec
=>
8000
)
assert_raise
(
ArgumentError
)
{
Time
.
local
(
2005
,
1
,
2
,
11
,
22
,
33
,
8
).
change
(
:usec
=>
1
,
:nsec
=>
1
)
}
end
def
test_utc_change
...
...
@@ -396,6 +398,7 @@ def test_utc_change
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
)
assert_equal
Time
.
utc
(
2005
,
1
,
2
,
11
,
22
,
33
,
8
),
Time
.
utc
(
2005
,
1
,
2
,
11
,
22
,
33
,
2
).
change
(
:nsec
=>
8000
)
end
def
test_offset_change
...
...
@@ -405,7 +408,11 @@ def test_offset_change
assert_equal
Time
.
new
(
2005
,
2
,
22
,
16
,
0
,
0
,
"-08:00"
),
Time
.
new
(
2005
,
2
,
22
,
15
,
15
,
10
,
"-08:00"
).
change
(
:hour
=>
16
)
assert_equal
Time
.
new
(
2005
,
2
,
22
,
16
,
45
,
0
,
"-08:00"
),
Time
.
new
(
2005
,
2
,
22
,
15
,
15
,
10
,
"-08:00"
).
change
(
:hour
=>
16
,
:min
=>
45
)
assert_equal
Time
.
new
(
2005
,
2
,
22
,
15
,
45
,
0
,
"-08:00"
),
Time
.
new
(
2005
,
2
,
22
,
15
,
15
,
10
,
"-08:00"
).
change
(
:min
=>
45
)
assert_equal
Time
.
new
(
2005
,
2
,
22
,
15
,
15
,
10
,
"-08:00"
),
Time
.
new
(
2005
,
2
,
22
,
15
,
15
,
0
,
"-08:00"
).
change
(
:sec
=>
10
)
assert_equal
10
,
Time
.
new
(
2005
,
2
,
22
,
15
,
15
,
0
,
"-08:00"
).
change
(
:usec
=>
10
).
usec
assert_equal
10
,
Time
.
new
(
2005
,
2
,
22
,
15
,
15
,
0
,
"-08:00"
).
change
(
:nsec
=>
10
).
nsec
assert_raise
(
ArgumentError
)
{
Time
.
new
(
2005
,
2
,
22
,
15
,
15
,
45
,
"-08:00"
).
change
(
:usec
=>
1000000
)
}
assert_raise
(
ArgumentError
)
{
Time
.
new
(
2005
,
2
,
22
,
15
,
15
,
45
,
"-08:00"
).
change
(
:nsec
=>
1000000000
)
}
end
def
test_advance
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录