Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
60659b6f
提交
60659b6f
authored
3月 16, 2011
作者:
Thomas Mueller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Some time and timezone functions didn't support years before 1.
上级
12ee4c9f
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
47 行增加
和
0 行删除
+47
-0
DateTimeUtils.java
h2/src/main/org/h2/util/DateTimeUtils.java
+16
-0
TestDate.java
h2/src/test/org/h2/test/unit/TestDate.java
+31
-0
没有找到文件。
h2/src/main/org/h2/util/DateTimeUtils.java
浏览文件 @
60659b6f
...
...
@@ -81,6 +81,7 @@ public class DateTimeUtils {
long
time
;
synchronized
(
cal
)
{
cal
.
setTime
(
value
);
cal
.
set
(
Calendar
.
ERA
,
GregorianCalendar
.
AD
);
// month is 0 based
cal
.
set
(
DEFAULT_YEAR
,
DEFAULT_MONTH
-
1
,
DEFAULT_DAY
);
time
=
cal
.
getTime
().
getTime
();
...
...
@@ -185,6 +186,7 @@ public class DateTimeUtils {
}
private
static
void
convertTime
(
Calendar
from
,
Calendar
to
)
{
to
.
set
(
Calendar
.
ERA
,
from
.
get
(
Calendar
.
ERA
));
to
.
set
(
Calendar
.
YEAR
,
from
.
get
(
Calendar
.
YEAR
));
to
.
set
(
Calendar
.
MONTH
,
from
.
get
(
Calendar
.
MONTH
));
to
.
set
(
Calendar
.
DAY_OF_MONTH
,
from
.
get
(
Calendar
.
DAY_OF_MONTH
));
...
...
@@ -401,6 +403,20 @@ public class DateTimeUtils {
return
value
;
}
/**
* Get the year (positive or negative) from a calendar.
*
* @param calendar the calendar
* @return the year
*/
public
static
int
getYear
(
Calendar
calendar
)
{
int
year
=
calendar
.
get
(
Calendar
.
YEAR
);
if
(
calendar
.
get
(
Calendar
.
ERA
)
==
GregorianCalendar
.
BC
)
{
year
=
1
-
year
;
}
return
year
;
}
/**
* Get the number of milliseconds since 1970-01-01 in the local timezone.
*
...
...
h2/src/test/org/h2/test/unit/TestDate.java
浏览文件 @
60659b6f
...
...
@@ -10,12 +10,18 @@ import java.sql.Connection;
import
java.sql.PreparedStatement
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
import
java.sql.Timestamp
;
import
java.util.Calendar
;
import
java.util.GregorianCalendar
;
import
java.util.TimeZone
;
import
org.h2.constant.ErrorCode
;
import
org.h2.test.TestBase
;
import
org.h2.util.DateTimeUtils
;
import
org.h2.value.Value
;
import
org.h2.value.ValueDate
;
import
org.h2.value.ValueTime
;
import
org.h2.value.ValueTimestamp
;
/**
* Tests the data parsing. The problem is that some dates are not allowed
...
...
@@ -35,10 +41,35 @@ public class TestDate extends TestBase {
}
public
void
test
()
throws
SQLException
{
testDateTimeUtils
();
testAllTimeZones
();
testCurrentTimeZone
();
}
private
void
testDateTimeUtils
()
{
java
.
sql
.
Timestamp
ts1
=
(
Timestamp
)
DateTimeUtils
.
parseDateTime
(
"-999-08-07 13:14:15.16"
,
Value
.
TIMESTAMP
,
0
);
java
.
sql
.
Timestamp
ts2
=
(
Timestamp
)
DateTimeUtils
.
parseDateTime
(
"19999-08-07 13:14:15.16"
,
Value
.
TIMESTAMP
,
0
);
java
.
sql
.
Time
t1
=
DateTimeUtils
.
cloneAndNormalizeTime
(
new
java
.
sql
.
Time
(
ts1
.
getTime
()));
java
.
sql
.
Time
t2
=
DateTimeUtils
.
cloneAndNormalizeTime
(
new
java
.
sql
.
Time
(
ts2
.
getTime
()));
java
.
sql
.
Date
d1
=
DateTimeUtils
.
cloneAndNormalizeDate
(
new
java
.
sql
.
Date
(
ts1
.
getTime
()));
java
.
sql
.
Date
d2
=
DateTimeUtils
.
cloneAndNormalizeDate
(
new
java
.
sql
.
Date
(
ts2
.
getTime
()));
assertEquals
(
"-999-08-07 13:14:15.16"
,
ValueTimestamp
.
get
(
ts1
).
getString
());
assertEquals
(
"-999-08-07"
,
ValueDate
.
get
(
d1
).
getString
());
assertEquals
(
"13:14:15"
,
ValueTime
.
get
(
t1
).
getString
());
assertEquals
(
"19999-08-07 13:14:15.16"
,
ValueTimestamp
.
get
(
ts2
).
getString
());
assertEquals
(
"19999-08-07"
,
ValueDate
.
get
(
d2
).
getString
());
assertEquals
(
"13:14:15"
,
ValueTime
.
get
(
t2
).
getString
());
Calendar
cal
=
Calendar
.
getInstance
();
cal
.
setTime
(
t1
);
assertEquals
(
GregorianCalendar
.
AD
,
cal
.
get
(
Calendar
.
ERA
));
cal
.
setTime
(
t2
);
assertEquals
(
GregorianCalendar
.
AD
,
cal
.
get
(
Calendar
.
ERA
));
java
.
sql
.
Timestamp
ts1a
=
DateTimeUtils
.
convertTimestampToCalendar
(
ts1
,
Calendar
.
getInstance
());
java
.
sql
.
Timestamp
ts2a
=
DateTimeUtils
.
convertTimestampToCalendar
(
ts2
,
Calendar
.
getInstance
());
assertEquals
(
"-999-08-07 13:14:15.16"
,
ValueTimestamp
.
get
(
ts1a
).
getString
());
assertEquals
(
"19999-08-07 13:14:15.16"
,
ValueTimestamp
.
get
(
ts2a
).
getString
());
}
private
static
void
testCurrentTimeZone
()
{
for
(
int
year
=
1970
;
year
<
2050
;
year
+=
3
)
{
for
(
int
month
=
1
;
month
<=
12
;
month
++)
{
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论