Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
726b5d79
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,发现更多精彩内容 >>
提交
726b5d79
编写于
4月 22, 2010
作者:
D
Daniel Neighman
提交者:
wycats
4月 21, 2010
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
updates String#to_(date|date_time|time) to return nil for blank strings
上级
b73177a0
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
9 addition
and
3 deletion
+9
-3
activesupport/lib/active_support/core_ext/string/conversions.rb
...support/lib/active_support/core_ext/string/conversions.rb
+3
-0
activesupport/test/core_ext/string_ext_test.rb
activesupport/test/core_ext/string_ext_test.rb
+6
-3
未找到文件。
activesupport/lib/active_support/core_ext/string/conversions.rb
浏览文件 @
726b5d79
...
...
@@ -30,16 +30,19 @@ def ord
# Form can be either :utc (default) or :local.
def
to_time
(
form
=
:utc
)
return
nil
if
self
.
blank?
d
=
::
Date
.
_parse
(
self
,
false
).
values_at
(
:year
,
:mon
,
:mday
,
:hour
,
:min
,
:sec
,
:sec_fraction
).
map
{
|
arg
|
arg
||
0
}
d
[
6
]
*=
1000000
::
Time
.
send
(
"
#{
form
}
_time"
,
*
d
)
end
def
to_date
return
nil
if
self
.
blank?
::
Date
.
new
(
*::
Date
.
_parse
(
self
,
false
).
values_at
(
:year
,
:mon
,
:mday
))
end
def
to_datetime
return
nil
if
self
.
blank?
d
=
::
Date
.
_parse
(
self
,
false
).
values_at
(
:year
,
:mon
,
:mday
,
:hour
,
:min
,
:sec
,
:zone
,
:sec_fraction
).
map
{
|
arg
|
arg
||
0
}
d
[
5
]
+=
d
.
pop
::
DateTime
.
civil
(
*
d
)
...
...
activesupport/test/core_ext/string_ext_test.rb
浏览文件 @
726b5d79
...
...
@@ -117,17 +117,20 @@ def test_string_to_time
assert_equal
Time
.
local
(
2005
,
2
,
27
,
23
,
50
,
19
,
275038
),
"2005-02-27T23:50:19.275038"
.
to_time
(
:local
)
assert_equal
DateTime
.
civil
(
2039
,
2
,
27
,
23
,
50
),
"2039-02-27 23:50"
.
to_time
assert_equal
Time
.
local_time
(
2039
,
2
,
27
,
23
,
50
),
"2039-02-27 23:50"
.
to_time
(
:local
)
assert_equal
nil
,
""
.
to_time
end
def
test_string_to_datetime
assert_equal
DateTime
.
civil
(
2039
,
2
,
27
,
23
,
50
),
"2039-02-27 23:50"
.
to_datetime
assert_equal
0
,
"2039-02-27 23:50"
.
to_datetime
.
offset
# use UTC offset
assert_equal
::
Date
::
ITALY
,
"2039-02-27 23:50"
.
to_datetime
.
start
# use Ruby's default start value
assert_equal
DateTime
.
civil
(
2039
,
2
,
27
,
23
,
50
,
19
+
Rational
(
275038
,
1000000
),
"-04:00"
),
"2039-02-27T23:50:19.275038-04:00"
.
to_datetime
assert_equal
nil
,
""
.
to_datetime
end
def
test_string_to_date
assert_equal
Date
.
new
(
2005
,
2
,
27
),
"2005-02-27"
.
to_date
assert_equal
nil
,
""
.
to_date
end
def
test_access
...
...
@@ -255,7 +258,7 @@ def test_mb_chars_returns_string
string.rb - Interpolation for String.
Copyright (C) 2005-2009 Masao Mutoh
You may redistribute it and/or modify it under the same
license terms as Ruby.
=end
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录