Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
3adb165a
提交
3adb165a
authored
7月 04, 2011
作者:
Thomas Mueller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Javadocs.
上级
ac7b3bea
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
132 行增加
和
9 行删除
+132
-9
DateTimeUtils.java
h2/src/main/org/h2/util/DateTimeUtils.java
+110
-0
ValueDate.java
h2/src/main/org/h2/value/ValueDate.java
+6
-0
ValueTime.java
h2/src/main/org/h2/value/ValueTime.java
+15
-8
TestDate.java
h2/src/test/org/h2/test/unit/TestDate.java
+1
-1
没有找到文件。
h2/src/main/org/h2/util/DateTimeUtils.java
浏览文件 @
3adb165a
...
...
@@ -28,6 +28,9 @@ import org.h2.value.ValueTimestamp;
*/
public
class
DateTimeUtils
{
/**
* The number of milliseconds per day.
*/
public
static
final
long
MILLIS_PER_DAY
=
24
*
60
*
60
*
1000L
;
private
static
final
long
NANOS_PER_DAY
=
MILLIS_PER_DAY
*
1000000
;
...
...
@@ -164,6 +167,13 @@ public class DateTimeUtils {
}
}
/**
* Convert a date to the specified time zone.
*
* @param x the date to convert
* @param target the calendar with the target timezone
* @return the milliseconds the milliseconds in UTC
*/
public
static
long
convertToLocal
(
java
.
util
.
Date
x
,
Calendar
target
)
{
if
(
target
==
null
)
{
throw
DbException
.
getInvalidValueException
(
"calendar"
,
null
);
...
...
@@ -188,6 +198,15 @@ public class DateTimeUtils {
to
.
set
(
Calendar
.
MILLISECOND
,
from
.
get
(
Calendar
.
MILLISECOND
));
}
/**
* Parse a date string. The format is: [+|-]year-month-day
*
* @param s the string to parse
* @param start the parse index start
* @param end the parse index end
* @return the date value
* @throws IllegalArgumentException if there is a problem
*/
public
static
long
parseDateValue
(
String
s
,
int
start
,
int
end
)
{
if
(
s
.
charAt
(
start
)
==
'+'
)
{
// +year
...
...
@@ -208,6 +227,17 @@ public class DateTimeUtils {
return
dateValue
(
year
,
month
,
day
);
}
/**
* Parse a time string. The format is: [-]hour:minute:second[.nanos]
*
* @param s the string to parse
* @param start the parse index start
* @param end the parse index end
* @param timeOfDay whether the result need to be within 0 (inclusive) and 1
* day (exclusive)
* @return the time in nanoseconds
* @throws IllegalArgumentException if there is a problem
*/
public
static
long
parseTimeNanos
(
String
s
,
int
start
,
int
end
,
boolean
timeOfDay
)
{
int
hour
=
0
,
minute
=
0
,
second
=
0
;
long
nanos
=
0
;
...
...
@@ -533,6 +563,12 @@ public class DateTimeUtils {
return
day
<=
((
year
&
3
)
!=
0
?
28
:
29
);
}
/**
* Convert a date value to a date, using the default timezone.
*
* @param dateValue the date value
* @return the date
*/
public
static
Date
convertDateValueToDate
(
long
dateValue
)
{
long
millis
=
getMillis
(
TimeZone
.
getDefault
(),
yearFromDateValue
(
dateValue
),
...
...
@@ -541,6 +577,14 @@ public class DateTimeUtils {
return
new
Date
(
millis
);
}
/**
* Convert a date value / time value to a timestamp, using the default
* timezone.
*
* @param dateValue the date value
* @param nanos the nanoseconds since midnight
* @return the timestamp
*/
public
static
Timestamp
convertDateValueToTimestamp
(
long
dateValue
,
long
nanos
)
{
long
millis
=
nanos
/
1000000
;
nanos
-=
millis
*
1000000
;
...
...
@@ -560,6 +604,13 @@ public class DateTimeUtils {
return
ts
;
}
/**
* Convert a time value to a time, using the default
* timezone.
*
* @param nanos the nanoseconds since midnight
* @return the time
*/
public
static
Time
convertNanoToTime
(
long
nanos
)
{
long
millis
=
nanos
/
1000000
;
long
s
=
millis
/
1000
;
...
...
@@ -573,22 +624,55 @@ public class DateTimeUtils {
return
new
Time
(
ms
);
}
/**
* Get the year from a date value.
*
* @param x the date value
* @return the year
*/
public
static
int
yearFromDateValue
(
long
x
)
{
return
(
int
)
(
x
>>>
SHIFT_YEAR
);
}
/**
* Get the month from a date value.
*
* @param x the date value
* @return the month (1..12)
*/
public
static
int
monthFromDateValue
(
long
x
)
{
return
(
int
)
(
x
>>>
SHIFT_MONTH
)
&
15
;
}
/**
* Get the day of month from a date value.
*
* @param x the date value
* @return the day (1..31)
*/
public
static
int
dayFromDateValue
(
long
x
)
{
return
(
int
)
(
x
&
31
);
}
/**
* Get the date value from a given date.
*
* @param year the year
* @param month the month (1..12)
* @param day the day (1..31)
* @return the date value
*/
public
static
long
dateValue
(
long
year
,
int
month
,
int
day
)
{
return
(
year
<<
SHIFT_YEAR
)
|
(
month
<<
SHIFT_MONTH
)
|
day
;
}
/**
* Calculate the date value (in the default timezone) from a given time in
* milliseconds in UTC.
*
* @param ms the milliseconds
* @return the date value
*/
public
static
long
dateValueFromDate
(
long
ms
)
{
Calendar
cal
=
getCalendar
();
synchronized
(
cal
)
{
...
...
@@ -602,6 +686,13 @@ public class DateTimeUtils {
}
}
/**
* Calculate the nanoseconds since midnight (in the default timezone) from a
* given time in milliseconds in UTC.
*
* @param ms the milliseconds
* @return the date value
*/
public
static
long
nanosFromDate
(
long
ms
)
{
Calendar
cal
=
getCalendar
();
synchronized
(
cal
)
{
...
...
@@ -615,6 +706,13 @@ public class DateTimeUtils {
}
}
/**
* Calculate the normalized timestamp.
*
* @param absoluteDay the absolute day
* @param nanos the nanoseconds (may be negative or larger than one day)
* @return the timestamp
*/
public
static
ValueTimestamp
normalizeTimestamp
(
long
absoluteDay
,
long
nanos
)
{
if
(
nanos
>
NANOS_PER_DAY
||
nanos
<
0
)
{
long
d
;
...
...
@@ -629,6 +727,12 @@ public class DateTimeUtils {
return
ValueTimestamp
.
fromDateValueAndNanos
(
dateValueFromAbsoluteDay
(
absoluteDay
),
nanos
);
}
/**
* Calculate the absolute day from a date value.
*
* @param dateValue the date value
* @return the absolute day
*/
public
static
long
absoluteDayFromDateValue
(
long
dateValue
)
{
long
y
=
yearFromDateValue
(
dateValue
);
int
m
=
monthFromDateValue
(
dateValue
);
...
...
@@ -648,6 +752,12 @@ public class DateTimeUtils {
return
a
;
}
/**
* Calculate the date value from an absolute day.
*
* @param absoluteDay the absolute day
* @return the date value
*/
public
static
long
dateValueFromAbsoluteDay
(
long
absoluteDay
)
{
long
d
=
absoluteDay
+
719468
;
long
y100
=
0
,
offset
;
...
...
h2/src/main/org/h2/value/ValueDate.java
浏览文件 @
3adb165a
...
...
@@ -125,6 +125,12 @@ public class ValueDate extends Value {
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
);
...
...
h2/src/main/org/h2/value/ValueTime.java
浏览文件 @
3adb165a
...
...
@@ -152,13 +152,20 @@ public class ValueTime extends Value {
return
ValueTime
.
fromNanos
(-
nanos
);
}
static
void
appendTime
(
StringBuilder
buff
,
long
n
,
boolean
alwaysAddMillis
)
{
if
(
n
<
0
)
{
/**
* 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
(
'-'
);
n
=
-
n
;
n
anos
=
-
nanos
;
}
long
ms
=
n
/
1000000
;
n
-=
ms
*
1000000
;
long
ms
=
n
anos
/
1000000
;
n
anos
-=
ms
*
1000000
;
long
s
=
ms
/
1000
;
ms
-=
s
*
1000
;
long
m
=
s
/
60
;
...
...
@@ -170,12 +177,12 @@ public class ValueTime extends Value {
StringUtils
.
appendZeroPadded
(
buff
,
2
,
m
);
buff
.
append
(
':'
);
StringUtils
.
appendZeroPadded
(
buff
,
2
,
s
);
if
(
alwaysAddMillis
||
ms
>
0
||
n
>
0
)
{
if
(
alwaysAddMillis
||
ms
>
0
||
n
anos
>
0
)
{
buff
.
append
(
'.'
);
int
start
=
buff
.
length
();
StringUtils
.
appendZeroPadded
(
buff
,
3
,
ms
);
if
(
n
>
0
)
{
StringUtils
.
appendZeroPadded
(
buff
,
6
,
n
);
if
(
n
anos
>
0
)
{
StringUtils
.
appendZeroPadded
(
buff
,
6
,
n
anos
);
}
for
(
int
i
=
buff
.
length
()
-
1
;
i
>
start
;
i
--)
{
if
(
buff
.
charAt
(
i
)
!=
'0'
)
{
...
...
h2/src/test/org/h2/test/unit/TestDate.java
浏览文件 @
3adb165a
...
...
@@ -367,7 +367,7 @@ public class TestDate extends TestBase {
}
}
static
void
testDate
(
int
y
,
int
m
,
int
day
)
{
private
static
void
testDate
(
int
y
,
int
m
,
int
day
)
{
long
millis
=
DateTimeUtils
.
getMillis
(
TimeZone
.
getDefault
(),
y
,
m
,
day
,
0
,
0
,
0
,
0
);
String
st
=
new
java
.
sql
.
Date
(
millis
).
toString
();
int
y2
=
Integer
.
parseInt
(
st
.
substring
(
0
,
4
));
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论