Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
734db10a
Unverified
提交
734db10a
authored
3月 11, 2018
作者:
Noel Grandin
提交者:
GitHub
3月 11, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #937 from ScaY/scay-issue#935
Add "date_trunc" function (only with 'day')
上级
a4fe2e9d
c8cbfba5
显示空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
181 行增加
和
2 行删除
+181
-2
Function.java
h2/src/main/org/h2/expression/Function.java
+13
-1
DateTimeUtils.java
h2/src/main/org/h2/util/DateTimeUtils.java
+44
-0
TestScript.java
h2/src/test/org/h2/test/scripts/TestScript.java
+2
-1
date_trunc.sql
.../org/h2/test/scripts/functions/timeanddate/date_trunc.sql
+122
-0
没有找到文件。
h2/src/main/org/h2/expression/Function.java
浏览文件 @
734db10a
...
@@ -107,7 +107,7 @@ public class Function extends Expression implements FunctionCall {
...
@@ -107,7 +107,7 @@ public class Function extends Expression implements FunctionCall {
SECOND
=
114
,
WEEK
=
115
,
YEAR
=
116
,
CURRENT_DATE
=
117
,
SECOND
=
114
,
WEEK
=
115
,
YEAR
=
116
,
CURRENT_DATE
=
117
,
CURRENT_TIME
=
118
,
CURRENT_TIMESTAMP
=
119
,
EXTRACT
=
120
,
CURRENT_TIME
=
118
,
CURRENT_TIMESTAMP
=
119
,
EXTRACT
=
120
,
FORMATDATETIME
=
121
,
PARSEDATETIME
=
122
,
ISO_YEAR
=
123
,
FORMATDATETIME
=
121
,
PARSEDATETIME
=
122
,
ISO_YEAR
=
123
,
ISO_WEEK
=
124
,
ISO_DAY_OF_WEEK
=
125
;
ISO_WEEK
=
124
,
ISO_DAY_OF_WEEK
=
125
,
DATE_TRUNC
=
132
;
/**
/**
* Pseudo functions for DATEADD, DATEDIFF, and EXTRACT.
* Pseudo functions for DATEADD, DATEDIFF, and EXTRACT.
...
@@ -419,6 +419,7 @@ public class Function extends Expression implements FunctionCall {
...
@@ -419,6 +419,7 @@ public class Function extends Expression implements FunctionCall {
1
,
Value
.
INT
);
1
,
Value
.
INT
);
addFunction
(
"ISO_DAY_OF_WEEK"
,
ISO_DAY_OF_WEEK
,
addFunction
(
"ISO_DAY_OF_WEEK"
,
ISO_DAY_OF_WEEK
,
1
,
Value
.
INT
);
1
,
Value
.
INT
);
addFunction
(
"DATE_TRUNC"
,
DATE_TRUNC
,
2
,
Value
.
NULL
);
// system
// system
addFunctionNotDeterministic
(
"DATABASE"
,
DATABASE
,
addFunctionNotDeterministic
(
"DATABASE"
,
DATABASE
,
0
,
Value
.
STRING
);
0
,
Value
.
STRING
);
...
@@ -1493,6 +1494,13 @@ public class Function extends Expression implements FunctionCall {
...
@@ -1493,6 +1494,13 @@ public class Function extends Expression implements FunctionCall {
break
;
break
;
case
DATE_DIFF:
case
DATE_DIFF:
result
=
ValueLong
.
get
(
datediff
(
v0
.
getString
(),
v1
,
v2
));
result
=
ValueLong
.
get
(
datediff
(
v0
.
getString
(),
v1
,
v2
));
break
;
case
DATE_TRUNC:
// Retrieve the time unit (e.g. 'day', 'microseconds', etc.)
String
timeUnit
=
StringUtils
.
toUpperEnglish
(
v0
.
getString
());
result
=
DateTimeUtils
.
truncateDate
(
timeUnit
,
v1
);
break
;
break
;
case
EXTRACT:
{
case
EXTRACT:
{
int
field
=
getDatePart
(
v0
.
getString
());
int
field
=
getDatePart
(
v0
.
getString
());
...
@@ -2371,6 +2379,10 @@ public class Function extends Expression implements FunctionCall {
...
@@ -2371,6 +2379,10 @@ public class Function extends Expression implements FunctionCall {
min
=
1
;
min
=
1
;
max
=
2
;
max
=
2
;
break
;
break
;
case
DATE_TRUNC:
min
=
2
;
max
=
2
;
break
;
case
TO_CHAR:
case
TO_CHAR:
case
TO_DATE:
case
TO_DATE:
min
=
1
;
min
=
1
;
...
...
h2/src/main/org/h2/util/DateTimeUtils.java
浏览文件 @
734db10a
...
@@ -1471,4 +1471,48 @@ public class DateTimeUtils {
...
@@ -1471,4 +1471,48 @@ public class DateTimeUtils {
return
nanosOfDay
-
mod
;
return
nanosOfDay
-
mod
;
}
}
/**
* Truncate the given date to 'day'
*
* @param timeUnit the time unit (e.g. 'DAY', 'HOUR', etc.)
* @param value the date
* @return date truncated to 'day'
*/
public
static
Value
truncateDate
(
String
timeUnit
,
Value
value
)
{
Value
result
=
null
;
// Retrieve the dateValue.
long
[]
fieldDateAndTime
=
DateTimeUtils
.
dateAndTimeFromValue
(
value
);
long
dateValue
=
fieldDateAndTime
[
0
];
// Case where the date has to be truncated to the day.
if
(
timeUnit
.
equals
(
"DAY"
))
{
if
(
value
instanceof
ValueTimestampTimeZone
)
{
// Create a new ValueTimestampTimeZone by only setting the
// date. The time in nanoseconds since midnight will be set
// to 0.
ValueTimestampTimeZone
vTmp
=
(
ValueTimestampTimeZone
)
value
;
result
=
ValueTimestampTimeZone
.
fromDateValueAndNanos
(
vTmp
.
getDateValue
(),
0
,
vTmp
.
getTimeZoneOffsetMins
());
}
else
{
// By default, we create a timestamp by setting the
// datevalue to the datevalue retrieved and the time in
// nanoseconds since midnight to 0.
result
=
ValueTimestamp
.
fromDateValueAndNanos
(
dateValue
,
0
);
}
}
else
{
// Return an exception for the other possible value (not yet
// supported).
throw
DbException
.
getUnsupportedException
(
timeUnit
);
}
return
result
;
}
}
}
h2/src/test/org/h2/test/scripts/TestScript.java
浏览文件 @
734db10a
...
@@ -147,9 +147,10 @@ public class TestScript extends TestBase {
...
@@ -147,9 +147,10 @@ public class TestScript extends TestBase {
"current-time"
,
"dateadd"
,
"datediff"
,
"dayname"
,
"current-time"
,
"dateadd"
,
"datediff"
,
"dayname"
,
"day-of-month"
,
"day-of-week"
,
"day-of-year"
,
"extract"
,
"day-of-month"
,
"day-of-week"
,
"day-of-year"
,
"extract"
,
"formatdatetime"
,
"hour"
,
"minute"
,
"month"
,
"monthname"
,
"formatdatetime"
,
"hour"
,
"minute"
,
"month"
,
"monthname"
,
"parsedatetime"
,
"quarter"
,
"second"
,
"truncate"
,
"week"
,
"year"
})
{
"parsedatetime"
,
"quarter"
,
"second"
,
"truncate"
,
"week"
,
"year"
,
"date_trunc"
})
{
testScript
(
"functions/timeanddate/"
+
s
+
".sql"
);
testScript
(
"functions/timeanddate/"
+
s
+
".sql"
);
}
}
deleteDb
(
"script"
);
deleteDb
(
"script"
);
System
.
out
.
flush
();
System
.
out
.
flush
();
}
}
...
...
h2/src/test/org/h2/test/scripts/functions/timeanddate/date_trunc.sql
0 → 100644
浏览文件 @
734db10a
select
DATE_TRUNC
(
'day'
,
time
'00:00:00'
);
>>
1970
-
01
-
01
00
:
00
:
00
select
DATE_TRUNC
(
'DAY'
,
time
'00:00:00'
);
>>
1970
-
01
-
01
00
:
00
:
00
select
DATE_TRUNC
(
'day'
,
time
'15:14:13'
);
>>
1970
-
01
-
01
00
:
00
:
00
select
DATE_TRUNC
(
'DAY'
,
time
'15:14:13'
);
>>
1970
-
01
-
01
00
:
00
:
00
select
DATE_TRUNC
(
'day'
,
date
'2015-05-29'
);
>>
2015
-
05
-
29
00
:
00
:
00
select
DATE_TRUNC
(
'DAY'
,
date
'2015-05-29'
);
>>
2015
-
05
-
29
00
:
00
:
00
select
DATE_TRUNC
(
'day'
,
timestamp
'2015-05-29 15:14:13'
);
>>
2015
-
05
-
29
00
:
00
:
00
select
DATE_TRUNC
(
'DAY'
,
timestamp
'2015-05-29 15:14:13'
);
>>
2015
-
05
-
29
00
:
00
:
00
select
DATE_TRUNC
(
'day'
,
timestamp
with
time
zone
'2015-05-29 15:14:13'
);
>>
2015
-
05
-
29
00
:
00
:
00
+
00
select
DATE_TRUNC
(
'DAY'
,
timestamp
with
time
zone
'2015-05-29 15:14:13'
);
>>
2015
-
05
-
29
00
:
00
:
00
+
00
select
DATE_TRUNC
(
'day'
,
timestamp
with
time
zone
'2015-05-29 05:14:13-06'
);
>>
2015
-
05
-
29
00
:
00
:
00
-
06
select
DATE_TRUNC
(
'DAY'
,
timestamp
with
time
zone
'2015-05-29 05:14:13-06'
);
>>
2015
-
05
-
29
00
:
00
:
00
-
06
select
DATE_TRUNC
(
'day'
,
timestamp
with
time
zone
'2015-05-29 15:14:13+10'
);
>>
2015
-
05
-
29
00
:
00
:
00
+
10
select
DATE_TRUNC
(
'DAY'
,
timestamp
with
time
zone
'2015-05-29 15:14:13+10'
);
>>
2015
-
05
-
29
00
:
00
:
00
+
10
select
DATE_TRUNC
(
'day'
,
'2015-05-29 15:14:13'
);
>>
2015
-
05
-
29
00
:
00
:
00
select
DATE_TRUNC
(
'DAY'
,
'2015-05-29 15:14:13'
);
>>
2015
-
05
-
29
00
:
00
:
00
SELECT
DATE_TRUNC
(
'---'
,
'2015-05-29 15:14:13'
);
>
exception
SELECT
DATE_TRUNC
(
'microseconds'
,
'2015-05-29 15:14:13'
);
>
exception
SELECT
DATE_TRUNC
(
'MICROSECONDS'
,
'2015-05-29 15:14:13'
);
>
exception
SELECT
DATE_TRUNC
(
'milliseconds'
,
'2015-05-29 15:14:13'
);
>
exception
SELECT
DATE_TRUNC
(
'MILLISECONDS'
,
'2015-05-29 15:14:13'
);
>
exception
SELECT
DATE_TRUNC
(
'second'
,
'2015-05-29 15:14:13'
);
>
exception
SELECT
DATE_TRUNC
(
'SECOND'
,
'2015-05-29 15:14:13'
);
>
exception
SELECT
DATE_TRUNC
(
'minute'
,
'2015-05-29 15:14:13'
);
>
exception
SELECT
DATE_TRUNC
(
'MINUTE'
,
'2015-05-29 15:14:13'
);
>
exception
SELECT
DATE_TRUNC
(
'hour'
,
'2015-05-29 15:14:13'
);
>
exception
SELECT
DATE_TRUNC
(
'HOUR'
,
'2015-05-29 15:14:13'
);
>
exception
SELECT
DATE_TRUNC
(
'week'
,
'2015-05-29 15:14:13'
);
>
exception
SELECT
DATE_TRUNC
(
'WEEK'
,
'2015-05-29 15:14:13'
);
>
exception
SELECT
DATE_TRUNC
(
'month'
,
'2015-05-29 15:14:13'
);
>
exception
SELECT
DATE_TRUNC
(
'MONTH'
,
'2015-05-29 15:14:13'
);
>
exception
SELECT
DATE_TRUNC
(
'quarter'
,
'2015-05-29 15:14:13'
);
>
exception
SELECT
DATE_TRUNC
(
'QUARTER'
,
'2015-05-29 15:14:13'
);
>
exception
SELECT
DATE_TRUNC
(
'year'
,
'2015-05-29 15:14:13'
);
>
exception
SELECT
DATE_TRUNC
(
'YEAR'
,
'2015-05-29 15:14:13'
);
>
exception
SELECT
DATE_TRUNC
(
'decade'
,
'2015-05-29 15:14:13'
);
>
exception
SELECT
DATE_TRUNC
(
'DECADE'
,
'2015-05-29 15:14:13'
);
>
exception
SELECT
DATE_TRUNC
(
'century'
,
'2015-05-29 15:14:13'
);
>
exception
SELECT
DATE_TRUNC
(
'CENTURY'
,
'2015-05-29 15:14:13'
);
>
exception
SELECT
DATE_TRUNC
(
'millennium'
,
'2015-05-29 15:14:13'
);
>
exception
SELECT
DATE_TRUNC
(
'MILLENNIUM'
,
'2015-05-29 15:14:13'
);
>
exception
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论