Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
134712c8
提交
134712c8
authored
1月 31, 2018
作者:
Evgenij Ryazanov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Move date-time formatting code to DateTimeUtils and reuse them in TimestampWithTimeZone
上级
cad0c35a
隐藏空白字符变更
内嵌
并排
正在显示
6 个修改的文件
包含
117 行增加
和
164 行删除
+117
-164
TimestampWithTimeZone.java
h2/src/main/org/h2/api/TimestampWithTimeZone.java
+1
-57
DateTimeUtils.java
h2/src/main/org/h2/util/DateTimeUtils.java
+109
-0
ValueDate.java
h2/src/main/org/h2/value/ValueDate.java
+1
-23
ValueTime.java
h2/src/main/org/h2/value/ValueTime.java
+2
-51
ValueTimestamp.java
h2/src/main/org/h2/value/ValueTimestamp.java
+2
-2
ValueTimestampTimeZone.java
h2/src/main/org/h2/value/ValueTimestampTimeZone.java
+2
-31
没有找到文件。
h2/src/main/org/h2/api/TimestampWithTimeZone.java
浏览文件 @
134712c8
...
@@ -7,7 +7,6 @@ package org.h2.api;
...
@@ -7,7 +7,6 @@ package org.h2.api;
import
java.io.Serializable
;
import
java.io.Serializable
;
import
org.h2.util.DateTimeUtils
;
import
org.h2.util.DateTimeUtils
;
import
org.h2.util.StringUtils
;
/**
/**
* How we expose "TIMESTAMP WITH TIMEZONE" in our ResultSets.
* How we expose "TIMESTAMP WITH TIMEZONE" in our ResultSets.
...
@@ -110,62 +109,7 @@ public class TimestampWithTimeZone implements Serializable, Cloneable {
...
@@ -110,62 +109,7 @@ public class TimestampWithTimeZone implements Serializable, Cloneable {
@Override
@Override
public
String
toString
()
{
public
String
toString
()
{
StringBuilder
buff
=
new
StringBuilder
();
return
DateTimeUtils
.
timestampTimeZoneToString
(
dateValue
,
timeNanos
,
timeZoneOffsetMins
);
int
y
=
DateTimeUtils
.
yearFromDateValue
(
dateValue
);
int
month
=
DateTimeUtils
.
monthFromDateValue
(
dateValue
);
int
d
=
DateTimeUtils
.
dayFromDateValue
(
dateValue
);
if
(
y
>
0
&&
y
<
10000
)
{
StringUtils
.
appendZeroPadded
(
buff
,
4
,
y
);
}
else
{
buff
.
append
(
y
);
}
buff
.
append
(
'-'
);
StringUtils
.
appendZeroPadded
(
buff
,
2
,
month
);
buff
.
append
(
'-'
);
StringUtils
.
appendZeroPadded
(
buff
,
2
,
d
);
buff
.
append
(
' '
);
long
nanos
=
timeNanos
;
long
ms
=
nanos
/
1000000
;
nanos
-=
ms
*
1000000
;
long
s
=
ms
/
1000
;
ms
-=
s
*
1000
;
long
min
=
s
/
60
;
s
-=
min
*
60
;
long
h
=
min
/
60
;
min
-=
h
*
60
;
StringUtils
.
appendZeroPadded
(
buff
,
2
,
h
);
buff
.
append
(
':'
);
StringUtils
.
appendZeroPadded
(
buff
,
2
,
min
);
buff
.
append
(
':'
);
StringUtils
.
appendZeroPadded
(
buff
,
2
,
s
);
buff
.
append
(
'.'
);
int
start
=
buff
.
length
();
StringUtils
.
appendZeroPadded
(
buff
,
3
,
ms
);
if
(
nanos
>
0
)
{
StringUtils
.
appendZeroPadded
(
buff
,
6
,
nanos
);
}
for
(
int
i
=
buff
.
length
()
-
1
;
i
>
start
;
i
--)
{
if
(
buff
.
charAt
(
i
)
!=
'0'
)
{
break
;
}
buff
.
deleteCharAt
(
i
);
}
short
tz
=
timeZoneOffsetMins
;
if
(
tz
<
0
)
{
buff
.
append
(
'-'
);
tz
=
(
short
)
-
tz
;
}
else
{
buff
.
append
(
'+'
);
}
int
hours
=
tz
/
60
;
tz
-=
hours
*
60
;
int
mins
=
tz
;
StringUtils
.
appendZeroPadded
(
buff
,
2
,
hours
);
if
(
mins
!=
0
)
{
buff
.
append
(
':'
);
StringUtils
.
appendZeroPadded
(
buff
,
2
,
mins
);
}
return
buff
.
toString
();
}
}
@Override
@Override
...
...
h2/src/main/org/h2/util/DateTimeUtils.java
浏览文件 @
134712c8
...
@@ -999,4 +999,113 @@ public class DateTimeUtils {
...
@@ -999,4 +999,113 @@ public class DateTimeUtils {
return
resultDate
;
return
resultDate
;
}
}
/**
* Append a date to the string builder.
*
* @param buff the target string builder
* @param dateValue the date value
*/
public
static
void
appendDate
(
StringBuilder
buff
,
long
dateValue
)
{
int
y
=
yearFromDateValue
(
dateValue
);
int
m
=
monthFromDateValue
(
dateValue
);
int
d
=
dayFromDateValue
(
dateValue
);
if
(
y
>
0
&&
y
<
10000
)
{
StringUtils
.
appendZeroPadded
(
buff
,
4
,
y
);
}
else
{
buff
.
append
(
y
);
}
buff
.
append
(
'-'
);
StringUtils
.
appendZeroPadded
(
buff
,
2
,
m
);
buff
.
append
(
'-'
);
StringUtils
.
appendZeroPadded
(
buff
,
2
,
d
);
}
/**
* Append a time to the string builder.
*
* @param buff the target string builder
* @param nanos the time in nanoseconds
* @param alwaysAddMillis whether to always add at least ".0"
*/
public
static
void
appendTime
(
StringBuilder
buff
,
long
nanos
,
boolean
alwaysAddMillis
)
{
if
(
nanos
<
0
)
{
buff
.
append
(
'-'
);
nanos
=
-
nanos
;
}
/*
* nanos now either in range from 0 to Long.MAX_VALUE or equals to
* Long.MIN_VALUE. We need to divide nanos by 1000000 with unsigned division to
* get correct result. The simplest way to do this with such constraints is to
* divide -nanos by -1000000.
*/
long
ms
=
-
nanos
/
-
1000000
;
nanos
-=
ms
*
1000000
;
long
s
=
ms
/
1000
;
ms
-=
s
*
1000
;
long
m
=
s
/
60
;
s
-=
m
*
60
;
long
h
=
m
/
60
;
m
-=
h
*
60
;
StringUtils
.
appendZeroPadded
(
buff
,
2
,
h
);
buff
.
append
(
':'
);
StringUtils
.
appendZeroPadded
(
buff
,
2
,
m
);
buff
.
append
(
':'
);
StringUtils
.
appendZeroPadded
(
buff
,
2
,
s
);
if
(
alwaysAddMillis
||
ms
>
0
||
nanos
>
0
)
{
buff
.
append
(
'.'
);
int
start
=
buff
.
length
();
StringUtils
.
appendZeroPadded
(
buff
,
3
,
ms
);
if
(
nanos
>
0
)
{
StringUtils
.
appendZeroPadded
(
buff
,
6
,
nanos
);
}
for
(
int
i
=
buff
.
length
()
-
1
;
i
>
start
;
i
--)
{
if
(
buff
.
charAt
(
i
)
!=
'0'
)
{
break
;
}
buff
.
deleteCharAt
(
i
);
}
}
}
/**
* Append a time zone to the string builder.
*
* @param buff the target string builder
* @param tz the time zone in minutes
*/
public
static
void
appendTimeZone
(
StringBuilder
buff
,
short
tz
)
{
if
(
tz
<
0
)
{
buff
.
append
(
'-'
);
tz
=
(
short
)
-
tz
;
}
else
{
buff
.
append
(
'+'
);
}
int
hours
=
tz
/
60
;
tz
-=
hours
*
60
;
int
mins
=
tz
;
StringUtils
.
appendZeroPadded
(
buff
,
2
,
hours
);
if
(
mins
!=
0
)
{
buff
.
append
(
':'
);
StringUtils
.
appendZeroPadded
(
buff
,
2
,
mins
);
}
}
/**
* Formats timestamp with time zone as string.
*
* @param dateValue the year-month-day bit field
* @param timeNanos nanoseconds since midnight
* @param timeZoneOffsetMins the time zone offset in minutes
* @return formatted string
*/
public
static
String
timestampTimeZoneToString
(
long
dateValue
,
long
timeNanos
,
short
timeZoneOffsetMins
)
{
StringBuilder
buff
=
new
StringBuilder
(
ValueTimestampTimeZone
.
DISPLAY_SIZE
);
appendDate
(
buff
,
dateValue
);
buff
.
append
(
' '
);
appendTime
(
buff
,
timeNanos
,
true
);
appendTimeZone
(
buff
,
timeZoneOffsetMins
);
return
buff
.
toString
();
}
}
}
h2/src/main/org/h2/value/ValueDate.java
浏览文件 @
134712c8
...
@@ -12,7 +12,6 @@ import java.sql.SQLException;
...
@@ -12,7 +12,6 @@ import java.sql.SQLException;
import
org.h2.api.ErrorCode
;
import
org.h2.api.ErrorCode
;
import
org.h2.message.DbException
;
import
org.h2.message.DbException
;
import
org.h2.util.DateTimeUtils
;
import
org.h2.util.DateTimeUtils
;
import
org.h2.util.StringUtils
;
/**
/**
* Implementation of the DATE data type.
* Implementation of the DATE data type.
...
@@ -99,7 +98,7 @@ public class ValueDate extends Value {
...
@@ -99,7 +98,7 @@ public class ValueDate extends Value {
@Override
@Override
public
String
getString
()
{
public
String
getString
()
{
StringBuilder
buff
=
new
StringBuilder
(
DISPLAY_SIZE
);
StringBuilder
buff
=
new
StringBuilder
(
DISPLAY_SIZE
);
appendDate
(
buff
,
dateValue
);
DateTimeUtils
.
appendDate
(
buff
,
dateValue
);
return
buff
.
toString
();
return
buff
.
toString
();
}
}
...
@@ -148,25 +147,4 @@ public class ValueDate extends Value {
...
@@ -148,25 +147,4 @@ public class ValueDate extends Value {
prep
.
setDate
(
parameterIndex
,
getDate
());
prep
.
setDate
(
parameterIndex
,
getDate
());
}
}
/**
* Append a date to the string builder.
*
* @param buff the target string builder
* @param dateValue the date value
*/
static
void
appendDate
(
StringBuilder
buff
,
long
dateValue
)
{
int
y
=
DateTimeUtils
.
yearFromDateValue
(
dateValue
);
int
m
=
DateTimeUtils
.
monthFromDateValue
(
dateValue
);
int
d
=
DateTimeUtils
.
dayFromDateValue
(
dateValue
);
if
(
y
>
0
&&
y
<
10000
)
{
StringUtils
.
appendZeroPadded
(
buff
,
4
,
y
);
}
else
{
buff
.
append
(
y
);
}
buff
.
append
(
'-'
);
StringUtils
.
appendZeroPadded
(
buff
,
2
,
m
);
buff
.
append
(
'-'
);
StringUtils
.
appendZeroPadded
(
buff
,
2
,
d
);
}
}
}
h2/src/main/org/h2/value/ValueTime.java
浏览文件 @
134712c8
...
@@ -12,7 +12,6 @@ import org.h2.api.ErrorCode;
...
@@ -12,7 +12,6 @@ import org.h2.api.ErrorCode;
import
org.h2.engine.SysProperties
;
import
org.h2.engine.SysProperties
;
import
org.h2.message.DbException
;
import
org.h2.message.DbException
;
import
org.h2.util.DateTimeUtils
;
import
org.h2.util.DateTimeUtils
;
import
org.h2.util.StringUtils
;
/**
/**
* Implementation of the TIME data type.
* Implementation of the TIME data type.
...
@@ -52,7 +51,7 @@ public class ValueTime extends Value {
...
@@ -52,7 +51,7 @@ public class ValueTime extends Value {
if
(!
SysProperties
.
UNLIMITED_TIME_RANGE
)
{
if
(!
SysProperties
.
UNLIMITED_TIME_RANGE
)
{
if
(
nanos
<
0L
||
nanos
>=
86400000000000L
)
{
if
(
nanos
<
0L
||
nanos
>=
86400000000000L
)
{
StringBuilder
builder
=
new
StringBuilder
();
StringBuilder
builder
=
new
StringBuilder
();
appendTime
(
builder
,
nanos
,
false
);
DateTimeUtils
.
appendTime
(
builder
,
nanos
,
false
);
throw
DbException
.
get
(
ErrorCode
.
INVALID_DATETIME_CONSTANT_2
,
throw
DbException
.
get
(
ErrorCode
.
INVALID_DATETIME_CONSTANT_2
,
"TIME"
,
builder
.
toString
());
"TIME"
,
builder
.
toString
());
}
}
...
@@ -116,7 +115,7 @@ public class ValueTime extends Value {
...
@@ -116,7 +115,7 @@ public class ValueTime extends Value {
@Override
@Override
public
String
getString
()
{
public
String
getString
()
{
StringBuilder
buff
=
new
StringBuilder
(
DISPLAY_SIZE
);
StringBuilder
buff
=
new
StringBuilder
(
DISPLAY_SIZE
);
appendTime
(
buff
,
nanos
,
false
);
DateTimeUtils
.
appendTime
(
buff
,
nanos
,
false
);
return
buff
.
toString
();
return
buff
.
toString
();
}
}
...
@@ -196,52 +195,4 @@ public class ValueTime extends Value {
...
@@ -196,52 +195,4 @@ public class ValueTime extends Value {
return
ValueTime
.
fromNanos
(-
nanos
);
return
ValueTime
.
fromNanos
(-
nanos
);
}
}
/**
* Append a time to the string builder.
*
* @param buff the target string builder
* @param nanos the time in nanoseconds
* @param alwaysAddMillis whether to always add at least ".0"
*/
static
void
appendTime
(
StringBuilder
buff
,
long
nanos
,
boolean
alwaysAddMillis
)
{
if
(
nanos
<
0
)
{
buff
.
append
(
'-'
);
nanos
=
-
nanos
;
}
/*
* nanos now either in range from 0 to Long.MAX_VALUE or equals to
* Long.MIN_VALUE. We need to divide nanos by 1000000 with unsigned division to
* get correct result. The simplest way to do this with such constraints is to
* divide -nanos by -1000000.
*/
long
ms
=
-
nanos
/
-
1000000
;
nanos
-=
ms
*
1000000
;
long
s
=
ms
/
1000
;
ms
-=
s
*
1000
;
long
m
=
s
/
60
;
s
-=
m
*
60
;
long
h
=
m
/
60
;
m
-=
h
*
60
;
StringUtils
.
appendZeroPadded
(
buff
,
2
,
h
);
buff
.
append
(
':'
);
StringUtils
.
appendZeroPadded
(
buff
,
2
,
m
);
buff
.
append
(
':'
);
StringUtils
.
appendZeroPadded
(
buff
,
2
,
s
);
if
(
alwaysAddMillis
||
ms
>
0
||
nanos
>
0
)
{
buff
.
append
(
'.'
);
int
start
=
buff
.
length
();
StringUtils
.
appendZeroPadded
(
buff
,
3
,
ms
);
if
(
nanos
>
0
)
{
StringUtils
.
appendZeroPadded
(
buff
,
6
,
nanos
);
}
for
(
int
i
=
buff
.
length
()
-
1
;
i
>
start
;
i
--)
{
if
(
buff
.
charAt
(
i
)
!=
'0'
)
{
break
;
}
buff
.
deleteCharAt
(
i
);
}
}
}
}
}
h2/src/main/org/h2/value/ValueTimestamp.java
浏览文件 @
134712c8
...
@@ -259,9 +259,9 @@ public class ValueTimestamp extends Value {
...
@@ -259,9 +259,9 @@ public class ValueTimestamp extends Value {
@Override
@Override
public
String
getString
()
{
public
String
getString
()
{
StringBuilder
buff
=
new
StringBuilder
(
DISPLAY_SIZE
);
StringBuilder
buff
=
new
StringBuilder
(
DISPLAY_SIZE
);
ValueDate
.
appendDate
(
buff
,
dateValue
);
DateTimeUtils
.
appendDate
(
buff
,
dateValue
);
buff
.
append
(
' '
);
buff
.
append
(
' '
);
ValueTime
.
appendTime
(
buff
,
timeNanos
,
true
);
DateTimeUtils
.
appendTime
(
buff
,
timeNanos
,
true
);
return
buff
.
toString
();
return
buff
.
toString
();
}
}
...
...
h2/src/main/org/h2/value/ValueTimestampTimeZone.java
浏览文件 @
134712c8
...
@@ -15,7 +15,6 @@ import org.h2.api.ErrorCode;
...
@@ -15,7 +15,6 @@ import org.h2.api.ErrorCode;
import
org.h2.api.TimestampWithTimeZone
;
import
org.h2.api.TimestampWithTimeZone
;
import
org.h2.message.DbException
;
import
org.h2.message.DbException
;
import
org.h2.util.DateTimeUtils
;
import
org.h2.util.DateTimeUtils
;
import
org.h2.util.StringUtils
;
/**
/**
* Implementation of the TIMESTAMP WITH TIME ZONE data type.
* Implementation of the TIMESTAMP WITH TIME ZONE data type.
...
@@ -34,7 +33,7 @@ public class ValueTimestampTimeZone extends Value {
...
@@ -34,7 +33,7 @@ public class ValueTimestampTimeZone extends Value {
* The display size of the textual representation of a timestamp. Example:
* The display size of the textual representation of a timestamp. Example:
* 2001-01-01 23:59:59.000 +10:00
* 2001-01-01 23:59:59.000 +10:00
*/
*/
static
final
int
DISPLAY_SIZE
=
30
;
public
static
final
int
DISPLAY_SIZE
=
30
;
/**
/**
* The default scale for timestamps.
* The default scale for timestamps.
...
@@ -233,35 +232,7 @@ public class ValueTimestampTimeZone extends Value {
...
@@ -233,35 +232,7 @@ public class ValueTimestampTimeZone extends Value {
@Override
@Override
public
String
getString
()
{
public
String
getString
()
{
StringBuilder
buff
=
new
StringBuilder
(
DISPLAY_SIZE
);
return
DateTimeUtils
.
timestampTimeZoneToString
(
dateValue
,
timeNanos
,
timeZoneOffsetMins
);
ValueDate
.
appendDate
(
buff
,
dateValue
);
buff
.
append
(
' '
);
ValueTime
.
appendTime
(
buff
,
timeNanos
,
true
);
appendTimeZone
(
buff
,
timeZoneOffsetMins
);
return
buff
.
toString
();
}
/**
* Append a time zone to the string builder.
*
* @param buff the target string builder
* @param tz the time zone in minutes
*/
private
static
void
appendTimeZone
(
StringBuilder
buff
,
short
tz
)
{
if
(
tz
<
0
)
{
buff
.
append
(
'-'
);
tz
=
(
short
)
-
tz
;
}
else
{
buff
.
append
(
'+'
);
}
int
hours
=
tz
/
60
;
tz
-=
hours
*
60
;
int
mins
=
tz
;
StringUtils
.
appendZeroPadded
(
buff
,
2
,
hours
);
if
(
mins
!=
0
)
{
buff
.
append
(
':'
);
StringUtils
.
appendZeroPadded
(
buff
,
2
,
mins
);
}
}
}
@Override
@Override
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论