Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
5e308433
提交
5e308433
authored
8月 17, 2018
作者:
Evgenij Ryazanov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Parse more date-time literals for compatibility with other databases
上级
c6f12e1c
显示空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
145 行增加
和
28 行删除
+145
-28
DateTimeUtils.java
h2/src/main/org/h2/util/DateTimeUtils.java
+82
-28
date.sql
h2/src/test/org/h2/test/scripts/datatypes/date.sql
+9
-0
time.sql
h2/src/test/org/h2/test/scripts/datatypes/time.sql
+30
-0
timestamp.sql
h2/src/test/org/h2/test/scripts/datatypes/timestamp.sql
+24
-0
没有找到文件。
h2/src/main/org/h2/util/DateTimeUtils.java
浏览文件 @
5e308433
...
...
@@ -336,6 +336,7 @@ public class DateTimeUtils {
/**
* Parse a date string. The format is: [+|-]year-month-day
* or [+|-]yyyyMMdd.
*
* @param s the string to parse
* @param start the parse index start
...
...
@@ -349,14 +350,27 @@ public class DateTimeUtils {
start
++;
}
// start at position 1 to support "-year"
int
s1
=
s
.
indexOf
(
'-'
,
start
+
1
);
int
s2
=
s
.
indexOf
(
'-'
,
s1
+
1
);
if
(
s1
<=
0
||
s2
<=
s1
)
{
int
yEnd
=
s
.
indexOf
(
'-'
,
start
+
1
);
int
mStart
,
mEnd
,
dStart
;
if
(
yEnd
>
0
)
{
// Standard [+|-]year-month-day format
mStart
=
yEnd
+
1
;
mEnd
=
s
.
indexOf
(
'-'
,
mStart
);
if
(
mEnd
<=
mStart
)
{
throw
new
IllegalArgumentException
(
s
);
}
int
year
=
Integer
.
parseInt
(
s
.
substring
(
start
,
s1
));
int
month
=
Integer
.
parseInt
(
s
.
substring
(
s1
+
1
,
s2
));
int
day
=
Integer
.
parseInt
(
s
.
substring
(
s2
+
1
,
end
));
dStart
=
mEnd
+
1
;
}
else
{
// Additional [+|-]yyyyMMdd format for compatibility
mEnd
=
dStart
=
end
-
2
;
yEnd
=
mStart
=
mEnd
-
2
;
if
(
yEnd
<=
start
)
{
throw
new
IllegalArgumentException
(
s
);
}
}
int
year
=
Integer
.
parseInt
(
s
.
substring
(
start
,
yEnd
));
int
month
=
Integer
.
parseInt
(
s
.
substring
(
mStart
,
mEnd
));
int
day
=
Integer
.
parseInt
(
s
.
substring
(
dStart
,
end
));
if
(!
isValidDate
(
year
,
month
,
day
))
{
throw
new
IllegalArgumentException
(
year
+
"-"
+
month
+
"-"
+
day
);
}
...
...
@@ -364,8 +378,8 @@ public class DateTimeUtils {
}
/**
* Parse a time string. The format is: hour:minute
:second[.nanos] or
*
alternatively
hour.minute.second[.nanos].
* Parse a time string. The format is: hour:minute
[:second[.nanos]],
*
hhmm[ss[.nanos]], or
hour.minute.second[.nanos].
*
* @param s the string to parse
* @param start the parse index start
...
...
@@ -375,30 +389,70 @@ public class DateTimeUtils {
*/
public
static
long
parseTimeNanos
(
String
s
,
int
start
,
int
end
)
{
int
hour
,
minute
,
second
,
nanos
;
int
s1
=
s
.
indexOf
(
':'
,
start
);
int
s2
=
s
.
indexOf
(
':'
,
s1
+
1
);
int
s3
=
s
.
indexOf
(
'.'
,
s2
+
1
);
if
(
s1
<=
0
||
s2
<=
s1
)
{
// if first try fails try to use IBM DB2 time format
// [-]hour.minute.second[.nanos]
s1
=
s
.
indexOf
(
'.'
,
start
);
s2
=
s
.
indexOf
(
'.'
,
s1
+
1
);
s3
=
s
.
indexOf
(
'.'
,
s2
+
1
);
if
(
s1
<=
0
||
s2
<=
s1
)
{
int
hEnd
=
s
.
indexOf
(
':'
,
start
);
int
mStart
,
mEnd
,
sStart
,
sEnd
;
if
(
hEnd
>
0
)
{
mStart
=
hEnd
+
1
;
mEnd
=
s
.
indexOf
(
':'
,
mStart
);
if
(
mEnd
>=
mStart
)
{
// Standard hour:minute:second[.nanos] format
sStart
=
mEnd
+
1
;
sEnd
=
s
.
indexOf
(
'.'
,
sStart
);
}
else
{
// Additional hour:minute format for compatibility
mEnd
=
end
;
sStart
=
sEnd
=
-
1
;
}
}
else
{
int
t
=
s
.
indexOf
(
'.'
,
start
);
if
(
t
<
0
)
{
// Additional hhmm[ss] format for compatibility
hEnd
=
mStart
=
start
+
2
;
mEnd
=
mStart
+
2
;
int
len
=
end
-
start
;
if
(
len
==
6
)
{
sStart
=
mEnd
;
sEnd
=
-
1
;
}
else
if
(
len
==
4
)
{
sStart
=
sEnd
=
-
1
;
}
else
{
throw
new
IllegalArgumentException
(
s
);
}
}
else
if
(
t
>=
start
+
6
)
{
// Additional hhmmss.nanos format for compatibility
if
(
t
-
start
!=
6
)
{
throw
new
IllegalArgumentException
(
s
);
}
hEnd
=
mStart
=
start
+
2
;
mEnd
=
sStart
=
mStart
+
2
;
sEnd
=
t
;
}
else
{
// Additional hour.minute.second[.nanos] IBM DB2 time format
hEnd
=
t
;
mStart
=
hEnd
+
1
;
mEnd
=
s
.
indexOf
(
'.'
,
mStart
);
if
(
mEnd
<=
mStart
)
{
throw
new
IllegalArgumentException
(
s
);
}
hour
=
Integer
.
parseInt
(
s
.
substring
(
start
,
s1
));
if
(
hour
<
0
||
hour
==
0
&&
s
.
charAt
(
0
)
==
'-'
||
hour
>=
24
)
{
sStart
=
mEnd
+
1
;
sEnd
=
s
.
indexOf
(
'.'
,
sStart
);
}
}
hour
=
Integer
.
parseInt
(
s
.
substring
(
start
,
hEnd
));
if
(
hour
<
0
||
hour
==
0
&&
s
.
charAt
(
start
)
==
'-'
||
hour
>=
24
)
{
throw
new
IllegalArgumentException
(
s
);
}
minute
=
Integer
.
parseInt
(
s
.
substring
(
s1
+
1
,
s2
));
if
(
s3
<
0
)
{
second
=
Integer
.
parseInt
(
s
.
substring
(
s2
+
1
,
end
));
minute
=
Integer
.
parseInt
(
s
.
substring
(
mStart
,
mEnd
));
if
(
sStart
>
0
)
{
if
(
sEnd
<
0
)
{
second
=
Integer
.
parseInt
(
s
.
substring
(
sStart
,
end
));
nanos
=
0
;
}
else
{
second
=
Integer
.
parseInt
(
s
.
substring
(
s2
+
1
,
s3
));
nanos
=
parseNanos
(
s
,
s3
+
1
,
end
);
second
=
Integer
.
parseInt
(
s
.
substring
(
sStart
,
sEnd
));
nanos
=
parseNanos
(
s
,
sEnd
+
1
,
end
);
}
}
else
{
second
=
nanos
=
0
;
}
if
(
minute
<
0
||
minute
>=
60
||
second
<
0
||
second
>=
60
)
{
throw
new
IllegalArgumentException
(
s
);
...
...
h2/src/test/org/h2/test/scripts/datatypes/date.sql
浏览文件 @
5e308433
...
...
@@ -15,3 +15,12 @@ SELECT COLUMN_NAME, DATA_TYPE, TYPE_NAME, COLUMN_TYPE, NUMERIC_SCALE, DATETIME_P
DROP
TABLE
TEST
;
>
ok
SELECT
DATE
'2000-01-02'
;
>>
2000
-
01
-
02
SELECT
DATE
'20000102'
;
>>
2000
-
01
-
02
SELECT
DATE
'-1000102'
;
>>
-
100
-
01
-
02
h2/src/test/org/h2/test/scripts/datatypes/time.sql
浏览文件 @
5e308433
...
...
@@ -81,3 +81,33 @@ SELECT T0 FROM TEST;
DROP
TABLE
TEST
;
>
ok
SELECT
TIME
'11:22:33'
;
>>
11
:
22
:
33
SELECT
TIME
'11:22'
;
>>
11
:
22
:
00
SELECT
TIME
'112233'
;
>>
11
:
22
:
33
SELECT
TIME
'1122'
;
>>
11
:
22
:
00
SELECT
TIME
'12233'
;
>
exception
INVALID_DATETIME_CONSTANT_2
SELECT
TIME
'122'
;
>
exception
INVALID_DATETIME_CONSTANT_2
SELECT
TIME
'11:22:33.1'
;
>>
11
:
22
:
33
.
1
SELECT
TIME
'112233.1'
;
>>
11
:
22
:
33
.
1
SELECT
TIME
'12233.1'
;
>
exception
INVALID_DATETIME_CONSTANT_2
SELECT
TIME
'1122.1'
;
>
exception
INVALID_DATETIME_CONSTANT_2
h2/src/test/org/h2/test/scripts/datatypes/timestamp.sql
浏览文件 @
5e308433
...
...
@@ -129,3 +129,27 @@ select * from test where d= timestamp '2006-01-01 12:00:00.000';
drop
table
test
;
>
ok
SELECT
TIMESTAMP
'2000-01-02 11:22:33'
;
>>
2000
-
01
-
02
11
:
22
:
33
SELECT
TIMESTAMP
'2000-01-02T11:22:33'
;
>>
2000
-
01
-
02
11
:
22
:
33
SELECT
TIMESTAMP
'20000102 11:22:33'
;
>>
2000
-
01
-
02
11
:
22
:
33
SELECT
TIMESTAMP
'20000102T11:22:33'
;
>>
2000
-
01
-
02
11
:
22
:
33
SELECT
TIMESTAMP
'2000-01-02 112233'
;
>>
2000
-
01
-
02
11
:
22
:
33
SELECT
TIMESTAMP
'2000-01-02T112233'
;
>>
2000
-
01
-
02
11
:
22
:
33
SELECT
TIMESTAMP
'20000102 112233'
;
>>
2000
-
01
-
02
11
:
22
:
33
SELECT
TIMESTAMP
'20000102T112233'
;
>>
2000
-
01
-
02
11
:
22
:
33
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论