Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
eba430ae
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 搜索 >>
提交
eba430ae
编写于
11月 28, 2012
作者:
A
Andrew White
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #7961 from exviva/seconds_until_end_of_day
Add #seconds_until_end_of_day to DateTime and Time
上级
ec17f0d3
ce730551
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
84 addition
and
1 deletion
+84
-1
activesupport/CHANGELOG.md
activesupport/CHANGELOG.md
+10
-1
activesupport/lib/active_support/core_ext/date_time/calculations.rb
...ort/lib/active_support/core_ext/date_time/calculations.rb
+9
-0
activesupport/lib/active_support/core_ext/time/calculations.rb
...esupport/lib/active_support/core_ext/time/calculations.rb
+9
-0
activesupport/test/core_ext/date_time_ext_test.rb
activesupport/test/core_ext/date_time_ext_test.rb
+8
-0
activesupport/test/core_ext/time_ext_test.rb
activesupport/test/core_ext/time_ext_test.rb
+48
-0
未找到文件。
activesupport/CHANGELOG.md
浏览文件 @
eba430ae
## Rails 4.0.0 (unreleased) ##
*
Add
`DateTime#seconds_until_end_of_day`
and
`Time#seconds_until_end_of_day`
as a complement for
`seconds_from_midnight`
; useful when setting expiration
times for caches, e.g.:
<% cache('dashboard', expires_in: Date.current.seconds_until_end_of_day) do %>
...
*Olek Janiszewski*
*
No longer proxy ActiveSupport::Multibyte#class.
*Steve Klabnik*
*
Deprecate
`ActiveSupport::TestCase#pending`
method, use
`skip`
from MiniTest instead.
*Carlos Antonio da Silva*
...
...
@@ -65,7 +74,7 @@
*Jeremy Kemper*
*
Add
logger.push_tags and .pop_tags
to complement logger.tagged:
*
Add
`logger.push_tags`
and
`.pop_tags`
to complement logger.tagged:
class Job
def before
...
...
activesupport/lib/active_support/core_ext/date_time/calculations.rb
浏览文件 @
eba430ae
...
...
@@ -32,6 +32,15 @@ def seconds_since_midnight
sec
+
(
min
*
60
)
+
(
hour
*
3600
)
end
# Returns the number of seconds until 23:59:59.
#
# DateTime.new(2012, 8, 29, 0, 0, 0).seconds_until_end_of_day # => 86399
# DateTime.new(2012, 8, 29, 12, 34, 56).seconds_until_end_of_day # => 41103
# DateTime.new(2012, 8, 29, 23, 59, 59).seconds_until_end_of_day # => 0
def
seconds_until_end_of_day
end_of_day
.
to_i
-
to_i
end
# Returns a new DateTime where one or more of the elements have been changed
# according to the +options+ parameter. The time options (<tt>:hour</tt>,
# <tt>:minute</tt>, <tt>:sec</tt>) reset cascadingly, so if only the hour is
...
...
activesupport/lib/active_support/core_ext/time/calculations.rb
浏览文件 @
eba430ae
...
...
@@ -62,6 +62,15 @@ def seconds_since_midnight
to_i
-
change
(
:hour
=>
0
).
to_i
+
(
usec
/
1.0e+6
)
end
# Returns the number of seconds until 23:59:59.
#
# Time.new(2012, 8, 29, 0, 0, 0).seconds_until_end_of_day # => 86399
# Time.new(2012, 8, 29, 12, 34, 56).seconds_until_end_of_day # => 41103
# Time.new(2012, 8, 29, 23, 59, 59).seconds_until_end_of_day # => 0
def
seconds_until_end_of_day
end_of_day
.
to_i
-
to_i
end
# 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,
...
...
activesupport/test/core_ext/date_time_ext_test.rb
浏览文件 @
eba430ae
...
...
@@ -61,6 +61,14 @@ def test_seconds_since_midnight
assert_equal
86399
,
DateTime
.
civil
(
2005
,
1
,
1
,
23
,
59
,
59
).
seconds_since_midnight
end
def
test_seconds_until_end_of_day
assert_equal
0
,
DateTime
.
civil
(
2005
,
1
,
1
,
23
,
59
,
59
).
seconds_until_end_of_day
assert_equal
1
,
DateTime
.
civil
(
2005
,
1
,
1
,
23
,
59
,
58
).
seconds_until_end_of_day
assert_equal
60
,
DateTime
.
civil
(
2005
,
1
,
1
,
23
,
58
,
59
).
seconds_until_end_of_day
assert_equal
3660
,
DateTime
.
civil
(
2005
,
1
,
1
,
22
,
58
,
59
).
seconds_until_end_of_day
assert_equal
86399
,
DateTime
.
civil
(
2005
,
1
,
1
,
0
,
0
,
0
).
seconds_until_end_of_day
end
def
test_beginning_of_day
assert_equal
DateTime
.
civil
(
2005
,
2
,
4
,
0
,
0
,
0
),
DateTime
.
civil
(
2005
,
2
,
4
,
10
,
10
,
10
).
beginning_of_day
end
...
...
activesupport/test/core_ext/time_ext_test.rb
浏览文件 @
eba430ae
...
...
@@ -57,6 +57,54 @@ def test_seconds_since_midnight_at_daylight_savings_time_end
end
end
def
test_seconds_until_end_of_day
assert_equal
0
,
Time
.
local
(
2005
,
1
,
1
,
23
,
59
,
59
).
seconds_until_end_of_day
assert_equal
1
,
Time
.
local
(
2005
,
1
,
1
,
23
,
59
,
58
).
seconds_until_end_of_day
assert_equal
60
,
Time
.
local
(
2005
,
1
,
1
,
23
,
58
,
59
).
seconds_until_end_of_day
assert_equal
3660
,
Time
.
local
(
2005
,
1
,
1
,
22
,
58
,
59
).
seconds_until_end_of_day
assert_equal
86399
,
Time
.
local
(
2005
,
1
,
1
,
0
,
0
,
0
).
seconds_until_end_of_day
end
def
test_seconds_until_end_of_day_at_daylight_savings_time_start
with_env_tz
'US/Eastern'
do
# dt: US: 2005 April 3rd 2:00am ST => April 3rd 3:00am DT
assert_equal
21
*
3600
,
Time
.
local
(
2005
,
4
,
3
,
1
,
59
,
59
).
seconds_until_end_of_day
,
'just before DST start'
assert_equal
21
*
3600
-
2
,
Time
.
local
(
2005
,
4
,
3
,
3
,
0
,
1
).
seconds_until_end_of_day
,
'just after DST start'
end
with_env_tz
'NZ'
do
# dt: New Zealand: 2006 October 1st 2:00am ST => October 1st 3:00am DT
assert_equal
21
*
3600
,
Time
.
local
(
2006
,
10
,
1
,
1
,
59
,
59
).
seconds_until_end_of_day
,
'just before DST start'
assert_equal
21
*
3600
-
2
,
Time
.
local
(
2006
,
10
,
1
,
3
,
0
,
1
).
seconds_until_end_of_day
,
'just after DST start'
end
end
def
test_seconds_until_end_of_day_at_daylight_savings_time_end
with_env_tz
'US/Eastern'
do
# st: US: 2005 October 30th 2:00am DT => October 30th 1:00am ST
# avoid setting a time between 1:00 and 2:00 since that requires specifying whether DST is active
assert_equal
24
*
3600
,
Time
.
local
(
2005
,
10
,
30
,
0
,
59
,
59
).
seconds_until_end_of_day
,
'just before DST end'
assert_equal
22
*
3600
-
2
,
Time
.
local
(
2005
,
10
,
30
,
2
,
0
,
1
).
seconds_until_end_of_day
,
'just after DST end'
# now set a time between 1:00 and 2:00 by specifying whether DST is active
# uses: Time.local( sec, min, hour, day, month, year, wday, yday, isdst, tz )
assert_equal
24
*
3600
-
30
*
60
-
1
,
Time
.
local
(
0
,
30
,
1
,
30
,
10
,
2005
,
0
,
0
,
true
,
ENV
[
'TZ'
]).
seconds_until_end_of_day
,
'before DST end'
assert_equal
23
*
3600
-
30
*
60
-
1
,
Time
.
local
(
0
,
30
,
1
,
30
,
10
,
2005
,
0
,
0
,
false
,
ENV
[
'TZ'
]).
seconds_until_end_of_day
,
'after DST end'
end
with_env_tz
'NZ'
do
# st: New Zealand: 2006 March 19th 3:00am DT => March 19th 2:00am ST
# avoid setting a time between 2:00 and 3:00 since that requires specifying whether DST is active
assert_equal
23
*
3600
,
Time
.
local
(
2006
,
3
,
19
,
1
,
59
,
59
).
seconds_until_end_of_day
,
'just before DST end'
assert_equal
21
*
3600
-
2
,
Time
.
local
(
2006
,
3
,
19
,
3
,
0
,
1
).
seconds_until_end_of_day
,
'just after DST end'
# now set a time between 2:00 and 3:00 by specifying whether DST is active
# uses: Time.local( sec, min, hour, day, month, year, wday, yday, isdst, tz )
assert_equal
23
*
3600
-
30
*
60
-
1
,
Time
.
local
(
0
,
30
,
2
,
19
,
3
,
2006
,
0
,
0
,
true
,
ENV
[
'TZ'
]).
seconds_until_end_of_day
,
'before DST end'
assert_equal
22
*
3600
-
30
*
60
-
1
,
Time
.
local
(
0
,
30
,
2
,
19
,
3
,
2006
,
0
,
0
,
false
,
ENV
[
'TZ'
]).
seconds_until_end_of_day
,
'after DST end'
end
end
def
test_beginning_of_day
assert_equal
Time
.
local
(
2005
,
2
,
4
,
0
,
0
,
0
),
Time
.
local
(
2005
,
2
,
4
,
10
,
10
,
10
).
beginning_of_day
with_env_tz
'US/Eastern'
do
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录