Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
c81f6743
提交
c81f6743
authored
8月 14, 2018
作者:
Evgenij Ryazanov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add some helper methods to Interval class
上级
dbc68eae
隐藏空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
248 行增加
和
50 行删除
+248
-50
Interval.java
h2/src/main/org/h2/api/Interval.java
+103
-0
DateTimeFunctions.java
h2/src/main/org/h2/util/DateTimeFunctions.java
+13
-10
DateTimeUtils.java
h2/src/main/org/h2/util/DateTimeUtils.java
+81
-40
TestAll.java
h2/src/test/org/h2/test/TestAll.java
+2
-0
TestInterval.java
h2/src/test/org/h2/test/unit/TestInterval.java
+49
-0
没有找到文件。
h2/src/main/org/h2/api/Interval.java
浏览文件 @
c81f6743
...
@@ -20,6 +20,67 @@ public final class Interval {
...
@@ -20,6 +20,67 @@ public final class Interval {
private
final
long
remaining
;
private
final
long
remaining
;
/**
* @param years
* years
* @return interval
*/
public
static
Interval
ofYears
(
long
years
)
{
return
new
Interval
(
IntervalQualifier
.
YEAR
,
years
<
0
,
Math
.
abs
(
years
),
0
);
}
/**
* @param months
* months
* @return interval
*/
public
static
Interval
ofMonths
(
long
months
)
{
return
new
Interval
(
IntervalQualifier
.
MONTH
,
months
<
0
,
Math
.
abs
(
months
),
0
);
}
/**
* @param days
* days
* @return interval
*/
public
static
Interval
ofDays
(
long
days
)
{
return
new
Interval
(
IntervalQualifier
.
DAY
,
days
<
0
,
Math
.
abs
(
days
),
0
);
}
/**
* @param hours
* hours
* @return interval
*/
public
static
Interval
ofHours
(
long
hours
)
{
return
new
Interval
(
IntervalQualifier
.
HOUR
,
hours
<
0
,
Math
.
abs
(
hours
),
0
);
}
/**
* @param minutes
* minutes
* @return interval
*/
public
static
Interval
ofMinutes
(
long
minutes
)
{
return
new
Interval
(
IntervalQualifier
.
MINUTE
,
minutes
<
0
,
Math
.
abs
(
minutes
),
0
);
}
/**
* @param nanos
* nanoseconds (including seconds)
* @return interval
*/
public
static
Interval
ofNanos
(
long
nanos
)
{
boolean
negative
=
nanos
<
0
;
if
(
negative
)
{
nanos
=
-
nanos
;
if
(
nanos
<
0
)
{
throw
new
IllegalArgumentException
();
}
}
return
new
Interval
(
IntervalQualifier
.
SECOND
,
negative
,
nanos
/
1_000_000_000
,
nanos
%
1_000_000_000
);
}
/**
/**
* @param qualifier
* @param qualifier
* qualifier
* qualifier
...
@@ -83,6 +144,48 @@ public final class Interval {
...
@@ -83,6 +144,48 @@ public final class Interval {
return
remaining
;
return
remaining
;
}
}
/**
* @return years, or 0
*/
public
long
getYears
()
{
return
DateTimeUtils
.
yearsFromInterval
(
qualifier
,
negative
,
leading
,
remaining
);
}
/**
* @return months, or 0
*/
public
long
getMonths
()
{
return
DateTimeUtils
.
monthsFromInterval
(
qualifier
,
negative
,
leading
,
remaining
);
}
/**
* @return days, or 0
*/
public
long
getDays
()
{
return
DateTimeUtils
.
daysFromInterval
(
qualifier
,
negative
,
leading
,
remaining
);
}
/**
* @return hours, or 0
*/
public
long
getHours
()
{
return
DateTimeUtils
.
hoursFromInterval
(
qualifier
,
negative
,
leading
,
remaining
);
}
/**
* @return minutes, or 0
*/
public
long
getMinutes
()
{
return
DateTimeUtils
.
minutesFromInterval
(
qualifier
,
negative
,
leading
,
remaining
);
}
/**
* @return nanoseconds (including seconds), or 0
*/
public
long
getNanos
()
{
return
DateTimeUtils
.
nanosFromInterval
(
qualifier
,
negative
,
leading
,
remaining
);
}
@Override
@Override
public
int
hashCode
()
{
public
int
hashCode
()
{
final
int
prime
=
31
;
final
int
prime
=
31
;
...
...
h2/src/main/org/h2/util/DateTimeFunctions.java
浏览文件 @
c81f6743
...
@@ -35,9 +35,9 @@ import java.util.HashMap;
...
@@ -35,9 +35,9 @@ import java.util.HashMap;
import
java.util.Locale
;
import
java.util.Locale
;
import
java.util.TimeZone
;
import
java.util.TimeZone
;
import
org.h2.api.ErrorCode
;
import
org.h2.api.ErrorCode
;
import
org.h2.api.IntervalQualifier
;
import
org.h2.expression.Function
;
import
org.h2.expression.Function
;
import
org.h2.message.DbException
;
import
org.h2.message.DbException
;
import
org.h2.value.DataType
;
import
org.h2.value.Value
;
import
org.h2.value.Value
;
import
org.h2.value.ValueDate
;
import
org.h2.value.ValueDate
;
import
org.h2.value.ValueDecimal
;
import
org.h2.value.ValueDecimal
;
...
@@ -623,36 +623,39 @@ public final class DateTimeFunctions {
...
@@ -623,36 +623,39 @@ public final class DateTimeFunctions {
public
static
int
getIntDatePart
(
Value
date
,
int
field
)
{
public
static
int
getIntDatePart
(
Value
date
,
int
field
)
{
if
(
date
instanceof
ValueInterval
)
{
if
(
date
instanceof
ValueInterval
)
{
ValueInterval
interval
=
(
ValueInterval
)
date
;
ValueInterval
interval
=
(
ValueInterval
)
date
;
IntervalQualifier
qualifier
=
interval
.
getQualifier
();
boolean
negative
=
interval
.
isNegative
();
long
leading
=
interval
.
getLeading
(),
remaining
=
interval
.
getRemaining
();
long
v
;
long
v
;
switch
(
field
)
{
switch
(
field
)
{
case
YEAR:
case
YEAR:
v
=
DateTimeUtils
.
yearsFromInterval
(
interval
);
v
=
DateTimeUtils
.
yearsFromInterval
(
qualifier
,
negative
,
leading
,
remaining
);
break
;
break
;
case
MONTH:
case
MONTH:
v
=
DateTimeUtils
.
month
FromInterval
(
interval
);
v
=
DateTimeUtils
.
month
sFromInterval
(
qualifier
,
negative
,
leading
,
remaining
);
break
;
break
;
case
DAY_OF_MONTH:
case
DAY_OF_MONTH:
case
DAY_OF_WEEK:
case
DAY_OF_WEEK:
case
DAY_OF_YEAR:
case
DAY_OF_YEAR:
v
=
DateTimeUtils
.
daysFromInterval
(
interval
);
v
=
DateTimeUtils
.
daysFromInterval
(
qualifier
,
negative
,
leading
,
remaining
);
break
;
break
;
case
HOUR:
case
HOUR:
v
=
DateTimeUtils
.
hoursFromInterval
(
interval
);
v
=
DateTimeUtils
.
hoursFromInterval
(
qualifier
,
negative
,
leading
,
remaining
);
break
;
break
;
case
MINUTE:
case
MINUTE:
v
=
DateTimeUtils
.
minutesFromInterval
(
interval
);
v
=
DateTimeUtils
.
minutesFromInterval
(
qualifier
,
negative
,
leading
,
remaining
);
break
;
break
;
case
SECOND:
case
SECOND:
v
=
DateTimeUtils
.
nanosFromInterval
(
interval
)
/
1_000_000_000
;
v
=
DateTimeUtils
.
nanosFromInterval
(
qualifier
,
negative
,
leading
,
remaining
)
/
1_000_000_000
;
break
;
break
;
case
MILLISECOND:
case
MILLISECOND:
v
=
DateTimeUtils
.
nanosFromInterval
(
interval
)
/
1_000_000
%
1_000
;
v
=
DateTimeUtils
.
nanosFromInterval
(
qualifier
,
negative
,
leading
,
remaining
)
/
1_000_000
%
1_000
;
break
;
break
;
case
MICROSECOND:
case
MICROSECOND:
v
=
DateTimeUtils
.
nanosFromInterval
(
interval
)
/
1_000
%
1_000_000
;
v
=
DateTimeUtils
.
nanosFromInterval
(
qualifier
,
negative
,
leading
,
remaining
)
/
1_000
%
1_000_000
;
break
;
break
;
case
NANOSECOND:
case
NANOSECOND:
v
=
DateTimeUtils
.
nanosFromInterval
(
interval
)
%
1_000_000_000
;
v
=
DateTimeUtils
.
nanosFromInterval
(
qualifier
,
negative
,
leading
,
remaining
)
%
1_000_000_000
;
break
;
break
;
default
:
default
:
throw
DbException
.
getUnsupportedException
(
"getDatePart("
+
date
+
", "
+
field
+
')'
);
throw
DbException
.
getUnsupportedException
(
"getDatePart("
+
date
+
", "
+
field
+
')'
);
...
...
h2/src/main/org/h2/util/DateTimeUtils.java
浏览文件 @
c81f6743
...
@@ -2040,14 +2040,20 @@ public class DateTimeUtils {
...
@@ -2040,14 +2040,20 @@ public class DateTimeUtils {
}
}
/**
/**
* @param value interval
* @param qualifier
* qualifier
* @param negative
* whether interval is negative
* @param leading
* value of leading field
* @param remaining
* values of all remaining fields
* @return years, or 0
* @return years, or 0
*/
*/
public
static
long
yearsFromInterval
(
ValueInterval
value
)
{
public
static
long
yearsFromInterval
(
IntervalQualifier
qualifier
,
boolean
negative
,
long
leading
,
long
remaining
)
{
IntervalQualifier
qualifier
=
value
.
getQualifier
();
if
(
qualifier
==
IntervalQualifier
.
YEAR
||
qualifier
==
IntervalQualifier
.
YEAR_TO_MONTH
)
{
if
(
qualifier
==
IntervalQualifier
.
YEAR
||
qualifier
==
IntervalQualifier
.
YEAR_TO_MONTH
)
{
long
v
=
value
.
getLeading
()
;
long
v
=
leading
;
if
(
value
.
isNegative
()
)
{
if
(
negative
)
{
v
=
-
v
;
v
=
-
v
;
}
}
return
v
;
return
v
;
...
@@ -2057,37 +2063,50 @@ public class DateTimeUtils {
...
@@ -2057,37 +2063,50 @@ public class DateTimeUtils {
}
}
/**
/**
* @param value interval
* @param qualifier
* qualifier
* @param negative
* whether interval is negative
* @param leading
* value of leading field
* @param remaining
* values of all remaining fields
* @return months, or 0
* @return months, or 0
*/
*/
public
static
long
monthFromInterval
(
ValueInterval
value
)
{
public
static
long
monthsFromInterval
(
IntervalQualifier
qualifier
,
boolean
negative
,
long
leading
,
long
remaining
)
{
IntervalQualifier
qualifier
=
value
.
getQualifier
();
long
v
;
long
v
;
if
(
qualifier
==
IntervalQualifier
.
MONTH
)
{
if
(
qualifier
==
IntervalQualifier
.
MONTH
)
{
v
=
value
.
getLeading
()
;
v
=
leading
;
}
else
if
(
qualifier
==
IntervalQualifier
.
YEAR_TO_MONTH
){
}
else
if
(
qualifier
==
IntervalQualifier
.
YEAR_TO_MONTH
){
v
=
value
.
getRemaining
()
;
v
=
remaining
;
}
else
{
}
else
{
return
0
;
return
0
;
}
}
if
(
value
.
isNegative
()
)
{
if
(
negative
)
{
v
=
-
v
;
v
=
-
v
;
}
}
return
v
;
return
v
;
}
}
/**
/**
* @param value interval
* @param qualifier
* qualifier
* @param negative
* whether interval is negative
* @param leading
* value of leading field
* @param remaining
* values of all remaining fields
* @return months, or 0
* @return months, or 0
*/
*/
public
static
long
daysFromInterval
(
ValueInterval
value
)
{
public
static
long
daysFromInterval
(
IntervalQualifier
qualifier
,
boolean
negative
,
long
leading
,
long
remaining
)
{
switch
(
value
.
getQualifier
()
)
{
switch
(
qualifier
)
{
case
DAY:
case
DAY:
case
DAY_TO_HOUR:
case
DAY_TO_HOUR:
case
DAY_TO_MINUTE:
case
DAY_TO_MINUTE:
case
DAY_TO_SECOND:
case
DAY_TO_SECOND:
long
v
=
value
.
getLeading
()
;
long
v
=
leading
;
if
(
value
.
isNegative
()
)
{
if
(
negative
)
{
v
=
-
v
;
v
=
-
v
;
}
}
return
v
;
return
v
;
...
@@ -2097,88 +2116,110 @@ public class DateTimeUtils {
...
@@ -2097,88 +2116,110 @@ public class DateTimeUtils {
}
}
/**
/**
* @param value interval
* @param qualifier
* qualifier
* @param negative
* whether interval is negative
* @param leading
* value of leading field
* @param remaining
* values of all remaining fields
* @return hours, or 0
* @return hours, or 0
*/
*/
public
static
long
hoursFromInterval
(
ValueInterval
value
)
{
public
static
long
hoursFromInterval
(
IntervalQualifier
qualifier
,
boolean
negative
,
long
leading
,
long
remaining
)
{
long
v
;
long
v
;
switch
(
value
.
getQualifier
()
)
{
switch
(
qualifier
)
{
case
HOUR:
case
HOUR:
case
HOUR_TO_MINUTE:
case
HOUR_TO_MINUTE:
case
HOUR_TO_SECOND:
case
HOUR_TO_SECOND:
v
=
value
.
getLeading
()
;
v
=
leading
;
break
;
break
;
case
DAY_TO_HOUR:
case
DAY_TO_HOUR:
v
=
value
.
getRemaining
()
;
v
=
remaining
;
break
;
break
;
case
DAY_TO_MINUTE:
case
DAY_TO_MINUTE:
v
=
value
.
getRemaining
()
/
60
;
v
=
remaining
/
60
;
break
;
break
;
case
DAY_TO_SECOND:
case
DAY_TO_SECOND:
v
=
value
.
getRemaining
()
/
3_600_000_000_000L
;
v
=
remaining
/
3_600_000_000_000L
;
break
;
break
;
default
:
default
:
return
0
;
return
0
;
}
}
if
(
value
.
isNegative
()
)
{
if
(
negative
)
{
v
=
-
v
;
v
=
-
v
;
}
}
return
v
;
return
v
;
}
}
/**
/**
* @param value interval
* @param qualifier
* qualifier
* @param negative
* whether interval is negative
* @param leading
* value of leading field
* @param remaining
* values of all remaining fields
* @return minutes, or 0
* @return minutes, or 0
*/
*/
public
static
long
minutesFromInterval
(
ValueInterval
value
)
{
public
static
long
minutesFromInterval
(
IntervalQualifier
qualifier
,
boolean
negative
,
long
leading
,
long
remaining
)
{
long
v
;
long
v
;
switch
(
value
.
getQualifier
()
)
{
switch
(
qualifier
)
{
case
MINUTE:
case
MINUTE:
case
MINUTE_TO_SECOND:
case
MINUTE_TO_SECOND:
v
=
value
.
getLeading
()
;
v
=
leading
;
break
;
break
;
case
DAY_TO_MINUTE:
case
DAY_TO_MINUTE:
v
=
value
.
getRemaining
()
%
60
;
v
=
remaining
%
60
;
break
;
break
;
case
DAY_TO_SECOND:
case
DAY_TO_SECOND:
v
=
value
.
getRemaining
()
/
60_000_000_000L
%
60
;
v
=
remaining
/
60_000_000_000L
%
60
;
break
;
break
;
case
HOUR_TO_MINUTE:
case
HOUR_TO_MINUTE:
v
=
value
.
getRemaining
()
;
v
=
remaining
;
break
;
break
;
case
HOUR_TO_SECOND:
case
HOUR_TO_SECOND:
v
=
value
.
getRemaining
()
/
60_000_000_000L
;
v
=
remaining
/
60_000_000_000L
;
break
;
break
;
default
:
default
:
return
0
;
return
0
;
}
}
if
(
value
.
isNegative
()
)
{
if
(
negative
)
{
v
=
-
v
;
v
=
-
v
;
}
}
return
v
;
return
v
;
}
}
/**
/**
* @param value interval
* @param qualifier
* qualifier
* @param negative
* whether interval is negative
* @param leading
* value of leading field
* @param remaining
* values of all remaining fields
* @return nanoseconds, or 0
* @return nanoseconds, or 0
*/
*/
public
static
long
nanosFromInterval
(
ValueInterval
value
)
{
public
static
long
nanosFromInterval
(
IntervalQualifier
qualifier
,
boolean
negative
,
long
leading
,
long
remaining
)
{
long
v
;
long
v
;
switch
(
value
.
getQualifier
()
)
{
switch
(
qualifier
)
{
case
SECOND:
case
SECOND:
v
=
value
.
getLeading
()
*
1_000_000_000
+
value
.
getRemaining
()
;
v
=
leading
*
1_000_000_000
+
remaining
;
break
;
break
;
case
DAY_TO_SECOND:
case
DAY_TO_SECOND:
case
HOUR_TO_SECOND:
case
HOUR_TO_SECOND:
v
=
value
.
getRemaining
()
%
60_000_000_000L
;
v
=
remaining
%
60_000_000_000L
;
break
;
break
;
case
MINUTE_TO_SECOND:
case
MINUTE_TO_SECOND:
v
=
value
.
getRemaining
()
;
v
=
remaining
;
break
;
break
;
default
:
default
:
return
0
;
return
0
;
}
}
if
(
value
.
isNegative
()
)
{
if
(
negative
)
{
v
=
-
v
;
v
=
-
v
;
}
}
return
v
;
return
v
;
...
...
h2/src/test/org/h2/test/TestAll.java
浏览文件 @
c81f6743
...
@@ -190,6 +190,7 @@ import org.h2.test.unit.TestFtp;
...
@@ -190,6 +190,7 @@ import org.h2.test.unit.TestFtp;
import
org.h2.test.unit.TestIntArray
;
import
org.h2.test.unit.TestIntArray
;
import
org.h2.test.unit.TestIntIntHashMap
;
import
org.h2.test.unit.TestIntIntHashMap
;
import
org.h2.test.unit.TestIntPerfectHash
;
import
org.h2.test.unit.TestIntPerfectHash
;
import
org.h2.test.unit.TestInterval
;
import
org.h2.test.unit.TestJmx
;
import
org.h2.test.unit.TestJmx
;
import
org.h2.test.unit.TestLocale
;
import
org.h2.test.unit.TestLocale
;
import
org.h2.test.unit.TestMathUtils
;
import
org.h2.test.unit.TestMathUtils
;
...
@@ -957,6 +958,7 @@ kill -9 `jps -l | grep "org.h2.test." | cut -d " " -f 1`
...
@@ -957,6 +958,7 @@ kill -9 `jps -l | grep "org.h2.test." | cut -d " " -f 1`
addTest
(
new
TestDateIso8601
());
addTest
(
new
TestDateIso8601
());
addTest
(
new
TestFile
());
addTest
(
new
TestFile
());
addTest
(
new
TestFtp
());
addTest
(
new
TestFtp
());
addTest
(
new
TestInterval
());
addTest
(
new
TestIntArray
());
addTest
(
new
TestIntArray
());
addTest
(
new
TestIntIntHashMap
());
addTest
(
new
TestIntIntHashMap
());
addTest
(
new
TestIntPerfectHash
());
addTest
(
new
TestIntPerfectHash
());
...
...
h2/src/test/org/h2/test/unit/TestInterval.java
0 → 100644
浏览文件 @
c81f6743
/*
* Copyright 2004-2018 H2 Group. Multiple-Licensed under the MPL 2.0,
* and the EPL 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package
org
.
h2
.
test
.
unit
;
import
org.h2.api.Interval
;
import
org.h2.test.TestBase
;
/**
* Test cases for Interval.
*/
public
class
TestInterval
extends
TestBase
{
/**
* Run just this test.
*
* @param a
* ignored
*/
public
static
void
main
(
String
...
a
)
throws
Exception
{
TestBase
.
createCaller
().
init
().
test
();
}
@Override
public
void
test
()
throws
Exception
{
Interval
i
;
i
=
Interval
.
ofYears
(
100
);
assertEquals
(
100
,
i
.
getYears
());
assertEquals
(
"INTERVAL '100' YEAR"
,
i
.
toString
());
i
=
Interval
.
ofMonths
(
100
);
assertEquals
(
100
,
i
.
getMonths
());
assertEquals
(
"INTERVAL '100' MONTH"
,
i
.
toString
());
i
=
Interval
.
ofDays
(
100
);
assertEquals
(
100
,
i
.
getDays
());
assertEquals
(
"INTERVAL '100' DAY"
,
i
.
toString
());
i
=
Interval
.
ofHours
(
100
);
assertEquals
(
100
,
i
.
getHours
());
assertEquals
(
"INTERVAL '100' HOUR"
,
i
.
toString
());
i
=
Interval
.
ofMinutes
(
100
);
assertEquals
(
100
,
i
.
getMinutes
());
assertEquals
(
"INTERVAL '100' MINUTE"
,
i
.
toString
());
i
=
Interval
.
ofNanos
(
100_123_456_789L
);
assertEquals
(
100_123_456_789L
,
i
.
getNanos
());
assertEquals
(
"INTERVAL '100.123456789' SECOND"
,
i
.
toString
());
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论