Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
9098cb27
Unverified
提交
9098cb27
authored
2月 07, 2018
作者:
Noel Grandin
提交者:
GitHub
2月 07, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #847 from katzyn/datetime
Reimplement remaining parts of EXTRACT, ISO_YEAR, etc without a Calendar
上级
5a61101b
8f5800e8
全部展开
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
395 行增加
和
252 行删除
+395
-252
Function.java
h2/src/main/org/h2/expression/Function.java
+89
-80
DateTimeUtils.java
h2/src/main/org/h2/util/DateTimeUtils.java
+152
-73
TestDateIso8601.java
h2/src/test/org/h2/test/unit/TestDateIso8601.java
+98
-99
TestDateTimeUtils.java
h2/src/test/org/h2/test/unit/TestDateTimeUtils.java
+56
-0
没有找到文件。
h2/src/main/org/h2/expression/Function.java
浏览文件 @
9098cb27
...
...
@@ -108,6 +108,11 @@ public class Function extends Expression implements FunctionCall {
FORMATDATETIME
=
121
,
PARSEDATETIME
=
122
,
ISO_YEAR
=
123
,
ISO_WEEK
=
124
,
ISO_DAY_OF_WEEK
=
125
;
/**
* Pseudo function for {@code EXTRACT(MILLISECOND FROM ...)}.
*/
public
static
final
int
MILLISECOND
=
126
;
public
static
final
int
DATABASE
=
150
,
USER
=
151
,
CURRENT_USER
=
152
,
IDENTITY
=
153
,
SCOPE_IDENTITY
=
154
,
AUTOCOMMIT
=
155
,
READONLY
=
156
,
DATABASE_PATH
=
157
,
LOCK_TIMEOUT
=
158
,
...
...
@@ -158,39 +163,39 @@ public class Function extends Expression implements FunctionCall {
static
{
// DATE_PART
DATE_PART
.
put
(
"SQL_TSI_YEAR"
,
Calendar
.
YEAR
);
DATE_PART
.
put
(
"YEAR"
,
Calendar
.
YEAR
);
DATE_PART
.
put
(
"YYYY"
,
Calendar
.
YEAR
);
DATE_PART
.
put
(
"YY"
,
Calendar
.
YEAR
);
DATE_PART
.
put
(
"SQL_TSI_MONTH"
,
Calendar
.
MONTH
);
DATE_PART
.
put
(
"MONTH"
,
Calendar
.
MONTH
);
DATE_PART
.
put
(
"MM"
,
Calendar
.
MONTH
);
DATE_PART
.
put
(
"M"
,
Calendar
.
MONTH
);
DATE_PART
.
put
(
"SQL_TSI_WEEK"
,
Calendar
.
WEEK_OF_YEAR
);
DATE_PART
.
put
(
"WW"
,
Calendar
.
WEEK_OF_YEAR
);
DATE_PART
.
put
(
"WK"
,
Calendar
.
WEEK_OF_YEAR
);
DATE_PART
.
put
(
"WEEK"
,
Calendar
.
WEEK_OF_YEAR
);
DATE_PART
.
put
(
"DAY"
,
Calendar
.
DAY_OF_MONTH
);
DATE_PART
.
put
(
"DD"
,
Calendar
.
DAY_OF_MONTH
);
DATE_PART
.
put
(
"D"
,
Calendar
.
DAY_OF_MONTH
);
DATE_PART
.
put
(
"SQL_TSI_DAY"
,
Calendar
.
DAY_OF_MONTH
);
DATE_PART
.
put
(
"DAYOFYEAR"
,
Calendar
.
DAY_OF_YEAR
);
DATE_PART
.
put
(
"DAY_OF_YEAR"
,
Calendar
.
DAY_OF_YEAR
);
DATE_PART
.
put
(
"DY"
,
Calendar
.
DAY_OF_YEAR
);
DATE_PART
.
put
(
"DOY"
,
Calendar
.
DAY_OF_YEAR
);
DATE_PART
.
put
(
"SQL_TSI_HOUR"
,
Calendar
.
HOUR_OF_DAY
);
DATE_PART
.
put
(
"HOUR"
,
Calendar
.
HOUR_OF_DAY
);
DATE_PART
.
put
(
"HH"
,
Calendar
.
HOUR_OF_DAY
);
DATE_PART
.
put
(
"SQL_TSI_MINUTE"
,
Calendar
.
MINUTE
);
DATE_PART
.
put
(
"MINUTE"
,
Calendar
.
MINUTE
);
DATE_PART
.
put
(
"MI"
,
Calendar
.
MINUTE
);
DATE_PART
.
put
(
"N"
,
Calendar
.
MINUTE
);
DATE_PART
.
put
(
"SQL_TSI_SECOND"
,
Calendar
.
SECOND
);
DATE_PART
.
put
(
"SECOND"
,
Calendar
.
SECOND
);
DATE_PART
.
put
(
"SS"
,
Calendar
.
SECOND
);
DATE_PART
.
put
(
"S"
,
Calendar
.
SECOND
);
DATE_PART
.
put
(
"MILLISECOND"
,
Calendar
.
MILLISECOND
);
DATE_PART
.
put
(
"MS"
,
Calendar
.
MILLISECOND
);
DATE_PART
.
put
(
"SQL_TSI_YEAR"
,
YEAR
);
DATE_PART
.
put
(
"YEAR"
,
YEAR
);
DATE_PART
.
put
(
"YYYY"
,
YEAR
);
DATE_PART
.
put
(
"YY"
,
YEAR
);
DATE_PART
.
put
(
"SQL_TSI_MONTH"
,
MONTH
);
DATE_PART
.
put
(
"MONTH"
,
MONTH
);
DATE_PART
.
put
(
"MM"
,
MONTH
);
DATE_PART
.
put
(
"M"
,
MONTH
);
DATE_PART
.
put
(
"SQL_TSI_WEEK"
,
WEEK
);
DATE_PART
.
put
(
"WW"
,
WEEK
);
DATE_PART
.
put
(
"WK"
,
WEEK
);
DATE_PART
.
put
(
"WEEK"
,
WEEK
);
DATE_PART
.
put
(
"DAY"
,
DAY_OF_MONTH
);
DATE_PART
.
put
(
"DD"
,
DAY_OF_MONTH
);
DATE_PART
.
put
(
"D"
,
DAY_OF_MONTH
);
DATE_PART
.
put
(
"SQL_TSI_DAY"
,
DAY_OF_MONTH
);
DATE_PART
.
put
(
"DAYOFYEAR"
,
DAY_OF_YEAR
);
DATE_PART
.
put
(
"DAY_OF_YEAR"
,
DAY_OF_YEAR
);
DATE_PART
.
put
(
"DY"
,
DAY_OF_YEAR
);
DATE_PART
.
put
(
"DOY"
,
DAY_OF_YEAR
);
DATE_PART
.
put
(
"SQL_TSI_HOUR"
,
HOUR
);
DATE_PART
.
put
(
"HOUR"
,
HOUR
);
DATE_PART
.
put
(
"HH"
,
HOUR
);
DATE_PART
.
put
(
"SQL_TSI_MINUTE"
,
MINUTE
);
DATE_PART
.
put
(
"MINUTE"
,
MINUTE
);
DATE_PART
.
put
(
"MI"
,
MINUTE
);
DATE_PART
.
put
(
"N"
,
MINUTE
);
DATE_PART
.
put
(
"SQL_TSI_SECOND"
,
SECOND
);
DATE_PART
.
put
(
"SECOND"
,
SECOND
);
DATE_PART
.
put
(
"SS"
,
SECOND
);
DATE_PART
.
put
(
"S"
,
SECOND
);
DATE_PART
.
put
(
"MILLISECOND"
,
MILLISECOND
);
DATE_PART
.
put
(
"MS"
,
MILLISECOND
);
// SOUNDEX_INDEX
String
index
=
"7AEIOUY8HW1BFPV2CGJKQSXZ3DT4L5MN6R"
;
...
...
@@ -837,22 +842,19 @@ public class Function extends Expression implements FunctionCall {
break
;
}
case
DAY_OF_MONTH:
result
=
ValueInt
.
get
(
DateTimeUtils
.
getDatePart
(
v0
,
Calendar
.
DAY_OF_MONTH
));
break
;
case
DAY_OF_WEEK:
result
=
ValueInt
.
get
(
DateTimeUtils
.
getDatePart
(
v0
,
Calendar
.
DAY_OF_WEEK
));
break
;
case
DAY_OF_YEAR:
result
=
ValueInt
.
get
(
DateTimeUtils
.
getDatePart
(
v0
,
Calendar
.
DAY_OF_YEAR
));
break
;
case
HOUR:
result
=
ValueInt
.
get
(
DateTimeUtils
.
getDatePart
(
v0
,
Calendar
.
HOUR_OF_DAY
));
break
;
case
MINUTE:
result
=
ValueInt
.
get
(
DateTimeUtils
.
getDatePart
(
v0
,
Calendar
.
MINUTE
));
break
;
case
MONTH:
result
=
ValueInt
.
get
(
DateTimeUtils
.
getDatePart
(
v0
,
Calendar
.
MONTH
));
case
QUARTER:
case
ISO_YEAR:
case
ISO_WEEK:
case
ISO_DAY_OF_WEEK:
case
SECOND:
case
WEEK:
case
YEAR:
result
=
ValueInt
.
get
(
DateTimeUtils
.
getDatePart
(
v0
,
info
.
type
));
break
;
case
MONTH_NAME:
{
SimpleDateFormat
monthName
=
new
SimpleDateFormat
(
"MMMM"
,
...
...
@@ -861,27 +863,6 @@ public class Function extends Expression implements FunctionCall {
database
.
getMode
().
treatEmptyStringsAsNull
);
break
;
}
case
QUARTER:
result
=
ValueInt
.
get
((
DateTimeUtils
.
getDatePart
(
v0
,
Calendar
.
MONTH
)
-
1
)
/
3
+
1
);
break
;
case
SECOND:
result
=
ValueInt
.
get
(
DateTimeUtils
.
getDatePart
(
v0
,
Calendar
.
SECOND
));
break
;
case
WEEK:
result
=
ValueInt
.
get
(
DateTimeUtils
.
getDatePart
(
v0
,
Calendar
.
WEEK_OF_YEAR
));
break
;
case
YEAR:
result
=
ValueInt
.
get
(
DateTimeUtils
.
getDatePart
(
v0
,
Calendar
.
YEAR
));
break
;
case
ISO_YEAR:
result
=
ValueInt
.
get
(
DateTimeUtils
.
getIsoYear
(
v0
));
break
;
case
ISO_WEEK:
result
=
ValueInt
.
get
(
DateTimeUtils
.
getIsoWeek
(
v0
));
break
;
case
ISO_DAY_OF_WEEK:
result
=
ValueInt
.
get
(
DateTimeUtils
.
getIsoDayOfWeek
(
v0
));
break
;
case
CURDATE:
case
CURRENT_DATE:
{
long
now
=
session
.
getTransactionStart
();
...
...
@@ -1827,11 +1808,39 @@ public class Function extends Expression implements FunctionCall {
private
static
Timestamp
dateadd
(
String
part
,
long
count
,
Timestamp
d
)
{
int
field
=
getDatePart
(
part
);
if
(
field
==
Calendar
.
MILLISECOND
)
{
switch
(
field
)
{
case
YEAR:
field
=
Calendar
.
YEAR
;
break
;
case
MONTH:
field
=
Calendar
.
MONTH
;
break
;
case
DAY_OF_MONTH:
field
=
Calendar
.
DAY_OF_MONTH
;
break
;
case
DAY_OF_YEAR:
field
=
Calendar
.
DAY_OF_YEAR
;
break
;
case
WEEK:
field
=
Calendar
.
WEEK_OF_YEAR
;
break
;
case
HOUR:
field
=
Calendar
.
HOUR_OF_DAY
;
break
;
case
MINUTE:
field
=
Calendar
.
MINUTE
;
break
;
case
SECOND:
field
=
Calendar
.
SECOND
;
break
;
case
MILLISECOND:
{
Timestamp
ts
=
new
Timestamp
(
d
.
getTime
()
+
count
);
ts
.
setNanos
(
ts
.
getNanos
()
+
(
d
.
getNanos
()
%
1000000
));
return
ts
;
}
default
:
throw
DbException
.
getUnsupportedException
(
"DATEADD "
+
part
);
}
// We allow long for manipulating the millisecond component,
// for the rest we only allow int.
if
(
count
>
Integer
.
MAX_VALUE
)
{
...
...
@@ -1878,34 +1887,34 @@ public class Function extends Expression implements FunctionCall {
calendar
.
get
(
Calendar
.
DAY_OF_WEEK
),
calendar
.
get
(
Calendar
.
MILLISECOND
));
switch
(
field
)
{
case
Calendar
.
MILLISECOND
:
case
MILLISECOND:
return
t2
-
t1
;
case
Calendar
.
SECOND
:
case
Calendar
.
MINUTE
:
case
Calendar
.
HOUR_OF_DAY
:
case
Calendar
.
DAY_OF_YEAR
:
case
Calendar
.
WEEK_OF_YEAR
:
{
case
SECOND:
case
MINUTE:
case
HOUR
:
case
DAY_OF_YEAR:
case
WEEK
:
{
// first 'normalize' the numbers so both are not negative
long
hour
=
60
*
60
*
1000
;
long
add
=
Math
.
min
(
t1
/
hour
*
hour
,
t2
/
hour
*
hour
);
t1
-=
add
;
t2
-=
add
;
switch
(
field
)
{
case
Calendar
.
SECOND
:
case
SECOND:
return
t2
/
1000
-
t1
/
1000
;
case
Calendar
.
MINUTE
:
case
MINUTE:
return
t2
/
(
60
*
1000
)
-
t1
/
(
60
*
1000
);
case
Calendar
.
HOUR_OF_DAY
:
case
HOUR
:
return
t2
/
hour
-
t1
/
hour
;
case
Calendar
.
DAY_OF_YEAR
:
case
DAY_OF_YEAR:
return
t2
/
(
hour
*
24
)
-
t1
/
(
hour
*
24
);
case
Calendar
.
WEEK_OF_YEAR
:
case
WEEK
:
return
t2
/
(
hour
*
24
*
7
)
-
t1
/
(
hour
*
24
*
7
);
default
:
throw
DbException
.
throwInternalError
(
"field:"
+
field
);
}
}
case
Calendar
.
DATE
:
case
DAY_OF_MONTH
:
return
t2
/
(
24
*
60
*
60
*
1000
)
-
t1
/
(
24
*
60
*
60
*
1000
);
default
:
break
;
...
...
@@ -1918,9 +1927,9 @@ public class Function extends Expression implements FunctionCall {
int
year2
=
calendar
.
get
(
Calendar
.
YEAR
);
int
month2
=
calendar
.
get
(
Calendar
.
MONTH
);
int
result
=
year2
-
year1
;
if
(
field
==
Calendar
.
MONTH
)
{
if
(
field
==
MONTH
)
{
return
12
*
result
+
(
month2
-
month1
);
}
else
if
(
field
==
Calendar
.
YEAR
)
{
}
else
if
(
field
==
YEAR
)
{
return
result
;
}
else
{
throw
DbException
.
getUnsupportedException
(
"DATEDIFF "
+
part
);
...
...
h2/src/main/org/h2/util/DateTimeUtils.java
浏览文件 @
9098cb27
差异被折叠。
点击展开。
h2/src/test/org/h2/test/unit/TestDateIso8601.java
浏览文件 @
9098cb27
差异被折叠。
点击展开。
h2/src/test/org/h2/test/unit/TestDateTimeUtils.java
浏览文件 @
9098cb27
...
...
@@ -5,6 +5,9 @@
*/
package
org
.
h2
.
test
.
unit
;
import
java.util.Calendar
;
import
java.util.GregorianCalendar
;
import
org.h2.test.TestBase
;
import
org.h2.util.DateTimeUtils
;
...
...
@@ -25,6 +28,8 @@ public class TestDateTimeUtils extends TestBase {
@Override
public
void
test
()
throws
Exception
{
testParseTimeNanosDB2Format
();
testDayOfWeek
();
testWeekOfYear
();
}
private
void
testParseTimeNanosDB2Format
()
{
...
...
@@ -34,4 +39,55 @@ public class TestDateTimeUtils extends TestBase {
assertEquals
(
3723000000000L
,
DateTimeUtils
.
parseTimeNanos
(
"01:02:03"
,
0
,
8
,
true
));
assertEquals
(
3723000000000L
,
DateTimeUtils
.
parseTimeNanos
(
"01.02.03"
,
0
,
8
,
true
));
}
/**
* Test for {@link DateTimeUtils#getSundayDayOfWeek()} and
* {@link DateTimeUtils#getIsoDayOfWeek(long)}.
*/
private
void
testDayOfWeek
()
{
GregorianCalendar
gc
=
DateTimeUtils
.
createGregorianCalendar
();
for
(
int
i
=
-
1_000_000
;
i
<=
1_000_000
;
i
++)
{
gc
.
clear
();
gc
.
setTimeInMillis
(
i
*
86400000L
);
int
year
=
gc
.
get
(
Calendar
.
YEAR
);
if
(
gc
.
get
(
Calendar
.
ERA
)
==
GregorianCalendar
.
BC
)
{
year
=
1
-
year
;
}
long
expectedDateValue
=
DateTimeUtils
.
dateValue
(
year
,
gc
.
get
(
Calendar
.
MONTH
)
+
1
,
gc
.
get
(
Calendar
.
DAY_OF_MONTH
));
long
dateValue
=
DateTimeUtils
.
dateValueFromAbsoluteDay
(
i
);
assertEquals
(
expectedDateValue
,
dateValue
);
assertEquals
(
i
,
DateTimeUtils
.
absoluteDayFromDateValue
(
dateValue
));
int
dow
=
gc
.
get
(
Calendar
.
DAY_OF_WEEK
);
assertEquals
(
dow
,
DateTimeUtils
.
getSundayDayOfWeek
(
dateValue
));
int
isoDow
=
(
dow
+
5
)
%
7
+
1
;
assertEquals
(
isoDow
,
DateTimeUtils
.
getIsoDayOfWeek
(
dateValue
));
assertEquals
(
gc
.
get
(
Calendar
.
WEEK_OF_YEAR
),
DateTimeUtils
.
getWeekOfYear
(
dateValue
,
gc
.
getFirstDayOfWeek
()
-
1
,
gc
.
getMinimalDaysInFirstWeek
()));
}
}
/**
* Test for {@link DateTimeUtils#getDayOfYear(long)},
* {@link DateTimeUtils#getWeekOfYear(long, int, int)} and
* {@link DateTimeUtils#getWeekYear(long, int, int)}.
*/
private
void
testWeekOfYear
()
{
GregorianCalendar
gc
=
new
GregorianCalendar
(
DateTimeUtils
.
UTC
);
for
(
int
firstDay
=
1
;
firstDay
<=
7
;
firstDay
++)
{
gc
.
setFirstDayOfWeek
(
firstDay
);
for
(
int
minimalDays
=
1
;
minimalDays
<=
7
;
minimalDays
++)
{
gc
.
setMinimalDaysInFirstWeek
(
minimalDays
);
for
(
int
i
=
0
;
i
<
150_000
;
i
++)
{
long
dateValue
=
DateTimeUtils
.
dateValueFromAbsoluteDay
(
i
);
gc
.
clear
();
gc
.
setTimeInMillis
(
i
*
86400000L
);
assertEquals
(
gc
.
get
(
Calendar
.
DAY_OF_YEAR
),
DateTimeUtils
.
getDayOfYear
(
dateValue
));
assertEquals
(
gc
.
get
(
Calendar
.
WEEK_OF_YEAR
),
DateTimeUtils
.
getWeekOfYear
(
dateValue
,
firstDay
-
1
,
minimalDays
));
assertEquals
(
gc
.
getWeekYear
(),
DateTimeUtils
.
getWeekYear
(
dateValue
,
firstDay
-
1
,
minimalDays
));
}
}
}
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论