Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
aeecde02
Unverified
提交
aeecde02
authored
2月 11, 2018
作者:
Noel Grandin
提交者:
GitHub
2月 11, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #864 from katzyn/datetime
Minor changes in DateUtils and Function
上级
705e592e
dedc5069
隐藏空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
36 行增加
和
65 行删除
+36
-65
Function.java
h2/src/main/org/h2/expression/Function.java
+2
-9
DateTimeUtils.java
h2/src/main/org/h2/util/DateTimeUtils.java
+1
-21
TestFunctions.java
h2/src/test/org/h2/test/db/TestFunctions.java
+0
-34
TestScript.java
h2/src/test/org/h2/test/scripts/TestScript.java
+1
-1
add_months.sql
.../org/h2/test/scripts/functions/timeanddate/add_months.sql
+32
-0
没有找到文件。
h2/src/main/org/h2/expression/Function.java
浏览文件 @
aeecde02
...
...
@@ -1326,7 +1326,7 @@ public class Function extends Expression implements FunctionCall {
if
(
s2
==
null
)
{
s2
=
""
;
}
result
=
ValueString
.
get
(
replace
(
s0
,
s1
,
s2
),
result
=
ValueString
.
get
(
StringUtils
.
replaceAll
(
s0
,
s1
,
s2
),
database
.
getMode
().
treatEmptyStringsAsNull
);
}
break
;
...
...
@@ -1471,7 +1471,7 @@ public class Function extends Expression implements FunctionCall {
v1
==
null
?
null
:
v1
.
getString
()));
break
;
case
ADD_MONTHS:
result
=
ValueTimestamp
.
get
(
DateTimeUtils
.
addMonths
(
v0
.
getTimestamp
(),
v1
.
getInt
())
);
result
=
dateadd
(
"MONTH"
,
v1
.
getInt
(),
v0
);
break
;
case
TRANSLATE:
{
String
matching
=
v1
.
getString
();
...
...
@@ -1980,13 +1980,6 @@ public class Function extends Expression implements FunctionCall {
return
s
.
substring
(
start
,
start
+
length
);
}
private
static
String
replace
(
String
s
,
String
replace
,
String
with
)
{
if
(
s
==
null
||
replace
==
null
||
with
==
null
)
{
return
null
;
}
return
StringUtils
.
replaceAll
(
s
,
replace
,
with
);
}
private
static
String
repeat
(
String
s
,
int
count
)
{
StringBuilder
buff
=
new
StringBuilder
(
s
.
length
()
*
count
);
while
(
count
--
>
0
)
{
...
...
h2/src/main/org/h2/util/DateTimeUtils.java
浏览文件 @
aeecde02
...
...
@@ -525,7 +525,7 @@ public class DateTimeUtils {
if
(
month
==
2
)
{
maxDay
=
c
.
isLeapYear
(
year
)
?
29
:
28
;
}
else
{
maxDay
=
30
+
((
month
+
(
month
>
7
?
1
:
0
))
&
1
)
;
maxDay
=
NORMAL_DAYS_PER_MONTH
[
month
]
;
}
if
(
day
<
1
||
day
>
maxDay
)
{
throw
e
;
...
...
@@ -1222,26 +1222,6 @@ public class DateTimeUtils {
return
dateValue
(
y
,
m
+
3
,
(
int
)
d
);
}
/**
* Adds the number of months to the date. If the resulting month's number of
* days is less than the original's day-of-month, the resulting
* day-of-months gets adjusted accordingly: <br>
* 30.04.2007 - 2 months = 28.02.2007
*
* @param refDate the original date
* @param nrOfMonthsToAdd the number of months to add
* @return the new timestamp
*/
public
static
Timestamp
addMonths
(
Timestamp
refDate
,
int
nrOfMonthsToAdd
)
{
Calendar
calendar
=
DateTimeUtils
.
createGregorianCalendar
();
calendar
.
setTime
(
refDate
);
calendar
.
add
(
Calendar
.
MONTH
,
nrOfMonthsToAdd
);
Timestamp
resultDate
=
new
Timestamp
(
calendar
.
getTimeInMillis
());
resultDate
.
setNanos
(
refDate
.
getNanos
());
return
resultDate
;
}
/**
* Append a date to the string builder.
*
...
...
h2/src/test/org/h2/test/db/TestFunctions.java
浏览文件 @
aeecde02
...
...
@@ -82,7 +82,6 @@ public class TestFunctions extends TestBase implements AggregateFunction {
testIfNull
();
testToDate
();
testToDateException
();
testAddMonths
();
testDataType
();
testVersion
();
testFunctionTable
();
...
...
@@ -1428,39 +1427,6 @@ public class TestFunctions extends TestBase implements AggregateFunction {
date
.
setTime
(
c
.
getTimeInMillis
());
}
private
void
testAddMonths
()
throws
ParseException
{
Timestamp
date
;
Timestamp
expected
;
// 01-Aug-03 + 3 months = 01-Nov-03
date
=
new
Timestamp
(
new
SimpleDateFormat
(
"yyyy-MM-dd"
).
parse
(
"2003-08-01"
).
getTime
());
expected
=
new
Timestamp
(
new
SimpleDateFormat
(
"yyyy-MM-dd"
).
parse
(
"2003-11-01"
).
getTime
());
assertEquals
(
expected
,
DateTimeUtils
.
addMonths
(
new
Timestamp
(
date
.
getTime
()),
3
));
// 31-Jan-03 + 1 month = 28-Feb-2003
date
=
new
Timestamp
(
new
SimpleDateFormat
(
"yyyy-MM-dd"
).
parse
(
"2003-01-31"
).
getTime
());
expected
=
new
Timestamp
(
new
SimpleDateFormat
(
"yyyy-MM-dd"
).
parse
(
"2003-02-28"
).
getTime
());
assertEquals
(
expected
,
DateTimeUtils
.
addMonths
(
new
Timestamp
(
date
.
getTime
()),
1
));
// 21-Aug-2003 - 3 months = 21-May-2003
date
=
new
Timestamp
(
new
SimpleDateFormat
(
"yyyy-MM-dd"
).
parse
(
"2003-08-21"
).
getTime
());
expected
=
new
Timestamp
(
new
SimpleDateFormat
(
"yyyy-MM-dd"
).
parse
(
"2003-05-21"
).
getTime
());
assertEquals
(
expected
,
DateTimeUtils
.
addMonths
(
new
Timestamp
(
date
.
getTime
()),
-
3
));
// 21-Aug-2003 00:00:00:333 - 3 months = 21-May-2003 00:00:00:333
date
=
new
Timestamp
(
new
SimpleDateFormat
(
"yyyy-MM-dd SSS"
).
parse
(
"2003-08-21 333"
).
getTime
());
expected
=
new
Timestamp
(
new
SimpleDateFormat
(
"yyyy-MM-dd SSS"
).
parse
(
"2003-05-21 333"
).
getTime
());
assertEquals
(
expected
,
DateTimeUtils
.
addMonths
(
new
Timestamp
(
date
.
getTime
()),
-
3
));
}
private
void
testToCharFromDateTime
()
throws
SQLException
{
deleteDb
(
"functions"
);
Connection
conn
=
getConnection
(
"functions"
);
...
...
h2/src/test/org/h2/test/scripts/TestScript.java
浏览文件 @
aeecde02
...
...
@@ -135,7 +135,7 @@ public class TestScript extends TestBase {
"set"
,
"table"
,
"transaction-id"
,
"truncate-value"
,
"user"
})
{
testScript
(
"functions/system/"
+
s
+
".sql"
);
}
for
(
String
s
:
new
String
[]
{
"current_date"
,
"current_timestamp"
,
for
(
String
s
:
new
String
[]
{
"
add_months"
,
"
current_date"
,
"current_timestamp"
,
"current-time"
,
"dateadd"
,
"datediff"
,
"dayname"
,
"day-of-month"
,
"day-of-week"
,
"day-of-year"
,
"extract"
,
"formatdatetime"
,
"hour"
,
"minute"
,
"month"
,
"monthname"
,
...
...
h2/src/test/org/h2/test/scripts/functions/timeanddate/add_months.sql
0 → 100644
浏览文件 @
aeecde02
-- Copyright 2004-2018 H2 Group. Multiple-Licensed under the MPL 2.0,
-- and the EPL 1.0 (http://h2database.com/html/license.html).
-- Initial Developer: H2 Group
--
-- 01-Aug-03 + 3 months = 01-Nov-03
SELECT
ADD_MONTHS
(
'2003-08-01'
,
3
)
AS
R
;
>
R
>
---------------------
>
2003
-
11
-
01
00
:
00
:
00
.
0
>
rows
:
1
-- 31-Jan-03 + 1 month = 28-Feb-2003
SELECT
ADD_MONTHS
(
'2003-01-31'
,
1
)
AS
R
;
>
R
>
---------------------
>
2003
-
02
-
28
00
:
00
:
00
.
0
>
rows
:
1
-- 21-Aug-2003 - 3 months = 21-May-2003
SELECT
ADD_MONTHS
(
'2003-08-21'
,
-
3
)
AS
R
;
>
R
>
---------------------
>
2003
-
05
-
21
00
:
00
:
00
.
0
>
rows
:
1
-- 21-Aug-2003 00:00:00.333 - 3 months = 21-May-2003 00:00:00.333
SELECT
ADD_MONTHS
(
'2003-08-21 00:00:00.333'
,
-
3
)
AS
R
;
>
R
>
-----------------------
>
2003
-
05
-
21
00
:
00
:
00
.
333
>
rows
:
1
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论