Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
f0a6cce7
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,发现更多精彩内容 >>
提交
f0a6cce7
编写于
4月 29, 2016
作者:
A
Andrew White
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #24762 from Envek/solid_durations_2
[FIX] Change 1.week to create 1 week durations instead of 7 days durations.
上级
97988829
434df001
变更
4
显示空白变更内容
内联
并排
Showing
4 changed file
with
47 addition
and
11 deletion
+47
-11
activesupport/CHANGELOG.md
activesupport/CHANGELOG.md
+22
-0
activesupport/lib/active_support/core_ext/numeric/time.rb
activesupport/lib/active_support/core_ext/numeric/time.rb
+8
-8
activesupport/lib/active_support/duration.rb
activesupport/lib/active_support/duration.rb
+5
-1
activesupport/test/core_ext/duration_test.rb
activesupport/test/core_ext/duration_test.rb
+12
-2
未找到文件。
activesupport/CHANGELOG.md
浏览文件 @
f0a6cce7
*
Change
`ActiveSupport::Duration`
creation with numeric methods like
`1.week`
to create durations with more predictable and ISO8601-conformant parts.
This is to remove astonishment from getting `3600 seconds` from `1.hour`.
It should not affect current apps as duration's `value` (number of seconds) remains the same,
only hash of `parts` (and `inspect` value) is changed and only when it's constructed by calling
methods on integers. Manual construction of Durations isn't affected.
Inside the ActiveSupport::Duration itself most operations rely only on number of seconds.
Before:
[1.hour.inspect, 1.hour.value, 1.hour.parts] # => ["3600 seconds", 3600, [[:seconds, 3600]]]
[1.week.inspect, 1.week.value, 1.week.parts] # => ["7 days", 604800, [[:days, 7]]]
After:
[1.hour.inspect, 1.hour.value, 1.hour.parts] # => ["1 hour", 3600, [[:hours, 1]]]
[1.week.inspect, 1.week.value, 1.week.parts] # => ["1 week", 604800, [[:weeks, 1]]]
*Andrey Novikov*
## Rails 5.0.0.beta4 (April 27, 2016) ##
*
Time zones: Ensure that the UTC offset reflects DST changes that occurred
...
...
activesupport/lib/active_support/core_ext/numeric/time.rb
浏览文件 @
f0a6cce7
...
...
@@ -25,17 +25,17 @@ def seconds
# Returns a Duration instance matching the number of minutes provided.
#
# 2.minutes # =>
120 second
s
# 2.minutes # =>
2 minute
s
def
minutes
ActiveSupport
::
Duration
.
new
(
self
*
60
,
[[
:
seconds
,
self
*
60
]])
ActiveSupport
::
Duration
.
new
(
self
*
60
,
[[
:
minutes
,
self
]])
end
alias
:minute
:minutes
# Returns a Duration instance matching the number of hours provided.
#
# 2.hours # =>
7_200 second
s
# 2.hours # =>
2 hour
s
def
hours
ActiveSupport
::
Duration
.
new
(
self
*
3600
,
[[
:
seconds
,
self
*
3600
]])
ActiveSupport
::
Duration
.
new
(
self
*
3600
,
[[
:
hours
,
self
]])
end
alias
:hour
:hours
...
...
@@ -49,17 +49,17 @@ def days
# Returns a Duration instance matching the number of weeks provided.
#
# 2.weeks # =>
14 day
s
# 2.weeks # =>
2 week
s
def
weeks
ActiveSupport
::
Duration
.
new
(
self
*
7
.
days
,
[[
:
days
,
self
*
7
]])
ActiveSupport
::
Duration
.
new
(
self
*
7
.
days
,
[[
:
weeks
,
self
]])
end
alias
:week
:weeks
# Returns a Duration instance matching the number of fortnights provided.
#
# 2.fortnights # =>
28 day
s
# 2.fortnights # =>
4 week
s
def
fortnights
ActiveSupport
::
Duration
.
new
(
self
*
2
.
weeks
,
[[
:
days
,
self
*
14
]])
ActiveSupport
::
Duration
.
new
(
self
*
2
.
weeks
,
[[
:
weeks
,
self
*
2
]])
end
alias
:fortnight
:fortnights
...
...
activesupport/lib/active_support/duration.rb
浏览文件 @
f0a6cce7
...
...
@@ -120,7 +120,7 @@ def ago(time = ::Time.current)
def
inspect
#:nodoc:
parts
.
reduce
(
::
Hash
.
new
(
0
))
{
|
h
,(
l
,
r
)
|
h
[
l
]
+=
r
;
h
}.
sort_by
{
|
unit
,
_
|
[
:years
,
:months
,
:
day
s
,
:minutes
,
:seconds
].
index
(
unit
)}.
sort_by
{
|
unit
,
_
|
[
:years
,
:months
,
:
weeks
,
:days
,
:hour
s
,
:minutes
,
:seconds
].
index
(
unit
)}.
map
{
|
unit
,
val
|
"
#{
val
}
#{
val
==
1
?
unit
.
to_s
.
chop
:
unit
.
to_s
}
"
}.
to_sentence
(
locale:
::
I18n
.
default_locale
)
end
...
...
@@ -159,6 +159,10 @@ def sum(sign, time = ::Time.current) #:nodoc:
if
t
.
acts_like?
(
:time
)
||
t
.
acts_like?
(
:date
)
if
type
==
:seconds
t
.
since
(
sign
*
number
)
elsif
type
==
:minutes
t
.
since
(
sign
*
number
*
60
)
elsif
type
==
:hours
t
.
since
(
sign
*
number
*
3600
)
else
t
.
advance
(
type
=>
sign
*
number
)
end
...
...
activesupport/test/core_ext/duration_test.rb
浏览文件 @
f0a6cce7
...
...
@@ -66,8 +66,9 @@ def test_inspect
assert_equal
'10 years, 2 months, and 1 day'
,
(
10
.
years
+
2
.
months
+
1
.
day
).
inspect
assert_equal
'10 years, 2 months, and 1 day'
,
(
10
.
years
+
1
.
month
+
1
.
day
+
1
.
month
).
inspect
assert_equal
'10 years, 2 months, and 1 day'
,
(
1
.
day
+
10
.
years
+
2
.
months
).
inspect
assert_equal
'7 days'
,
1
.
week
.
inspect
assert_equal
'14 days'
,
1
.
fortnight
.
inspect
assert_equal
'7 days'
,
7
.
days
.
inspect
assert_equal
'1 week'
,
1
.
week
.
inspect
assert_equal
'2 weeks'
,
1
.
fortnight
.
inspect
end
def
test_inspect_locale
...
...
@@ -87,6 +88,15 @@ def test_plus_with_time
assert_equal
1
+
1
.
second
,
1
.
second
+
1
,
"Duration + Numeric should == Numeric + Duration"
end
def
test_time_plus_duration_returns_same_time_datatype
twz
=
ActiveSupport
::
TimeWithZone
.
new
(
nil
,
ActiveSupport
::
TimeZone
[
'Moscow'
]
,
Time
.
utc
(
2016
,
4
,
28
,
00
,
45
))
now
=
Time
.
now
.
utc
%w( second minute hour day week month year )
.
each
do
|
unit
|
assert_equal
((
now
+
1
.
send
(
unit
)).
class
,
Time
,
"Time + 1.
#{
unit
}
must be Time"
)
assert_equal
((
twz
+
1
.
send
(
unit
)).
class
,
ActiveSupport
::
TimeWithZone
,
"TimeWithZone + 1.
#{
unit
}
must be TimeWithZone"
)
end
end
def
test_argument_error
e
=
assert_raise
ArgumentError
do
1
.
second
.
ago
(
''
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录