Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
1105b977
提交
1105b977
authored
2月 19, 2018
作者:
Evgenij Ryazanov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add support for TIMESTAMP WITH TIME ZONE to FORMATDATETIME
上级
47ed036f
显示空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
68 行增加
和
19 行删除
+68
-19
help.csv
h2/src/docsrc/help/help.csv
+2
-0
Function.java
h2/src/main/org/h2/expression/Function.java
+10
-1
DateTimeUtils.java
h2/src/main/org/h2/util/DateTimeUtils.java
+43
-2
ToChar.java
h2/src/main/org/h2/util/ToChar.java
+1
-16
formatdatetime.sql
.../h2/test/scripts/functions/timeanddate/formatdatetime.sql
+12
-0
没有找到文件。
h2/src/docsrc/help/help.csv
浏览文件 @
1105b977
...
...
@@ -3759,6 +3759,8 @@ Formats a date, time or timestamp as a string.
The most important format characters are:
y year, M month, d day, H hour, m minute, s second.
For details of the format, see ""java.text.SimpleDateFormat"".
timeZoneString may be specified if timestamp is a DATE, TIME or TIMESTAMP.
timeZoneString is ignored if timestamp is TIMESTAMP WITH TIME ZONE.
This method returns a string.
","
CALL FORMATDATETIME(TIMESTAMP '2001-02-03 04:05:06',
...
...
h2/src/main/org/h2/expression/Function.java
浏览文件 @
1105b977
...
...
@@ -1553,8 +1553,17 @@ public class Function extends Expression implements FunctionCall {
null
:
v2
==
ValueNull
.
INSTANCE
?
null
:
v2
.
getString
();
String
tz
=
v3
==
null
?
null
:
v3
==
ValueNull
.
INSTANCE
?
null
:
v3
.
getString
();
java
.
util
.
Date
date
;
if
(
v0
instanceof
ValueTimestampTimeZone
)
{
ValueTimestampTimeZone
ts
=
(
ValueTimestampTimeZone
)
v0
;
tz
=
DateTimeUtils
.
timeZoneNameFromOffsetMins
(
ts
.
getTimeZoneOffsetMins
());
date
=
DateTimeUtils
.
convertTimestampTimeZoneToTimestamp
(
ts
.
getDateValue
(),
ts
.
getTimeNanos
(),
ts
.
getTimeZoneOffsetMins
());
}
else
{
date
=
v0
.
getTimestamp
();
}
result
=
ValueString
.
get
(
DateTimeUtils
.
formatDateTime
(
v0
.
getTimestamp
()
,
v1
.
getString
(),
locale
,
tz
),
date
,
v1
.
getString
(),
locale
,
tz
),
database
.
getMode
().
treatEmptyStringsAsNull
);
}
break
;
...
...
h2/src/main/org/h2/util/DateTimeUtils.java
浏览文件 @
1105b977
...
...
@@ -955,9 +955,25 @@ public class DateTimeUtils {
*/
public
static
Timestamp
convertDateValueToTimestamp
(
long
dateValue
,
long
timeNanos
)
{
Timestamp
ts
=
new
Timestamp
(
convertDateTimeValueToMillis
(
null
,
dateValue
,
timeNanos
/
1
000
000
));
Timestamp
ts
=
new
Timestamp
(
convertDateTimeValueToMillis
(
null
,
dateValue
,
timeNanos
/
1
_000_
000
));
// This method expects the complete nanoseconds value including milliseconds
ts
.
setNanos
((
int
)
(
timeNanos
%
1000000000
));
ts
.
setNanos
((
int
)
(
timeNanos
%
1_000_000_000
));
return
ts
;
}
/**
* Convert an encoded date value / time value to a timestamp using the specified
* time zone offset.
*
* @param dateValue the date value
* @param timeNanos the nanoseconds since midnight
* @param offsetMins time zone offset in minutes
* @return the timestamp
*/
public
static
Timestamp
convertTimestampTimeZoneToTimestamp
(
long
dateValue
,
long
timeNanos
,
short
offsetMins
)
{
Timestamp
ts
=
new
Timestamp
(
absoluteDayFromDateValue
(
dateValue
)
*
MILLIS_PER_DAY
+
timeNanos
/
1_000_000
-
offsetMins
*
60_000
);
ts
.
setNanos
((
int
)
(
timeNanos
%
1_000_000_000
));
return
ts
;
}
...
...
@@ -1338,4 +1354,29 @@ public class DateTimeUtils {
return
buff
.
toString
();
}
/**
* Generates time zone name for the specified offset in minutes.
*
* @param offsetMins
* offset in minutes
* @return time zone name
*/
public
static
String
timeZoneNameFromOffsetMins
(
int
offsetMins
)
{
if
(
offsetMins
==
0
)
{
return
"UTC"
;
}
StringBuilder
b
=
new
StringBuilder
(
9
);
b
.
append
(
"GMT"
);
if
(
offsetMins
<
0
)
{
b
.
append
(
'-'
);
offsetMins
=
-
offsetMins
;
}
else
{
b
.
append
(
'+'
);
}
StringUtils
.
appendZeroPadded
(
b
,
2
,
offsetMins
/
60
);
b
.
append
(
':'
);
StringUtils
.
appendZeroPadded
(
b
,
2
,
offsetMins
%
60
);
return
b
.
toString
();
}
}
h2/src/main/org/h2/util/ToChar.java
浏览文件 @
1105b977
...
...
@@ -496,22 +496,7 @@ public class ToChar {
}
return
tz
.
getID
();
}
int
offset
=
((
ValueTimestampTimeZone
)
value
).
getTimeZoneOffsetMins
();
if
(
offset
==
0
)
{
return
"UTC"
;
}
StringBuilder
b
=
new
StringBuilder
(
9
);
b
.
append
(
"GMT"
);
if
(
offset
<
0
)
{
b
.
append
(
'-'
);
offset
=
-
offset
;
}
else
{
b
.
append
(
'+'
);
}
StringUtils
.
appendZeroPadded
(
b
,
2
,
offset
/
60
);
b
.
append
(
':'
);
StringUtils
.
appendZeroPadded
(
b
,
2
,
offset
%
60
);
return
b
.
toString
();
return
DateTimeUtils
.
timeZoneNameFromOffsetMins
(((
ValueTimestampTimeZone
)
value
).
getTimeZoneOffsetMins
());
}
/**
...
...
h2/src/test/org/h2/test/scripts/functions/timeanddate/formatdatetime.sql
浏览文件 @
1105b977
...
...
@@ -32,3 +32,15 @@ CALL FORMATDATETIME(PARSEDATETIME('Sat, 3 Feb 2001 04:05:06 GMT', 'EEE, d MMM yy
>
---------------------
>
2001
-
02
-
03
04
:
05
:
06
>
rows
:
1
SELECT
FORMATDATETIME
(
TIMESTAMP
WITH
TIME
ZONE
'2010-05-06 07:08:09.123Z'
,
'yyyy-MM-dd HH:mm:ss.SSS z'
)
AS
S
;
>
S
>
---------------------------
>
2010
-
05
-
06
07
:
08
:
09
.
123
UTC
>
rows
:
1
SELECT
FORMATDATETIME
(
TIMESTAMP
WITH
TIME
ZONE
'2010-05-06 07:08:09.123+13:30'
,
'yyyy-MM-dd HH:mm:ss.SSS z'
)
AS
S
;
>
S
>
---------------------------------
>
2010
-
05
-
06
07
:
08
:
09
.
123
GMT
+
13
:
30
>
rows
:
1
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论