Skip to content

Commit 6b4d624

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

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-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: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import java.text.ParseException;
1010
import java.text.SimpleDateFormat;
1111

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

3133
private static final long oneDay = 86400000;
34+
private static final TimeZone UTC = TimeZone.getTimeZone("UTC");
3235

3336
private String groupPartionSize;
3437
private int intGroupPartionSize;
@@ -40,13 +43,16 @@ public void init()
4043
{
4144
try
4245
{
43-
beginDate = new SimpleDateFormat(dateFormat).parse(sBeginDate)
44-
.getTime();
46+
SimpleDateFormat sdfInit = new SimpleDateFormat(dateFormat);
47+
sdfInit.setTimeZone(UTC);
48+
beginDate = sdfInit.parse(sBeginDate).getTime();
4549
intGroupPartionSize = Integer.parseInt(groupPartionSize);
4650
formatter = new ThreadLocal<SimpleDateFormat>() {
4751
@Override
4852
protected SimpleDateFormat initialValue() {
49-
return new SimpleDateFormat(dateFormat);
53+
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
54+
sdf.setTimeZone(UTC);
55+
return sdf;
5056
}
5157
};
5258
if (intGroupPartionSize <= 0)
@@ -80,8 +86,7 @@ public Integer calculateStart(String columnValue)
8086
{
8187
try
8288
{
83-
long targetTime = new SimpleDateFormat(dateFormat).parse(
84-
columnValue).getTime();
89+
long targetTime = formatter.get().parse(columnValue).getTime();
8590
int targetPartition = (int) ((targetTime - beginDate) / partionTime);
8691
return targetPartition * intGroupPartionSize;
8792

@@ -96,8 +101,7 @@ public Integer calculateEnd(String columnValue)
96101
{
97102
try
98103
{
99-
long targetTime = new SimpleDateFormat(dateFormat).parse(
100-
columnValue).getTime();
104+
long targetTime = formatter.get().parse(columnValue).getTime();
101105
int targetPartition = (int) ((targetTime - beginDate) / partionTime);
102106
return (targetPartition+1) * intGroupPartionSize - 1;
103107

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)