Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
53c1cd6c
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,发现更多精彩内容 >>
提交
53c1cd6c
编写于
5月 05, 2010
作者:
X
Xavier Noria
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
let Time.time_with_datetime_fallback handle properly years in the range 0..138
上级
38da0ace
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
30 addition
and
4 deletion
+30
-4
activesupport/lib/active_support/core_ext/date_time/conversions.rb
...port/lib/active_support/core_ext/date_time/conversions.rb
+6
-0
activesupport/lib/active_support/core_ext/time/calculations.rb
...esupport/lib/active_support/core_ext/time/calculations.rb
+5
-3
activesupport/test/core_ext/date_ext_test.rb
activesupport/test/core_ext/date_ext_test.rb
+7
-1
activesupport/test/core_ext/date_time_ext_test.rb
activesupport/test/core_ext/date_time_ext_test.rb
+5
-0
activesupport/test/core_ext/time_ext_test.rb
activesupport/test/core_ext/time_ext_test.rb
+7
-0
未找到文件。
activesupport/lib/active_support/core_ext/date_time/conversions.rb
浏览文件 @
53c1cd6c
require
'active_support/inflector'
require
'active_support/core_ext/time/conversions'
require
'active_support/core_ext/date_time/calculations'
class
DateTime
# Ruby 1.9 has DateTime#to_time which internally relies on Time. We define our own #to_time which allows
...
...
@@ -72,6 +73,11 @@ def to_datetime
self
end
unless
method_defined?
(
:to_datetime
)
def
self
.
civil_from_format
(
utc_or_local
,
year
,
month
=
1
,
day
=
1
,
hour
=
0
,
min
=
0
,
sec
=
0
)
offset
=
utc_or_local
.
to_sym
==
:local
?
local_offset
:
0
civil
(
year
,
month
,
day
,
hour
,
min
,
sec
,
offset
)
end
# Converts datetime to an appropriate format for use in XML
def
xmlschema
strftime
(
"%Y-%m-%dT%H:%M:%S%Z"
)
...
...
activesupport/lib/active_support/core_ext/time/calculations.rb
浏览文件 @
53c1cd6c
require
'active_support/duration'
require
'active_support/core_ext/date/acts_like'
require
'active_support/core_ext/date/calculations'
require
'active_support/core_ext/date_time/conversions'
class
Time
COMMON_YEAR_DAYS_IN_MONTH
=
[
nil
,
31
,
28
,
31
,
30
,
31
,
30
,
31
,
31
,
30
,
31
,
30
,
31
]
...
...
@@ -23,10 +24,11 @@ def days_in_month(month, year = now.year)
# (i.e., if year is within either 1970..2038 or 1902..2038, depending on system architecture);
# otherwise returns a DateTime
def
time_with_datetime_fallback
(
utc_or_local
,
year
,
month
=
1
,
day
=
1
,
hour
=
0
,
min
=
0
,
sec
=
0
,
usec
=
0
)
::
Time
.
send
(
utc_or_local
,
year
,
month
,
day
,
hour
,
min
,
sec
,
usec
)
time
=
::
Time
.
send
(
utc_or_local
,
year
,
month
,
day
,
hour
,
min
,
sec
,
usec
)
# This check is needed because Time.utc(y) returns a time object in the 2000s for 0 <= y <= 138.
time
.
year
==
year
?
time
:
::
DateTime
.
civil_from_format
(
utc_or_local
,
year
,
month
,
day
,
hour
,
min
,
sec
)
rescue
offset
=
utc_or_local
.
to_sym
==
:local
?
::
DateTime
.
local_offset
:
0
::
DateTime
.
civil
(
year
,
month
,
day
,
hour
,
min
,
sec
,
offset
)
::
DateTime
.
civil_from_format
(
utc_or_local
,
year
,
month
,
day
,
hour
,
min
,
sec
)
end
# Wraps class method +time_with_datetime_fallback+ with +utc_or_local+ set to <tt>:utc</tt>.
...
...
activesupport/test/core_ext/date_ext_test.rb
浏览文件 @
53c1cd6c
...
...
@@ -20,6 +20,13 @@ def test_readable_inspect
def
test_to_time
assert_equal
Time
.
local
(
2005
,
2
,
21
),
Date
.
new
(
2005
,
2
,
21
).
to_time
assert_equal
Time
.
local_time
(
2039
,
2
,
21
),
Date
.
new
(
2039
,
2
,
21
).
to_time
silence_warnings
do
0
.
upto
(
138
)
do
|
year
|
[
:utc
,
:local
].
each
do
|
format
|
assert_equal
year
,
Date
.
new
(
year
).
to_time
(
format
).
year
end
end
end
end
def
test_to_datetime
...
...
@@ -89,7 +96,6 @@ def test_end_of_month
assert_equal
Date
.
new
(
2005
,
3
,
31
),
Date
.
new
(
2005
,
3
,
20
).
end_of_month
assert_equal
Date
.
new
(
2005
,
2
,
28
),
Date
.
new
(
2005
,
2
,
20
).
end_of_month
assert_equal
Date
.
new
(
2005
,
4
,
30
),
Date
.
new
(
2005
,
4
,
20
).
end_of_month
end
def
test_beginning_of_year
...
...
activesupport/test/core_ext/date_time_ext_test.rb
浏览文件 @
53c1cd6c
...
...
@@ -40,6 +40,11 @@ def test_to_time
assert_equal
DateTime
.
new
(
2005
,
2
,
21
,
10
,
11
,
12
,
Rational
(
-
5
,
24
)),
DateTime
.
new
(
2005
,
2
,
21
,
10
,
11
,
12
,
Rational
(
-
5
,
24
)).
to_time
end
def
test_civil_from_format
assert_equal
DateTime
.
civil
(
2010
,
5
,
4
,
0
,
0
,
0
,
DateTime
.
local_offset
),
DateTime
.
civil_from_format
(
:local
,
2010
,
5
,
4
)
assert_equal
DateTime
.
civil
(
2010
,
5
,
4
,
0
,
0
,
0
,
0
),
DateTime
.
civil_from_format
(
:utc
,
2010
,
5
,
4
)
end
def
test_seconds_since_midnight
assert_equal
1
,
DateTime
.
civil
(
2005
,
1
,
1
,
0
,
0
,
1
).
seconds_since_midnight
assert_equal
60
,
DateTime
.
civil
(
2005
,
1
,
1
,
0
,
1
,
0
).
seconds_since_midnight
...
...
activesupport/test/core_ext/time_ext_test.rb
浏览文件 @
53c1cd6c
...
...
@@ -587,6 +587,13 @@ def test_time_with_datetime_fallback
DateTime
.
civil
(
2039
,
2
,
21
,
17
,
44
,
30
,
0
,
0
)
assert_equal
::
Date
::
ITALY
,
Time
.
time_with_datetime_fallback
(
:utc
,
2039
,
2
,
21
,
17
,
44
,
30
,
1
).
start
# use Ruby's default start value
end
silence_warnings
do
0
.
upto
(
138
)
do
|
year
|
[
:utc
,
:local
].
each
do
|
format
|
assert_equal
year
,
Time
.
time_with_datetime_fallback
(
format
,
year
).
year
end
end
end
end
def
test_utc_time
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录