Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
487e9e87
提交
487e9e87
authored
1月 06, 2007
作者:
Thomas Mueller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
--no commit message
--no commit message
上级
8c989942
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
220 行增加
和
0 行删除
+220
-0
DateTimeUtils.java
h2/src/main/org/h2/util/DateTimeUtils.java
+220
-0
没有找到文件。
h2/src/main/org/h2/util/DateTimeUtils.java
0 → 100644
浏览文件 @
487e9e87
/*
* Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package
org
.
h2
.
util
;
import
java.sql.Date
;
import
java.sql.SQLException
;
import
java.sql.Time
;
import
java.sql.Timestamp
;
import
java.util.Calendar
;
import
java.util.GregorianCalendar
;
import
java.util.TimeZone
;
import
org.h2.message.Message
;
import
org.h2.value.Value
;
import
org.h2.value.ValueDate
;
import
org.h2.value.ValueTime
;
import
org.h2.value.ValueTimestamp
;
public
class
DateTimeUtils
{
public
static
Timestamp
convertTimestampToCalendar
(
Timestamp
x
,
Calendar
calendar
)
throws
SQLException
{
if
(
x
!=
null
)
{
Timestamp
y
=
new
Timestamp
(
getLocalTime
(
x
,
calendar
));
// fix the nano seconds
y
.
setNanos
(
x
.
getNanos
());
x
=
y
;
}
return
x
;
}
public
static
Time
cloneAndNormalizeTime
(
Time
value
)
{
Calendar
cal
=
Calendar
.
getInstance
();
cal
.
setTime
(
value
);
// TODO gcj: required so that the millis are calculated?
cal
.
get
(
Calendar
.
HOUR_OF_DAY
);
cal
.
set
(
1970
,
0
,
1
);
return
new
Time
(
cal
.
getTime
().
getTime
());
}
public
static
Date
cloneAndNormalizeDate
(
Date
value
)
{
Calendar
cal
=
Calendar
.
getInstance
();
cal
.
setTime
(
value
);
// TODO gcj: required so that the millis are calculated?
cal
.
get
(
Calendar
.
YEAR
);
cal
.
set
(
Calendar
.
MILLISECOND
,
0
);
cal
.
set
(
Calendar
.
SECOND
,
0
);
cal
.
set
(
Calendar
.
MINUTE
,
0
);
cal
.
set
(
Calendar
.
HOUR_OF_DAY
,
0
);
return
new
Date
(
cal
.
getTime
().
getTime
());
}
public
static
Value
convertDateToUniversal
(
Date
x
,
Calendar
source
)
throws
SQLException
{
return
ValueDate
.
get
(
new
Date
(
getUniversalTime
(
source
,
x
)));
}
public
static
Value
convertTimeToUniversal
(
Time
x
,
Calendar
source
)
throws
SQLException
{
return
ValueTime
.
get
(
new
Time
(
getUniversalTime
(
source
,
x
)));
}
public
static
Value
convertTimestampToUniversal
(
Timestamp
x
,
Calendar
source
)
throws
SQLException
{
Timestamp
y
=
new
Timestamp
(
getUniversalTime
(
source
,
x
));
// fix the nano seconds
y
.
setNanos
(
x
.
getNanos
());
return
ValueTimestamp
.
getNoCopy
(
y
);
}
private
static
long
getUniversalTime
(
Calendar
source
,
java
.
util
.
Date
x
)
throws
SQLException
{
if
(
source
==
null
)
{
throw
Message
.
getInvalidValueException
(
"calendar"
,
null
);
}
source
=
(
Calendar
)
source
.
clone
();
Calendar
universal
=
Calendar
.
getInstance
();
source
.
setTime
(
x
);
convertTime
(
source
,
universal
);
return
universal
.
getTime
().
getTime
();
}
private
static
long
getLocalTime
(
java
.
util
.
Date
x
,
Calendar
target
)
throws
SQLException
{
if
(
target
==
null
)
{
throw
Message
.
getInvalidValueException
(
"calendar"
,
null
);
}
target
=
(
Calendar
)
target
.
clone
();
Calendar
local
=
Calendar
.
getInstance
();
local
.
setTime
(
x
);
convertTime
(
local
,
target
);
return
target
.
getTime
().
getTime
();
}
private
static
void
convertTime
(
Calendar
from
,
Calendar
to
)
{
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
));
to
.
set
(
Calendar
.
HOUR_OF_DAY
,
from
.
get
(
Calendar
.
HOUR_OF_DAY
));
to
.
set
(
Calendar
.
MINUTE
,
from
.
get
(
Calendar
.
MINUTE
));
to
.
set
(
Calendar
.
SECOND
,
from
.
get
(
Calendar
.
SECOND
));
to
.
set
(
Calendar
.
MILLISECOND
,
from
.
get
(
Calendar
.
MILLISECOND
));
}
public
static
Date
convertDateToCalendar
(
Date
x
,
Calendar
calendar
)
throws
SQLException
{
return
x
==
null
?
x
:
new
Date
(
getLocalTime
(
x
,
calendar
));
}
public
static
Time
convertTimeToCalendar
(
Time
x
,
Calendar
calendar
)
throws
SQLException
{
return
x
==
null
?
x
:
new
Time
(
getLocalTime
(
x
,
calendar
));
}
public
static
java
.
util
.
Date
parseDateTime
(
String
s
,
int
type
,
int
errorCode
)
throws
SQLException
{
if
(
s
==
null
)
{
return
null
;
}
try
{
int
timeStart
=
0
;
TimeZone
tz
=
null
;
if
(
type
==
Value
.
TIME
)
{
timeStart
=
0
;
}
else
{
timeStart
=
s
.
indexOf
(
' '
)
+
1
;
if
(
timeStart
<=
0
)
{
// ISO 8601 compatibility
timeStart
=
s
.
indexOf
(
'T'
)
+
1
;
}
}
int
year
=
1970
,
month
=
1
,
day
=
1
;
if
(
type
!=
Value
.
TIME
)
{
// support +year
if
(
s
.
startsWith
(
"+"
))
{
s
=
s
.
substring
(
1
);
}
// start at position 1 to support -year
int
s1
=
s
.
indexOf
(
'-'
,
1
);
int
s2
=
s
.
indexOf
(
'-'
,
s1
+
1
);
if
(
s1
<=
0
||
s2
<=
s1
)
{
throw
Message
.
getSQLException
(
errorCode
,
s
);
}
year
=
Integer
.
parseInt
(
s
.
substring
(
0
,
s1
));
month
=
Integer
.
parseInt
(
s
.
substring
(
s1
+
1
,
s2
));
int
end
=
timeStart
==
0
?
s
.
length
()
:
timeStart
-
1
;
day
=
Integer
.
parseInt
(
s
.
substring
(
s2
+
1
,
end
));
}
int
hour
=
0
,
minute
=
0
,
second
=
0
,
nano
=
0
;
if
(
type
!=
Value
.
DATE
)
{
int
s1
=
s
.
indexOf
(
':'
,
timeStart
);
int
s2
=
s
.
indexOf
(
':'
,
s1
+
1
);
int
s3
=
s
.
indexOf
(
'.'
,
s2
+
1
);
if
(
s1
<=
0
||
s2
<=
s1
)
{
throw
Message
.
getSQLException
(
errorCode
,
s
);
}
if
(
s
.
endsWith
(
"Z"
))
{
s
=
s
.
substring
(
0
,
s
.
length
()-
1
);
}
else
{
int
tzstart
=
s
.
indexOf
(
'+'
,
s2
+
1
);
if
(
tzstart
<
0
)
{
tzstart
=
s
.
indexOf
(
'-'
,
s2
+
1
);
}
if
(
tzstart
>=
0
)
{
String
tzName
=
"GMT"
+
s
.
substring
(
tzstart
);
tz
=
TimeZone
.
getTimeZone
(
tzName
);
if
(!
tz
.
getID
().
equals
(
tzName
))
{
throw
Message
.
getSQLException
(
errorCode
,
s
+
" "
+
tz
.
getID
()
+
"/"
+
tzName
);
}
s
=
s
.
substring
(
0
,
tzstart
);
}
}
hour
=
Integer
.
parseInt
(
s
.
substring
(
timeStart
,
s1
));
minute
=
Integer
.
parseInt
(
s
.
substring
(
s1
+
1
,
s2
));
if
(
s3
<
0
)
{
second
=
Integer
.
parseInt
(
s
.
substring
(
s2
+
1
));
}
else
{
second
=
Integer
.
parseInt
(
s
.
substring
(
s2
+
1
,
s3
));
String
n
=
(
s
+
"000000000"
).
substring
(
s3
+
1
,
s3
+
10
);
nano
=
Integer
.
parseInt
(
n
);
}
}
Calendar
c
;
if
(
tz
==
null
)
{
c
=
Calendar
.
getInstance
();
}
else
{
c
=
Calendar
.
getInstance
(
tz
);
}
c
.
setLenient
(
false
);
long
time
;
if
(
year
<=
0
)
{
c
.
set
(
Calendar
.
ERA
,
GregorianCalendar
.
BC
);
c
.
set
(
Calendar
.
YEAR
,
1
-
year
);
}
else
{
c
.
set
(
Calendar
.
YEAR
,
year
);
}
c
.
set
(
Calendar
.
MONTH
,
month
-
1
);
// january is 0
c
.
set
(
Calendar
.
DAY_OF_MONTH
,
day
);
c
.
set
(
Calendar
.
HOUR_OF_DAY
,
hour
);
c
.
set
(
Calendar
.
MINUTE
,
minute
);
c
.
set
(
Calendar
.
SECOND
,
second
);
if
(
type
!=
Value
.
TIMESTAMP
)
{
c
.
set
(
Calendar
.
MILLISECOND
,
nano
/
1000000
);
}
time
=
c
.
getTime
().
getTime
();
switch
(
type
)
{
case
Value
.
DATE
:
return
new
java
.
sql
.
Date
(
time
);
case
Value
.
TIME
:
return
new
java
.
sql
.
Time
(
time
);
case
Value
.
TIMESTAMP
:
{
Timestamp
ts
=
new
Timestamp
(
time
);
ts
.
setNanos
(
nano
);
return
ts
;
}
default
:
throw
Message
.
getInternalError
(
"type:"
+
type
);
}
}
catch
(
IllegalArgumentException
e
)
{
throw
Message
.
getSQLException
(
errorCode
,
s
);
}
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论