Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
d7aa67f5
提交
d7aa67f5
authored
10月 11, 2016
作者:
Noel Grandin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
comment some of the TIMESTAMP code better
上级
2a871b0b
显示空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
87 行增加
和
85 行删除
+87
-85
TimestampWithTimeZone.java
h2/src/main/org/h2/api/TimestampWithTimeZone.java
+14
-12
DateTimeUtils.java
h2/src/main/org/h2/util/DateTimeUtils.java
+63
-71
ValueTime.java
h2/src/main/org/h2/value/ValueTime.java
+10
-2
没有找到文件。
h2/src/main/org/h2/api/TimestampWithTimeZone.java
浏览文件 @
d7aa67f5
...
...
@@ -10,7 +10,7 @@ import org.h2.util.DateTimeUtils;
import
org.h2.util.StringUtils
;
/**
* How we expose "
DATETIME
WITH TIMEZONE" in our ResultSets.
* How we expose "
TIMESTAMP
WITH TIMEZONE" in our ResultSets.
*/
public
class
TimestampWithTimeZone
implements
Serializable
,
Cloneable
{
...
...
@@ -170,27 +170,29 @@ public class TimestampWithTimeZone implements Serializable, Cloneable {
@Override
public
int
hashCode
()
{
return
31
*
super
.
hashCode
()
+
timeZoneOffsetMins
;
final
int
prime
=
31
;
int
result
=
1
;
result
=
prime
*
result
+
(
int
)
(
dateValue
^
(
dateValue
>>>
32
));
result
=
prime
*
result
+
(
int
)
(
timeNanos
^
(
timeNanos
>>>
32
));
result
=
prime
*
result
+
timeZoneOffsetMins
;
return
result
;
}
@Override
public
boolean
equals
(
Object
obj
)
{
if
(
this
==
obj
)
{
if
(
this
==
obj
)
return
true
;
}
if
(
getClass
()
!=
obj
.
getClass
())
{
if
(
obj
==
null
)
return
false
;
if
(
getClass
()
!=
obj
.
getClass
())
return
false
;
}
TimestampWithTimeZone
other
=
(
TimestampWithTimeZone
)
obj
;
if
(
dateValue
!=
other
.
dateValue
)
{
if
(
dateValue
!=
other
.
dateValue
)
return
false
;
}
if
(
timeNanos
!=
other
.
timeNanos
)
{
if
(
timeNanos
!=
other
.
timeNanos
)
return
false
;
}
if
(
timeZoneOffsetMins
!=
other
.
timeZoneOffsetMins
)
{
if
(
timeZoneOffsetMins
!=
other
.
timeZoneOffsetMins
)
return
false
;
}
return
true
;
}
...
...
h2/src/main/org/h2/util/DateTimeUtils.java
浏览文件 @
d7aa67f5
/*
* Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0,
* and the EPL 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
* Iso8601:
* Initial Developer: Robert Rathsack (firstName dot lastName at gmx dot de)
* Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0, and the
* EPL 1.0 (http://h2database.com/html/license.html). Initial Developer: H2
* Group Iso8601: Initial Developer: Robert Rathsack (firstName dot lastName at
* gmx dot de)
*/
package
org
.
h2
.
util
;
...
...
@@ -26,8 +25,8 @@ import org.h2.value.ValueTimestamp;
/**
* This utility class contains time conversion functions.
* <p>
* Date value: a bit field with bits for the year, month, and day.
*
Absolute day:
the day number (0 means 1970-01-01).
* Date value: a bit field with bits for the year, month, and day.
Absolute day:
* the day number (0 means 1970-01-01).
*/
public
class
DateTimeUtils
{
...
...
@@ -58,24 +57,23 @@ public class DateTimeUtils {
* have that problem, and while it is still a small memory leak, it is not a
* class loader memory leak.
*/
private
static
final
ThreadLocal
<
Calendar
>
CACHED_CALENDAR
=
new
ThreadLocal
<
Calendar
>();
private
static
final
ThreadLocal
<
Calendar
>
CACHED_CALENDAR
=
new
ThreadLocal
<
Calendar
>();
/**
* A cached instance of Calendar used when a timezone is specified.
*/
private
static
final
ThreadLocal
<
Calendar
>
CACHED_CALENDAR_NON_DEFAULT_TIMEZONE
=
new
ThreadLocal
<
Calendar
>();
private
static
final
ThreadLocal
<
Calendar
>
CACHED_CALENDAR_NON_DEFAULT_TIMEZONE
=
new
ThreadLocal
<
Calendar
>();
/**
* Observed JVM behaviour is that if the timezone of the host computer is
changed while the JVM is running,
*
the zone offset does not change but keeps the initial value. So it is correct to measure this once and use
*
this value throughout the JVM's lifecycle.
*
*
In any case, it is safer to use a fixed value throughout the duration of the JVM's life, rather than have
* this offset change, possibly midway through a long-running query.
* Observed JVM behaviour is that if the timezone of the host computer is
*
changed while the JVM is running, the zone offset does not change but
*
keeps the initial value. So it is correct to measure this once and use
*
this value throughout the JVM's lifecycle. In any case, it is safer to
*
use a fixed value throughout the duration of the JVM's life, rather than
*
have
this offset change, possibly midway through a long-running query.
*/
private
static
int
zoneOffsetMillis
=
Calendar
.
getInstance
().
get
(
Calendar
.
ZONE_OFFSET
);
private
static
int
zoneOffsetMillis
=
Calendar
.
getInstance
()
.
get
(
Calendar
.
ZONE_OFFSET
);
private
DateTimeUtils
()
{
// utility class
...
...
@@ -138,9 +136,8 @@ public class DateTimeUtils {
cal
.
setLenient
(
true
);
long
dateValue
=
d
.
getDateValue
();
setCalendarFields
(
cal
,
yearFromDateValue
(
dateValue
),
monthFromDateValue
(
dateValue
),
dayFromDateValue
(
dateValue
),
0
,
0
,
0
,
0
);
monthFromDateValue
(
dateValue
),
dayFromDateValue
(
dateValue
),
0
,
0
,
0
,
0
);
long
ms
=
cal
.
getTimeInMillis
();
return
new
Date
(
ms
);
}
...
...
@@ -169,8 +166,8 @@ public class DateTimeUtils {
s
-=
m
*
60
;
long
h
=
m
/
60
;
m
-=
h
*
60
;
setCalendarFields
(
cal
,
1970
,
1
,
1
,
(
int
)
h
,
(
int
)
m
,
(
int
)
s
,
(
int
)
millis
);
setCalendarFields
(
cal
,
1970
,
1
,
1
,
(
int
)
h
,
(
int
)
m
,
(
int
)
s
,
(
int
)
millis
);
long
ms
=
cal
.
getTimeInMillis
();
return
new
Time
(
ms
);
}
...
...
@@ -201,8 +198,7 @@ public class DateTimeUtils {
long
h
=
m
/
60
;
m
-=
h
*
60
;
setCalendarFields
(
cal
,
yearFromDateValue
(
dateValue
),
monthFromDateValue
(
dateValue
),
dayFromDateValue
(
dateValue
),
monthFromDateValue
(
dateValue
),
dayFromDateValue
(
dateValue
),
(
int
)
h
,
(
int
)
m
,
(
int
)
s
,
(
int
)
millis
);
long
ms
=
cal
.
getTimeInMillis
();
Timestamp
x
=
new
Timestamp
(
ms
);
...
...
@@ -211,7 +207,7 @@ public class DateTimeUtils {
}
/**
* Convert
the d
ate using the specified calendar.
* Convert
a java.util.D
ate using the specified calendar.
*
* @param x the date
* @param calendar the calendar
...
...
@@ -282,7 +278,8 @@ public class DateTimeUtils {
* @param calendar the calendar
* @return the timestamp
*/
public
static
ValueTimestamp
convertTimestamp
(
Timestamp
x
,
Calendar
calendar
)
{
public
static
ValueTimestamp
convertTimestamp
(
Timestamp
x
,
Calendar
calendar
)
{
if
(
calendar
==
null
)
{
throw
DbException
.
getInvalidValueException
(
"calendar"
,
null
);
}
...
...
@@ -372,8 +369,8 @@ public class DateTimeUtils {
String
n
=
(
s
.
substring
(
s3
+
1
,
end
)
+
"000000000"
).
substring
(
0
,
9
);
nanos
=
Integer
.
parseInt
(
n
);
}
if
(
hour
>=
2000000
||
minute
<
0
||
minute
>=
60
||
second
<
0
||
second
>=
60
)
{
if
(
hour
>=
2000000
||
minute
<
0
||
minute
>=
60
||
second
<
0
||
second
>=
60
)
{
throw
new
IllegalArgumentException
(
s
);
}
if
(
timeOfDay
&&
hour
>=
24
)
{
...
...
@@ -387,8 +384,8 @@ public class DateTimeUtils {
* Calculate the milliseconds since 1970-01-01 (UTC) for the given date and
* time (in the specified timezone).
*
* @param tz the timezone of the parameters,
* or null for the default
timezone
* @param tz the timezone of the parameters,
or null for the default
*
timezone
* @param year the absolute year (positive or negative)
* @param month the month (1-12)
* @param day the day (1-31)
...
...
@@ -401,7 +398,8 @@ public class DateTimeUtils {
public
static
long
getMillis
(
TimeZone
tz
,
int
year
,
int
month
,
int
day
,
int
hour
,
int
minute
,
int
second
,
int
millis
)
{
try
{
return
getTimeTry
(
false
,
tz
,
year
,
month
,
day
,
hour
,
minute
,
second
,
millis
);
return
getTimeTry
(
false
,
tz
,
year
,
month
,
day
,
hour
,
minute
,
second
,
millis
);
}
catch
(
IllegalArgumentException
e
)
{
// special case: if the time simply doesn't exist because of
// daylight saving time changes, use the lenient version
...
...
@@ -435,9 +433,8 @@ public class DateTimeUtils {
}
}
private
static
long
getTimeTry
(
boolean
lenient
,
TimeZone
tz
,
int
year
,
int
month
,
int
day
,
int
hour
,
int
minute
,
int
second
,
int
millis
)
{
private
static
long
getTimeTry
(
boolean
lenient
,
TimeZone
tz
,
int
year
,
int
month
,
int
day
,
int
hour
,
int
minute
,
int
second
,
int
millis
)
{
Calendar
c
;
if
(
tz
==
null
)
{
c
=
getCalendar
();
...
...
@@ -529,7 +526,6 @@ public class DateTimeUtils {
* starts at Monday. See also http://en.wikipedia.org/wiki/ISO_8601
*
* @author Robert Rathsack
*
* @param date the date object which day of week should be calculated
* @return the day of the week, Monday as 1 to Sunday as 7
*/
...
...
@@ -566,7 +562,6 @@ public class DateTimeUtils {
* Returns the year according to the ISO week definition.
*
* @author Robert Rathsack
*
* @param date the date object which year should be calculated
* @return the year
*/
...
...
@@ -680,35 +675,35 @@ public class DateTimeUtils {
}
/**
* Convert a date value to a date, using the default timezone.
* Convert an encoded date value to a java.util.Date, using the default
* timezone.
*
* @param dateValue the date value
* @return the date
*/
public
static
Date
convertDateValueToDate
(
long
dateValue
)
{
long
millis
=
getMillis
(
null
,
yearFromDateValue
(
dateValue
),
monthFromDateValue
(
dateValue
),
dayFromDateValue
(
dateValue
),
0
,
0
,
0
,
0
);
long
millis
=
getMillis
(
null
,
yearFromDateValue
(
dateValue
),
monthFromDateValue
(
dateValue
),
dayFromDateValue
(
dateValue
),
0
,
0
,
0
,
0
);
return
new
Date
(
millis
);
}
/**
* Convert a date value to millis, using the supplied timezone.
* Convert a
n encoded
date value to millis, using the supplied timezone.
*
* @param tz the timezone
* @param dateValue the date value
* @return the date
*/
public
static
long
convertDateValueToMillis
(
TimeZone
tz
,
long
dateValue
)
{
return
getMillis
(
tz
,
yearFromDateValue
(
dateValue
),
monthFromDateValue
(
dateValue
),
dayFromDateValue
(
dateValue
),
0
,
0
,
0
,
0
);
return
getMillis
(
tz
,
yearFromDateValue
(
dateValue
),
monthFromDateValue
(
dateValue
),
dayFromDateValue
(
dateValue
),
0
,
0
,
0
,
0
);
}
/**
* Convert a
date value / time value to a timestamp, using the default
* timezone.
* Convert a
n encoded date value / time value to a timestamp, using the
*
default
timezone.
*
* @param dateValue the date value
* @param timeNanos the nanoseconds since midnight
...
...
@@ -724,10 +719,8 @@ public class DateTimeUtils {
s
-=
m
*
60
;
long
h
=
m
/
60
;
m
-=
h
*
60
;
long
ms
=
getMillis
(
null
,
yearFromDateValue
(
dateValue
),
monthFromDateValue
(
dateValue
),
dayFromDateValue
(
dateValue
),
long
ms
=
getMillis
(
null
,
yearFromDateValue
(
dateValue
),
monthFromDateValue
(
dateValue
),
dayFromDateValue
(
dateValue
),
(
int
)
h
,
(
int
)
m
,
(
int
)
s
,
0
);
Timestamp
ts
=
new
Timestamp
(
ms
);
ts
.
setNanos
((
int
)
(
timeNanos
+
millis
*
1000000
));
...
...
@@ -735,22 +728,21 @@ public class DateTimeUtils {
}
/**
* Convert a time value to a time, using the default
* timezone.
* Convert a time value to a time, using the default timezone.
*
* @param nanos the nanoseconds since midnight
* @param nanos
SinceMidnight
the nanoseconds since midnight
* @return the time
*/
public
static
Time
convertNanoToTime
(
long
nanos
)
{
long
millis
=
nanos
/
1000000
;
public
static
Time
convertNanoToTime
(
long
nanos
SinceMidnight
)
{
long
millis
=
nanos
SinceMidnight
/
1000000
;
long
s
=
millis
/
1000
;
millis
-=
s
*
1000
;
long
m
=
s
/
60
;
s
-=
m
*
60
;
long
h
=
m
/
60
;
m
-=
h
*
60
;
long
ms
=
getMillis
(
null
,
1970
,
1
,
1
,
(
int
)
(
h
%
24
),
(
int
)
m
,
(
int
)
s
,
(
int
)
millis
);
long
ms
=
getMillis
(
null
,
1970
,
1
,
1
,
(
int
)
(
h
%
24
),
(
int
)
m
,
(
int
)
s
,
(
int
)
millis
);
return
new
Time
(
ms
);
}
...
...
@@ -797,8 +789,8 @@ public class DateTimeUtils {
}
/**
* C
alculate the date value (in the default timezone) from a given time in
*
milliseconds in UTC
.
* C
onvert a UTC datetime in millis to an encoded date in the default
*
timezone
.
*
* @param ms the milliseconds
* @return the date value
...
...
@@ -810,7 +802,7 @@ public class DateTimeUtils {
}
/**
* Calculate the date value from a given calendar.
* Calculate the
encoded
date value from a given calendar.
*
* @param cal the calendar
* @return the date value
...
...
@@ -824,8 +816,8 @@ public class DateTimeUtils {
}
/**
* C
alculate the nanoseconds since midnight (in the default timezone) from a
*
given time in milliseconds in UTC
.
* C
onvert a time in milliseconds in UTC to the nanoseconds since midnight
*
(in the default timezone)
.
*
* @param ms the milliseconds
* @return the nanoseconds
...
...
@@ -837,7 +829,7 @@ public class DateTimeUtils {
}
/**
* C
alculate the nanoseconds since midnight from a given calendar
.
* C
onvert a java.util.Calendar to nanoseconds since midnight
.
*
* @param cal the calendar
* @return the nanoseconds
...
...
@@ -857,7 +849,8 @@ public class DateTimeUtils {
* @param nanos the nanoseconds (may be negative or larger than one day)
* @return the timestamp
*/
public
static
ValueTimestamp
normalizeTimestamp
(
long
absoluteDay
,
long
nanos
)
{
public
static
ValueTimestamp
normalizeTimestamp
(
long
absoluteDay
,
long
nanos
)
{
if
(
nanos
>
NANOS_PER_DAY
||
nanos
<
0
)
{
long
d
;
if
(
nanos
>
NANOS_PER_DAY
)
{
...
...
@@ -873,7 +866,7 @@ public class DateTimeUtils {
}
/**
* Calculate the absolute day from a date value.
* Calculate the absolute day from a
n encoded
date value.
*
* @param dateValue the date value
* @return the absolute day
...
...
@@ -898,7 +891,7 @@ public class DateTimeUtils {
}
/**
* Calculate the date value from an absolute day.
* Calculate the
encoded
date value from an absolute day.
*
* @param absoluteDay the absolute day
* @return the date value
...
...
@@ -940,8 +933,7 @@ public class DateTimeUtils {
/**
* Adds the number of months to the date. If the resulting month's number of
* days is less than the original's day-of-month, the resulting
* day-of-months gets adjusted accordingly:
* <br>
* day-of-months gets adjusted accordingly: <br>
* 30.04.2007 - 2 months = 28.02.2007
*
* @param refDate the original date
...
...
h2/src/main/org/h2/value/ValueTime.java
浏览文件 @
d7aa67f5
...
...
@@ -30,8 +30,14 @@ public class ValueTime extends Value {
*/
static
final
int
DISPLAY_SIZE
=
8
;
/**
* Nanoseconds since midnight
*/
private
final
long
nanos
;
/**
* @param nanos nanoseconds since midnight
*/
private
ValueTime
(
long
nanos
)
{
this
.
nanos
=
nanos
;
}
...
...
@@ -39,7 +45,7 @@ public class ValueTime extends Value {
/**
* Get or create a time value.
*
* @param nanos the nanoseconds
* @param nanos the nanoseconds
since midnight
* @return the value
*/
public
static
ValueTime
fromNanos
(
long
nanos
)
{
...
...
@@ -73,7 +79,6 @@ public class ValueTime extends Value {
* @param s the string to parse
* @return the time
*/
public
static
ValueTime
parse
(
String
s
)
{
try
{
return
fromNanos
(
DateTimeUtils
.
parseTimeNanos
(
s
,
0
,
s
.
length
(),
false
));
...
...
@@ -83,6 +88,9 @@ public class ValueTime extends Value {
}
}
/**
* @return nanoseconds since midnight
*/
public
long
getNanos
()
{
return
nanos
;
}
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论