Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
0316d856
提交
0316d856
authored
2月 06, 2018
作者:
Evgenij Ryazanov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Reimplement remaining parts of EXTRACT, ISO_YEAR, etc without a Calendar
上级
0200afe4
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
302 行增加
和
174 行删除
+302
-174
Function.java
h2/src/main/org/h2/expression/Function.java
+4
-10
DateTimeUtils.java
h2/src/main/org/h2/util/DateTimeUtils.java
+144
-65
TestDateIso8601.java
h2/src/test/org/h2/test/unit/TestDateIso8601.java
+98
-99
TestDateTimeUtils.java
h2/src/test/org/h2/test/unit/TestDateTimeUtils.java
+56
-0
没有找到文件。
h2/src/main/org/h2/expression/Function.java
浏览文件 @
0316d856
...
...
@@ -862,7 +862,10 @@ public class Function extends Expression implements FunctionCall {
break
;
}
case
QUARTER:
result
=
ValueInt
.
get
((
DateTimeUtils
.
getDatePart
(
v0
,
Calendar
.
MONTH
)
-
1
)
/
3
+
1
);
case
ISO_YEAR:
case
ISO_WEEK:
case
ISO_DAY_OF_WEEK:
result
=
ValueInt
.
get
(
DateTimeUtils
.
getDatePart
(
v0
,
info
.
type
));
break
;
case
SECOND:
result
=
ValueInt
.
get
(
DateTimeUtils
.
getDatePart
(
v0
,
Calendar
.
SECOND
));
...
...
@@ -873,15 +876,6 @@ public class Function extends Expression implements FunctionCall {
case
YEAR:
result
=
ValueInt
.
get
(
DateTimeUtils
.
getDatePart
(
v0
,
Calendar
.
YEAR
));
break
;
case
ISO_YEAR:
result
=
ValueInt
.
get
(
DateTimeUtils
.
getIsoYear
(
v0
));
break
;
case
ISO_WEEK:
result
=
ValueInt
.
get
(
DateTimeUtils
.
getIsoWeek
(
v0
));
break
;
case
ISO_DAY_OF_WEEK:
result
=
ValueInt
.
get
(
DateTimeUtils
.
getIsoDayOfWeek
(
v0
));
break
;
case
CURDATE:
case
CURRENT_DATE:
{
long
now
=
session
.
getTransactionStart
();
...
...
h2/src/main/org/h2/util/DateTimeUtils.java
浏览文件 @
0316d856
...
...
@@ -16,6 +16,7 @@ import java.util.Locale;
import
java.util.TimeZone
;
import
org.h2.api.ErrorCode
;
import
org.h2.engine.Mode
;
import
org.h2.expression.Function
;
import
org.h2.message.DbException
;
import
org.h2.value.Value
;
import
org.h2.value.ValueDate
;
...
...
@@ -24,6 +25,7 @@ import org.h2.value.ValueTime;
import
org.h2.value.ValueTimestamp
;
import
org.h2.value.ValueTimestampTimeZone
;
/**
* This utility class contains time conversion functions.
* <p>
...
...
@@ -298,30 +300,6 @@ public class DateTimeUtils {
return
ValueTimestamp
.
fromDateValueAndNanos
(
dateValue
,
nanos
);
}
private
static
Calendar
valueToCalendar
(
Value
value
)
{
Calendar
cal
;
if
(
value
instanceof
ValueTimestamp
)
{
cal
=
createGregorianCalendar
();
cal
.
setTime
(
value
.
getTimestamp
());
}
else
if
(
value
instanceof
ValueDate
)
{
cal
=
createGregorianCalendar
();
cal
.
setTime
(
value
.
getDate
());
}
else
if
(
value
instanceof
ValueTime
)
{
cal
=
createGregorianCalendar
();
cal
.
setTime
(
value
.
getTime
());
}
else
if
(
value
instanceof
ValueTimestampTimeZone
)
{
ValueTimestampTimeZone
v
=
(
ValueTimestampTimeZone
)
value
;
cal
=
createGregorianCalendar
(
v
.
getTimeZone
());
cal
.
setTimeInMillis
(
DateTimeUtils
.
convertDateValueToMillis
(
DateTimeUtils
.
UTC
,
v
.
getDateValue
())
+
v
.
getTimeNanos
()
/
1000000L
-
v
.
getTimeZoneOffsetMins
()
*
60000
);
}
else
{
cal
=
createGregorianCalendar
();
cal
.
setTime
(
value
.
getTimestamp
());
}
return
cal
;
}
/**
* Parse a date string. The format is: [+|-]year-month-day
*
...
...
@@ -608,7 +586,6 @@ public class DateTimeUtils {
timeNanos
=
v
.
getTimeNanos
();
}
else
{
ValueTimestamp
v
=
(
ValueTimestamp
)
date
.
convertTo
(
Value
.
TIMESTAMP
);
date
=
v
;
// For valueToCalendar() to avoid second convertTo() call
dateValue
=
v
.
getDateValue
();
timeNanos
=
v
.
getTimeNanos
();
}
...
...
@@ -627,8 +604,23 @@ public class DateTimeUtils {
return
(
int
)
(
timeNanos
/
1_000_000_000
%
60
);
case
Calendar
.
MILLISECOND
:
return
(
int
)
(
timeNanos
/
1_000_000
%
1_000
);
case
Calendar
.
DAY_OF_YEAR
:
return
getDayOfYear
(
dateValue
);
case
Calendar
.
DAY_OF_WEEK
:
return
getSundayDayOfWeek
(
dateValue
);
case
Calendar
.
WEEK_OF_YEAR
:
GregorianCalendar
gc
=
getCalendar
();
return
getWeekOfYear
(
dateValue
,
gc
.
getFirstDayOfWeek
()
-
1
,
gc
.
getMinimalDaysInFirstWeek
());
case
Function
.
QUARTER
:
return
(
monthFromDateValue
(
dateValue
)
-
1
)
/
3
+
1
;
case
Function
.
ISO_YEAR
:
return
getIsoWeekYear
(
dateValue
);
case
Function
.
ISO_WEEK
:
return
getIsoWeekOfYear
(
dateValue
);
case
Function
.
ISO_DAY_OF_WEEK
:
return
getIsoDayOfWeek
(
dateValue
);
}
return
valueToCalendar
(
date
).
get
(
field
);
throw
DbException
.
getUnsupportedException
(
"getDatePart("
+
date
+
", "
+
field
+
')'
);
}
/**
...
...
@@ -668,57 +660,144 @@ public class DateTimeUtils {
}
/**
* Return the day of week according to the ISO 8601 specification. Week
* starts at Monday. See also http://en.wikipedia.org/wiki/ISO_8601
* Returns day of week.
*
* @author Robert Rathsack
* @param value the date object which day of week should be calculated
* @return the day of the week, Monday as 1 to Sunday as 7
* @param dateValue
* the date value
* @param firstDayOfWeek
* first day of week, Monday as 1, Sunday as 7 or 0
* @return day of week
* @see #getIsoDayOfWeek(long)
*/
public
static
int
getIsoDayOfWeek
(
Value
value
)
{
int
val
=
valueToCalendar
(
value
).
get
(
Calendar
.
DAY_OF_WEEK
)
-
1
;
return
val
==
0
?
7
:
val
;
public
static
int
getDayOfWeek
(
long
dateValue
,
int
firstDayOfWeek
)
{
return
getDayOfWeekFromAbsolute
(
absoluteDayFromDateValue
(
dateValue
),
firstDayOfWeek
);
}
private
static
int
getDayOfWeekFromAbsolute
(
long
absoluteValue
,
int
firstDayOfWeek
)
{
return
absoluteValue
>=
0
?
(
int
)
((
absoluteValue
-
firstDayOfWeek
+
11
)
%
7
)
+
1
:
(
int
)
((
absoluteValue
-
firstDayOfWeek
-
2
)
%
7
)
+
7
;
}
/**
* Returns the week of the year according to the ISO 8601 specification. The
* spec defines the first week of the year as the week which contains at
* least 4 days of the new year. The week starts at Monday. Therefore
* December 29th - 31th could belong to the next year and January 1st - 3th
* could belong to the previous year. If January 1st is on Thursday (or
* earlier) it belongs to the first week, otherwise to the last week of the
* previous year. Hence January 4th always belongs to the first week while
* the December 28th always belongs to the last week.
* Returns number of day in year.
*
* @
author Robert Rathsack
*
@param value the date object which week of year should be calculated
* @return
the week of the
year
* @
param dateValue
*
the date value
* @return
number of day in
year
*/
public
static
int
getIsoWeek
(
Value
value
)
{
Calendar
c
=
valueToCalendar
(
value
);
c
.
setFirstDayOfWeek
(
Calendar
.
MONDAY
);
c
.
setMinimalDaysInFirstWeek
(
4
);
return
c
.
get
(
Calendar
.
WEEK_OF_YEAR
);
public
static
int
getDayOfYear
(
long
dateValue
)
{
int
year
=
yearFromDateValue
(
dateValue
);
return
(
int
)
(
absoluteDayFromDateValue
(
dateValue
)
-
absoluteDayFromDateValue
(
dateValue
(
year
,
1
,
1
)))
+
1
;
}
/**
* Returns
the year according to the ISO week definition
.
* Returns
ISO day of week
.
*
* @author Robert Rathsack
* @param value the date object which year should be calculated
* @return the year
* @param dateValue
* the date value
* @return ISO day of week, Monday as 1 to Sunday as 7
* @see #getSundayDayOfWeek(long)
*/
public
static
int
getIsoDayOfWeek
(
long
dateValue
)
{
return
getDayOfWeek
(
dateValue
,
1
);
}
/**
* Returns ISO number of week in year.
*
* @param dateValue
* the date value
* @return number of week in year
* @see #getIsoWeekYear(long)
* @see #getWeekOfYear(long, int, int)
*/
public
static
int
getIsoWeekOfYear
(
long
dateValue
)
{
return
getWeekOfYear
(
dateValue
,
1
,
4
);
}
/**
* Returns ISO week year.
*
* @param dateValue
* the date value
* @return ISO week year
* @see #getIsoWeekOfYear(long)
* @see #getWeekYear(long, int, int)
*/
public
static
int
getIsoYear
(
Value
value
)
{
Calendar
cal
=
valueToCalendar
(
value
);
cal
.
setFirstDayOfWeek
(
Calendar
.
MONDAY
);
cal
.
setMinimalDaysInFirstWeek
(
4
);
int
year
=
getYear
(
cal
);
int
month
=
cal
.
get
(
Calendar
.
MONTH
);
int
week
=
cal
.
get
(
Calendar
.
WEEK_OF_YEAR
);
if
(
month
==
0
&&
week
>
51
)
{
year
--;
}
else
if
(
month
==
11
&&
week
==
1
)
{
year
++;
public
static
int
getIsoWeekYear
(
long
dateValue
)
{
return
getWeekYear
(
dateValue
,
1
,
4
);
}
/**
* Returns day of week with Sunday as 1.
*
* @param dateValue
* the date value
* @return day of week, Sunday as 1 to Monday as 7
* @see #getIsoDayOfWeek(long)
*/
public
static
int
getSundayDayOfWeek
(
long
dateValue
)
{
return
getDayOfWeek
(
dateValue
,
0
);
}
/**
* Returns number of week in year.
*
* @param dateValue
* the date value
* @param firstDayOfWeek
* first day of week, Monday as 1, Sunday as 7 or 0
* @param minimalDaysInFirstWeek
* minimal days in first week of year
* @return number of week in year
* @see #getIsoWeekOfYear(long)
*/
public
static
int
getWeekOfYear
(
long
dateValue
,
int
firstDayOfWeek
,
int
minimalDaysInFirstWeek
)
{
long
abs
=
absoluteDayFromDateValue
(
dateValue
);
int
year
=
yearFromDateValue
(
dateValue
);
long
base
=
getWeekOfYearBase
(
year
,
firstDayOfWeek
,
minimalDaysInFirstWeek
);
if
(
abs
-
base
<
0
)
{
base
=
getWeekOfYearBase
(
year
-
1
,
firstDayOfWeek
,
minimalDaysInFirstWeek
);
}
else
if
(
monthFromDateValue
(
dateValue
)
==
12
&&
24
+
minimalDaysInFirstWeek
<
dayFromDateValue
(
dateValue
))
{
if
(
abs
>=
getWeekOfYearBase
(
year
+
1
,
firstDayOfWeek
,
minimalDaysInFirstWeek
))
{
return
1
;
}
}
return
(
int
)
((
abs
-
base
)
/
7
)
+
1
;
}
private
static
long
getWeekOfYearBase
(
int
year
,
int
firstDayOfWeek
,
int
minimalDaysInFirstWeek
)
{
long
first
=
absoluteDayFromDateValue
(
dateValue
(
year
,
1
,
1
));
int
daysInFirstWeek
=
8
-
getDayOfWeekFromAbsolute
(
first
,
firstDayOfWeek
);
long
base
=
first
+
daysInFirstWeek
;
if
(
daysInFirstWeek
>=
minimalDaysInFirstWeek
)
{
base
-=
7
;
}
return
base
;
}
/**
* Returns week year.
*
* @param dateValue
* the date value
* @param firstDayOfWeek
* first day of week, Monday as 1, Sunday as 7 or 0
* @param minimalDaysInFirstWeek
* minimal days in first week of year
* @return week year
* @see #getIsoWeekYear(long)
*/
public
static
int
getWeekYear
(
long
dateValue
,
int
firstDayOfWeek
,
int
minimalDaysInFirstWeek
)
{
long
abs
=
absoluteDayFromDateValue
(
dateValue
);
int
year
=
yearFromDateValue
(
dateValue
);
long
base
=
getWeekOfYearBase
(
year
,
firstDayOfWeek
,
minimalDaysInFirstWeek
);
if
(
abs
-
base
<
0
)
{
return
year
-
1
;
}
else
if
(
monthFromDateValue
(
dateValue
)
==
12
&&
24
+
minimalDaysInFirstWeek
<
dayFromDateValue
(
dateValue
))
{
if
(
abs
>=
getWeekOfYearBase
(
year
+
1
,
firstDayOfWeek
,
minimalDaysInFirstWeek
))
{
return
year
+
1
;
}
}
return
year
;
}
...
...
h2/src/test/org/h2/test/unit/TestDateIso8601.java
浏览文件 @
0316d856
...
...
@@ -6,11 +6,10 @@
package
org
.
h2
.
test
.
unit
;
import
static
org
.
h2
.
util
.
DateTimeUtils
.
getIsoDayOfWeek
;
import
static
org
.
h2
.
util
.
DateTimeUtils
.
getIsoWeek
;
import
static
org
.
h2
.
util
.
DateTimeUtils
.
getIsoYear
;
import
static
org
.
h2
.
util
.
DateTimeUtils
.
getIsoWeek
OfYear
;
import
static
org
.
h2
.
util
.
DateTimeUtils
.
getIso
Week
Year
;
import
org.h2.test.TestBase
;
import
org.h2.value.Value
;
import
org.h2.value.ValueDate
;
import
org.h2.value.ValueTimestamp
;
import
org.h2.value.ValueTimestampTimeZone
;
...
...
@@ -35,21 +34,21 @@ public class TestDateIso8601 extends TestBase {
TestBase
.
createCaller
().
init
().
test
();
}
private
static
Value
parse
(
String
s
)
{
private
static
long
parse
(
String
s
)
{
if
(
type
==
null
)
{
throw
new
IllegalStateException
();
}
switch
(
type
)
{
case
DATE:
return
ValueDate
.
parse
(
s
);
return
ValueDate
.
parse
(
s
)
.
getDateValue
()
;
case
TIMESTAMP:
return
ValueTimestamp
.
parse
(
s
);
return
ValueTimestamp
.
parse
(
s
)
.
getDateValue
()
;
case
TIMESTAMP_TIMEZONE_0:
return
ValueTimestampTimeZone
.
parse
(
s
+
" 00:00:00.0Z"
);
return
ValueTimestampTimeZone
.
parse
(
s
+
" 00:00:00.0Z"
)
.
getDateValue
()
;
case
TIMESTAMP_TIMEZONE_PLUS_18:
return
ValueTimestampTimeZone
.
parse
(
s
+
" 00:00:00+18:00"
);
return
ValueTimestampTimeZone
.
parse
(
s
+
" 00:00:00+18:00"
)
.
getDateValue
()
;
case
TIMESTAMP_TIMEZONE_MINUS_18:
return
ValueTimestampTimeZone
.
parse
(
s
+
" 00:00:00-18:00"
);
return
ValueTimestampTimeZone
.
parse
(
s
+
" 00:00:00-18:00"
)
.
getDateValue
()
;
default
:
throw
new
IllegalStateException
();
}
...
...
@@ -104,57 +103,57 @@ public class TestDateIso8601 extends TestBase {
* January 1st is a Monday therefore the week belongs to the next year.
*/
private
void
testIsoWeekJanuary1thMonday
()
throws
Exception
{
assertEquals
(
52
,
getIsoWeek
(
parse
(
"2006-12-31"
)));
assertEquals
(
1
,
getIsoWeek
(
parse
(
"2007-01-01"
)));
assertEquals
(
1
,
getIsoWeek
(
parse
(
"2007-01-07"
)));
assertEquals
(
2
,
getIsoWeek
(
parse
(
"2007-01-08"
)));
assertEquals
(
52
,
getIsoWeek
OfYear
(
parse
(
"2006-12-31"
)));
assertEquals
(
1
,
getIsoWeek
OfYear
(
parse
(
"2007-01-01"
)));
assertEquals
(
1
,
getIsoWeek
OfYear
(
parse
(
"2007-01-07"
)));
assertEquals
(
2
,
getIsoWeek
OfYear
(
parse
(
"2007-01-08"
)));
}
/**
* January 1st is a Tuesday therefore the week belongs to the next year.
*/
private
void
testIsoWeekJanuary1thTuesday
()
throws
Exception
{
assertEquals
(
52
,
getIsoWeek
(
parse
(
"2007-12-30"
)));
assertEquals
(
1
,
getIsoWeek
(
parse
(
"2007-12-31"
)));
assertEquals
(
1
,
getIsoWeek
(
parse
(
"2008-01-01"
)));
assertEquals
(
1
,
getIsoWeek
(
parse
(
"2008-01-06"
)));
assertEquals
(
2
,
getIsoWeek
(
parse
(
"2008-01-07"
)));
assertEquals
(
52
,
getIsoWeek
OfYear
(
parse
(
"2007-12-30"
)));
assertEquals
(
1
,
getIsoWeek
OfYear
(
parse
(
"2007-12-31"
)));
assertEquals
(
1
,
getIsoWeek
OfYear
(
parse
(
"2008-01-01"
)));
assertEquals
(
1
,
getIsoWeek
OfYear
(
parse
(
"2008-01-06"
)));
assertEquals
(
2
,
getIsoWeek
OfYear
(
parse
(
"2008-01-07"
)));
}
/**
* January1th is a Wednesday therefore the week belongs to the next year.
*/
private
void
testIsoWeekJanuary1thWednesday
()
throws
Exception
{
assertEquals
(
52
,
getIsoWeek
(
parse
(
"2002-12-28"
)));
assertEquals
(
52
,
getIsoWeek
(
parse
(
"2002-12-29"
)));
assertEquals
(
1
,
getIsoWeek
(
parse
(
"2002-12-30"
)));
assertEquals
(
1
,
getIsoWeek
(
parse
(
"2002-12-31"
)));
assertEquals
(
1
,
getIsoWeek
(
parse
(
"2003-01-01"
)));
assertEquals
(
1
,
getIsoWeek
(
parse
(
"2003-01-05"
)));
assertEquals
(
2
,
getIsoWeek
(
parse
(
"2003-01-06"
)));
assertEquals
(
52
,
getIsoWeek
OfYear
(
parse
(
"2002-12-28"
)));
assertEquals
(
52
,
getIsoWeek
OfYear
(
parse
(
"2002-12-29"
)));
assertEquals
(
1
,
getIsoWeek
OfYear
(
parse
(
"2002-12-30"
)));
assertEquals
(
1
,
getIsoWeek
OfYear
(
parse
(
"2002-12-31"
)));
assertEquals
(
1
,
getIsoWeek
OfYear
(
parse
(
"2003-01-01"
)));
assertEquals
(
1
,
getIsoWeek
OfYear
(
parse
(
"2003-01-05"
)));
assertEquals
(
2
,
getIsoWeek
OfYear
(
parse
(
"2003-01-06"
)));
}
/**
* January 1st is a Thursday therefore the week belongs to the next year.
*/
private
void
testIsoWeekJanuary1thThursday
()
throws
Exception
{
assertEquals
(
52
,
getIsoWeek
(
parse
(
"2008-12-28"
)));
assertEquals
(
1
,
getIsoWeek
(
parse
(
"2008-12-29"
)));
assertEquals
(
1
,
getIsoWeek
(
parse
(
"2008-12-30"
)));
assertEquals
(
1
,
getIsoWeek
(
parse
(
"2008-12-31"
)));
assertEquals
(
1
,
getIsoWeek
(
parse
(
"2009-01-01"
)));
assertEquals
(
1
,
getIsoWeek
(
parse
(
"2009-01-04"
)));
assertEquals
(
2
,
getIsoWeek
(
parse
(
"2009-01-09"
)));
assertEquals
(
52
,
getIsoWeek
OfYear
(
parse
(
"2008-12-28"
)));
assertEquals
(
1
,
getIsoWeek
OfYear
(
parse
(
"2008-12-29"
)));
assertEquals
(
1
,
getIsoWeek
OfYear
(
parse
(
"2008-12-30"
)));
assertEquals
(
1
,
getIsoWeek
OfYear
(
parse
(
"2008-12-31"
)));
assertEquals
(
1
,
getIsoWeek
OfYear
(
parse
(
"2009-01-01"
)));
assertEquals
(
1
,
getIsoWeek
OfYear
(
parse
(
"2009-01-04"
)));
assertEquals
(
2
,
getIsoWeek
OfYear
(
parse
(
"2009-01-09"
)));
}
/**
* January 1st is a Friday therefore the week belongs to the previous year.
*/
private
void
testIsoWeekJanuary1thFriday
()
throws
Exception
{
assertEquals
(
53
,
getIsoWeek
(
parse
(
"2009-12-31"
)));
assertEquals
(
53
,
getIsoWeek
(
parse
(
"2010-01-01"
)));
assertEquals
(
53
,
getIsoWeek
(
parse
(
"2010-01-03"
)));
assertEquals
(
1
,
getIsoWeek
(
parse
(
"2010-01-04"
)));
assertEquals
(
53
,
getIsoWeek
OfYear
(
parse
(
"2009-12-31"
)));
assertEquals
(
53
,
getIsoWeek
OfYear
(
parse
(
"2010-01-01"
)));
assertEquals
(
53
,
getIsoWeek
OfYear
(
parse
(
"2010-01-03"
)));
assertEquals
(
1
,
getIsoWeek
OfYear
(
parse
(
"2010-01-04"
)));
}
/**
...
...
@@ -162,34 +161,34 @@ public class TestDateIso8601 extends TestBase {
* year.
*/
private
void
testIsoWeekJanuary1thSaturday
()
throws
Exception
{
assertEquals
(
52
,
getIsoWeek
(
parse
(
"2010-12-31"
)));
assertEquals
(
52
,
getIsoWeek
(
parse
(
"2011-01-01"
)));
assertEquals
(
52
,
getIsoWeek
(
parse
(
"2011-01-02"
)));
assertEquals
(
1
,
getIsoWeek
(
parse
(
"2011-01-03"
)));
assertEquals
(
52
,
getIsoWeek
OfYear
(
parse
(
"2010-12-31"
)));
assertEquals
(
52
,
getIsoWeek
OfYear
(
parse
(
"2011-01-01"
)));
assertEquals
(
52
,
getIsoWeek
OfYear
(
parse
(
"2011-01-02"
)));
assertEquals
(
1
,
getIsoWeek
OfYear
(
parse
(
"2011-01-03"
)));
}
/**
* January 1st is a Sunday therefore the week belongs to the previous year.
*/
private
void
testIsoWeekJanuary1thSunday
()
throws
Exception
{
assertEquals
(
52
,
getIsoWeek
(
parse
(
"2011-12-31"
)));
assertEquals
(
52
,
getIsoWeek
(
parse
(
"2012-01-01"
)));
assertEquals
(
1
,
getIsoWeek
(
parse
(
"2012-01-02"
)));
assertEquals
(
1
,
getIsoWeek
(
parse
(
"2012-01-08"
)));
assertEquals
(
2
,
getIsoWeek
(
parse
(
"2012-01-09"
)));
assertEquals
(
52
,
getIsoWeek
OfYear
(
parse
(
"2011-12-31"
)));
assertEquals
(
52
,
getIsoWeek
OfYear
(
parse
(
"2012-01-01"
)));
assertEquals
(
1
,
getIsoWeek
OfYear
(
parse
(
"2012-01-02"
)));
assertEquals
(
1
,
getIsoWeek
OfYear
(
parse
(
"2012-01-08"
)));
assertEquals
(
2
,
getIsoWeek
OfYear
(
parse
(
"2012-01-09"
)));
}
/**
* January 1st is a Monday therefore year is equal to isoYear.
*/
private
void
testIsoYearJanuary1thMonday
()
throws
Exception
{
assertEquals
(
2006
,
getIsoYear
(
parse
(
"2006-12-28"
)));
assertEquals
(
2006
,
getIsoYear
(
parse
(
"2006-12-29"
)));
assertEquals
(
2006
,
getIsoYear
(
parse
(
"2006-12-30"
)));
assertEquals
(
2006
,
getIsoYear
(
parse
(
"2006-12-31"
)));
assertEquals
(
2007
,
getIsoYear
(
parse
(
"2007-01-01"
)));
assertEquals
(
2007
,
getIsoYear
(
parse
(
"2007-01-02"
)));
assertEquals
(
2007
,
getIsoYear
(
parse
(
"2007-01-03"
)));
assertEquals
(
2006
,
getIso
Week
Year
(
parse
(
"2006-12-28"
)));
assertEquals
(
2006
,
getIso
Week
Year
(
parse
(
"2006-12-29"
)));
assertEquals
(
2006
,
getIso
Week
Year
(
parse
(
"2006-12-30"
)));
assertEquals
(
2006
,
getIso
Week
Year
(
parse
(
"2006-12-31"
)));
assertEquals
(
2007
,
getIso
Week
Year
(
parse
(
"2007-01-01"
)));
assertEquals
(
2007
,
getIso
Week
Year
(
parse
(
"2007-01-02"
)));
assertEquals
(
2007
,
getIso
Week
Year
(
parse
(
"2007-01-03"
)));
}
/**
...
...
@@ -197,14 +196,14 @@ public class TestDateIso8601 extends TestBase {
* year.
*/
private
void
testIsoYearJanuary1thTuesday
()
throws
Exception
{
assertEquals
(
2007
,
getIsoYear
(
parse
(
"2007-12-28"
)));
assertEquals
(
2007
,
getIsoYear
(
parse
(
"2007-12-29"
)));
assertEquals
(
2007
,
getIsoYear
(
parse
(
"2007-12-30"
)));
assertEquals
(
2008
,
getIsoYear
(
parse
(
"2007-12-31"
)));
assertEquals
(
2008
,
getIsoYear
(
parse
(
"2008-01-01"
)));
assertEquals
(
2008
,
getIsoYear
(
parse
(
"2008-01-02"
)));
assertEquals
(
2008
,
getIsoYear
(
parse
(
"2008-01-03"
)));
assertEquals
(
2008
,
getIsoYear
(
parse
(
"2008-01-04"
)));
assertEquals
(
2007
,
getIso
Week
Year
(
parse
(
"2007-12-28"
)));
assertEquals
(
2007
,
getIso
Week
Year
(
parse
(
"2007-12-29"
)));
assertEquals
(
2007
,
getIso
Week
Year
(
parse
(
"2007-12-30"
)));
assertEquals
(
2008
,
getIso
Week
Year
(
parse
(
"2007-12-31"
)));
assertEquals
(
2008
,
getIso
Week
Year
(
parse
(
"2008-01-01"
)));
assertEquals
(
2008
,
getIso
Week
Year
(
parse
(
"2008-01-02"
)));
assertEquals
(
2008
,
getIso
Week
Year
(
parse
(
"2008-01-03"
)));
assertEquals
(
2008
,
getIso
Week
Year
(
parse
(
"2008-01-04"
)));
}
/**
...
...
@@ -212,13 +211,13 @@ public class TestDateIso8601 extends TestBase {
* the next year.
*/
private
void
testIsoYearJanuary1thWednesday
()
throws
Exception
{
assertEquals
(
2002
,
getIsoYear
(
parse
(
"2002-12-28"
)));
assertEquals
(
2002
,
getIsoYear
(
parse
(
"2002-12-29"
)));
assertEquals
(
2003
,
getIsoYear
(
parse
(
"2002-12-30"
)));
assertEquals
(
2003
,
getIsoYear
(
parse
(
"2002-12-31"
)));
assertEquals
(
2003
,
getIsoYear
(
parse
(
"2003-01-01"
)));
assertEquals
(
2003
,
getIsoYear
(
parse
(
"2003-01-02"
)));
assertEquals
(
2003
,
getIsoYear
(
parse
(
"2003-12-02"
)));
assertEquals
(
2002
,
getIso
Week
Year
(
parse
(
"2002-12-28"
)));
assertEquals
(
2002
,
getIso
Week
Year
(
parse
(
"2002-12-29"
)));
assertEquals
(
2003
,
getIso
Week
Year
(
parse
(
"2002-12-30"
)));
assertEquals
(
2003
,
getIso
Week
Year
(
parse
(
"2002-12-31"
)));
assertEquals
(
2003
,
getIso
Week
Year
(
parse
(
"2003-01-01"
)));
assertEquals
(
2003
,
getIso
Week
Year
(
parse
(
"2003-01-02"
)));
assertEquals
(
2003
,
getIso
Week
Year
(
parse
(
"2003-12-02"
)));
}
/**
...
...
@@ -226,14 +225,14 @@ public class TestDateIso8601 extends TestBase {
* next year.
*/
private
void
testIsoYearJanuary1thThursday
()
throws
Exception
{
assertEquals
(
2008
,
getIsoYear
(
parse
(
"2008-12-28"
)));
assertEquals
(
2009
,
getIsoYear
(
parse
(
"2008-12-29"
)));
assertEquals
(
2009
,
getIsoYear
(
parse
(
"2008-12-30"
)));
assertEquals
(
2009
,
getIsoYear
(
parse
(
"2008-12-31"
)));
assertEquals
(
2009
,
getIsoYear
(
parse
(
"2009-01-01"
)));
assertEquals
(
2009
,
getIsoYear
(
parse
(
"2009-01-02"
)));
assertEquals
(
2009
,
getIsoYear
(
parse
(
"2009-01-03"
)));
assertEquals
(
2009
,
getIsoYear
(
parse
(
"2009-01-04"
)));
assertEquals
(
2008
,
getIso
Week
Year
(
parse
(
"2008-12-28"
)));
assertEquals
(
2009
,
getIso
Week
Year
(
parse
(
"2008-12-29"
)));
assertEquals
(
2009
,
getIso
Week
Year
(
parse
(
"2008-12-30"
)));
assertEquals
(
2009
,
getIso
Week
Year
(
parse
(
"2008-12-31"
)));
assertEquals
(
2009
,
getIso
Week
Year
(
parse
(
"2009-01-01"
)));
assertEquals
(
2009
,
getIso
Week
Year
(
parse
(
"2009-01-02"
)));
assertEquals
(
2009
,
getIso
Week
Year
(
parse
(
"2009-01-03"
)));
assertEquals
(
2009
,
getIso
Week
Year
(
parse
(
"2009-01-04"
)));
}
/**
...
...
@@ -241,14 +240,14 @@ public class TestDateIso8601 extends TestBase {
* previous year.
*/
private
void
testIsoYearJanuary1thFriday
()
throws
Exception
{
assertEquals
(
2009
,
getIsoYear
(
parse
(
"2009-12-28"
)));
assertEquals
(
2009
,
getIsoYear
(
parse
(
"2009-12-29"
)));
assertEquals
(
2009
,
getIsoYear
(
parse
(
"2009-12-30"
)));
assertEquals
(
2009
,
getIsoYear
(
parse
(
"2009-12-31"
)));
assertEquals
(
2009
,
getIsoYear
(
parse
(
"2010-01-01"
)));
assertEquals
(
2009
,
getIsoYear
(
parse
(
"2010-01-02"
)));
assertEquals
(
2009
,
getIsoYear
(
parse
(
"2010-01-03"
)));
assertEquals
(
2010
,
getIsoYear
(
parse
(
"2010-01-04"
)));
assertEquals
(
2009
,
getIso
Week
Year
(
parse
(
"2009-12-28"
)));
assertEquals
(
2009
,
getIso
Week
Year
(
parse
(
"2009-12-29"
)));
assertEquals
(
2009
,
getIso
Week
Year
(
parse
(
"2009-12-30"
)));
assertEquals
(
2009
,
getIso
Week
Year
(
parse
(
"2009-12-31"
)));
assertEquals
(
2009
,
getIso
Week
Year
(
parse
(
"2010-01-01"
)));
assertEquals
(
2009
,
getIso
Week
Year
(
parse
(
"2010-01-02"
)));
assertEquals
(
2009
,
getIso
Week
Year
(
parse
(
"2010-01-03"
)));
assertEquals
(
2010
,
getIso
Week
Year
(
parse
(
"2010-01-04"
)));
}
/**
...
...
@@ -256,28 +255,28 @@ public class TestDateIso8601 extends TestBase {
* previous year.
*/
private
void
testIsoYearJanuary1thSaturday
()
throws
Exception
{
assertEquals
(
2010
,
getIsoYear
(
parse
(
"2010-12-28"
)));
assertEquals
(
2010
,
getIsoYear
(
parse
(
"2010-12-29"
)));
assertEquals
(
2010
,
getIsoYear
(
parse
(
"2010-12-30"
)));
assertEquals
(
2010
,
getIsoYear
(
parse
(
"2010-12-31"
)));
assertEquals
(
2010
,
getIsoYear
(
parse
(
"2011-01-01"
)));
assertEquals
(
2010
,
getIsoYear
(
parse
(
"2011-01-02"
)));
assertEquals
(
2011
,
getIsoYear
(
parse
(
"2011-01-03"
)));
assertEquals
(
2011
,
getIsoYear
(
parse
(
"2011-01-04"
)));
assertEquals
(
2010
,
getIso
Week
Year
(
parse
(
"2010-12-28"
)));
assertEquals
(
2010
,
getIso
Week
Year
(
parse
(
"2010-12-29"
)));
assertEquals
(
2010
,
getIso
Week
Year
(
parse
(
"2010-12-30"
)));
assertEquals
(
2010
,
getIso
Week
Year
(
parse
(
"2010-12-31"
)));
assertEquals
(
2010
,
getIso
Week
Year
(
parse
(
"2011-01-01"
)));
assertEquals
(
2010
,
getIso
Week
Year
(
parse
(
"2011-01-02"
)));
assertEquals
(
2011
,
getIso
Week
Year
(
parse
(
"2011-01-03"
)));
assertEquals
(
2011
,
getIso
Week
Year
(
parse
(
"2011-01-04"
)));
}
/**
* January 1st is a Sunday therefore this day belong to the previous year.
*/
private
void
testIsoYearJanuary1thSunday
()
throws
Exception
{
assertEquals
(
2011
,
getIsoYear
(
parse
(
"2011-12-28"
)));
assertEquals
(
2011
,
getIsoYear
(
parse
(
"2011-12-29"
)));
assertEquals
(
2011
,
getIsoYear
(
parse
(
"2011-12-30"
)));
assertEquals
(
2011
,
getIsoYear
(
parse
(
"2011-12-31"
)));
assertEquals
(
2011
,
getIsoYear
(
parse
(
"2012-01-01"
)));
assertEquals
(
2012
,
getIsoYear
(
parse
(
"2012-01-02"
)));
assertEquals
(
2012
,
getIsoYear
(
parse
(
"2012-01-03"
)));
assertEquals
(
2012
,
getIsoYear
(
parse
(
"2012-01-04"
)));
assertEquals
(
2011
,
getIso
Week
Year
(
parse
(
"2011-12-28"
)));
assertEquals
(
2011
,
getIso
Week
Year
(
parse
(
"2011-12-29"
)));
assertEquals
(
2011
,
getIso
Week
Year
(
parse
(
"2011-12-30"
)));
assertEquals
(
2011
,
getIso
Week
Year
(
parse
(
"2011-12-31"
)));
assertEquals
(
2011
,
getIso
Week
Year
(
parse
(
"2012-01-01"
)));
assertEquals
(
2012
,
getIso
Week
Year
(
parse
(
"2012-01-02"
)));
assertEquals
(
2012
,
getIso
Week
Year
(
parse
(
"2012-01-03"
)));
assertEquals
(
2012
,
getIso
Week
Year
(
parse
(
"2012-01-04"
)));
}
}
h2/src/test/org/h2/test/unit/TestDateTimeUtils.java
浏览文件 @
0316d856
...
...
@@ -5,6 +5,9 @@
*/
package
org
.
h2
.
test
.
unit
;
import
java.util.Calendar
;
import
java.util.GregorianCalendar
;
import
org.h2.test.TestBase
;
import
org.h2.util.DateTimeUtils
;
...
...
@@ -25,6 +28,8 @@ public class TestDateTimeUtils extends TestBase {
@Override
public
void
test
()
throws
Exception
{
testParseTimeNanosDB2Format
();
testDayOfWeek
();
testWeekOfYear
();
}
private
void
testParseTimeNanosDB2Format
()
{
...
...
@@ -34,4 +39,55 @@ public class TestDateTimeUtils extends TestBase {
assertEquals
(
3723000000000L
,
DateTimeUtils
.
parseTimeNanos
(
"01:02:03"
,
0
,
8
,
true
));
assertEquals
(
3723000000000L
,
DateTimeUtils
.
parseTimeNanos
(
"01.02.03"
,
0
,
8
,
true
));
}
/**
* Test for {@link DateTimeUtils#getSundayDayOfWeek()} and
* {@link DateTimeUtils#getIsoDayOfWeek(long)}.
*/
private
void
testDayOfWeek
()
{
GregorianCalendar
gc
=
DateTimeUtils
.
createGregorianCalendar
();
for
(
int
i
=
-
1_000_000
;
i
<=
1_000_000
;
i
++)
{
gc
.
clear
();
gc
.
setTimeInMillis
(
i
*
86400000L
);
int
year
=
gc
.
get
(
Calendar
.
YEAR
);
if
(
gc
.
get
(
Calendar
.
ERA
)
==
GregorianCalendar
.
BC
)
{
year
=
1
-
year
;
}
long
expectedDateValue
=
DateTimeUtils
.
dateValue
(
year
,
gc
.
get
(
Calendar
.
MONTH
)
+
1
,
gc
.
get
(
Calendar
.
DAY_OF_MONTH
));
long
dateValue
=
DateTimeUtils
.
dateValueFromAbsoluteDay
(
i
);
assertEquals
(
expectedDateValue
,
dateValue
);
assertEquals
(
i
,
DateTimeUtils
.
absoluteDayFromDateValue
(
dateValue
));
int
dow
=
gc
.
get
(
Calendar
.
DAY_OF_WEEK
);
assertEquals
(
dow
,
DateTimeUtils
.
getSundayDayOfWeek
(
dateValue
));
int
isoDow
=
(
dow
+
5
)
%
7
+
1
;
assertEquals
(
isoDow
,
DateTimeUtils
.
getIsoDayOfWeek
(
dateValue
));
assertEquals
(
gc
.
get
(
Calendar
.
WEEK_OF_YEAR
),
DateTimeUtils
.
getWeekOfYear
(
dateValue
,
gc
.
getFirstDayOfWeek
()
-
1
,
gc
.
getMinimalDaysInFirstWeek
()));
}
}
/**
* Test for {@link DateTimeUtils#getDayOfYear(long)},
* {@link DateTimeUtils#getWeekOfYear(long, int, int)} and
* {@link DateTimeUtils#getWeekYear(long, int, int)}.
*/
private
void
testWeekOfYear
()
{
GregorianCalendar
gc
=
new
GregorianCalendar
(
DateTimeUtils
.
UTC
);
for
(
int
firstDay
=
1
;
firstDay
<=
7
;
firstDay
++)
{
gc
.
setFirstDayOfWeek
(
firstDay
);
for
(
int
minimalDays
=
1
;
minimalDays
<=
7
;
minimalDays
++)
{
gc
.
setMinimalDaysInFirstWeek
(
minimalDays
);
for
(
int
i
=
0
;
i
<
150_000
;
i
++)
{
long
dateValue
=
DateTimeUtils
.
dateValueFromAbsoluteDay
(
i
);
gc
.
clear
();
gc
.
setTimeInMillis
(
i
*
86400000L
);
assertEquals
(
gc
.
get
(
Calendar
.
DAY_OF_YEAR
),
DateTimeUtils
.
getDayOfYear
(
dateValue
));
assertEquals
(
gc
.
get
(
Calendar
.
WEEK_OF_YEAR
),
DateTimeUtils
.
getWeekOfYear
(
dateValue
,
firstDay
-
1
,
minimalDays
));
assertEquals
(
gc
.
getWeekYear
(),
DateTimeUtils
.
getWeekYear
(
dateValue
,
firstDay
-
1
,
minimalDays
));
}
}
}
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论