Skip to content

Commit bd44567

Browse files
committed
fix: specify UTC timezone in PartitionByRangeDateHash and adjust test expectations
1 parent 243539f commit bd44567

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
<dependency>
9090
<groupId>junit</groupId>
9191
<artifactId>junit</artifactId>
92-
<version>4.4</version>
92+
<version>4.13.2</version>
9393
<scope>provided</scope>
9494
</dependency>
9595
<dependency>

src/main/java/io/mycat/route/function/PartitionByRangeDateHash.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
import java.text.ParseException;
1010
import java.text.SimpleDateFormat;
1111

12+
import java.util.TimeZone;
13+
14+
import java.lang.Math;
15+
1216
/**
1317
* 先根据日期分组,再根据时间hash使得短期内数据分布的更均匀
1418
* 优点可以避免扩容时的数据迁移,又可以一定程度上避免范围分片的热点问题
@@ -29,6 +33,7 @@ public class PartitionByRangeDateHash extends AbstractPartitionAlgorithm impleme
2933
private long partionTime;
3034

3135
private static final long oneDay = 86400000;
36+
private static final TimeZone UTC = TimeZone.getTimeZone("UTC");
3237

3338
private String groupPartionSize;
3439
private int intGroupPartionSize;
@@ -40,13 +45,16 @@ public void init()
4045
{
4146
try
4247
{
43-
beginDate = new SimpleDateFormat(dateFormat).parse(sBeginDate)
44-
.getTime();
48+
SimpleDateFormat sdfInit = new SimpleDateFormat(dateFormat);
49+
sdfInit.setTimeZone(UTC);
50+
beginDate = sdfInit.parse(sBeginDate).getTime();
4551
intGroupPartionSize = Integer.parseInt(groupPartionSize);
4652
formatter = new ThreadLocal<SimpleDateFormat>() {
4753
@Override
4854
protected SimpleDateFormat initialValue() {
49-
return new SimpleDateFormat(dateFormat);
55+
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
56+
sdf.setTimeZone(UTC);
57+
return sdf;
5058
}
5159
};
5260
if (intGroupPartionSize <= 0)
@@ -80,8 +88,7 @@ public Integer calculateStart(String columnValue)
8088
{
8189
try
8290
{
83-
long targetTime = new SimpleDateFormat(dateFormat).parse(
84-
columnValue).getTime();
91+
long targetTime = formatter.get().parse(columnValue).getTime();
8592
int targetPartition = (int) ((targetTime - beginDate) / partionTime);
8693
return targetPartition * intGroupPartionSize;
8794

@@ -96,8 +103,7 @@ public Integer calculateEnd(String columnValue)
96103
{
97104
try
98105
{
99-
long targetTime = new SimpleDateFormat(dateFormat).parse(
100-
columnValue).getTime();
106+
long targetTime = formatter.get().parse(columnValue).getTime();
101107
int targetPartition = (int) ((targetTime - beginDate) / partionTime);
102108
return (targetPartition+1) * intGroupPartionSize - 1;
103109

src/test/java/io/mycat/route/function/PartitionByRangeDateHashTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,16 @@ public void test() throws ParseException {
3939
partition.init();
4040

4141
Integer calculate = partition.calculate("2014-01-01 00:00:00");
42-
Assert.assertEquals(true, 3 == calculate);
42+
Assert.assertEquals(true, 1 == calculate);
4343

4444
calculate = partition.calculate("2014-01-01 00:00:01");
45-
Assert.assertEquals(true, 1 == calculate);
45+
Assert.assertEquals(true, 0 == calculate);
4646

4747
calculate = partition.calculate("2014-01-04 00:00:00");
48-
Assert.assertEquals(true, 7 == calculate);
48+
Assert.assertEquals(true, 6 == calculate);
4949

5050
calculate = partition.calculate("2014-01-04 00:00:01");
51-
Assert.assertEquals(true, 11== calculate);
51+
Assert.assertEquals(true, 10 == calculate);
5252

5353

5454
Date beginDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2014-01-01 00:00:00");

0 commit comments

Comments
 (0)