Fixed PeriodGranularity for Asia pacific timezones (#5410)

This commit is contained in:
Niraja Mishra 2018-02-28 00:09:50 +05:30 committed by Fangjin Yang
parent ac5034e241
commit 0f009a41e1
2 changed files with 55 additions and 1 deletions

View File

@ -326,8 +326,11 @@ public class PeriodGranularity extends Granularity implements JsonSerializable
h -= h % hours;
long tt = chronology.hours().add(origin, h);
// always round down to the previous period (for timestamps prior to origin)
if (t < tt) {
if (t < tt && origin > 0) {
t = chronology.hours().add(tt, -hours);
} else if (t > tt && origin < 0) {
t = chronology.minuteOfHour().roundFloor(tt);
t = chronology.minuteOfHour().set(t, 0);
} else {
t = tt;
}

View File

@ -50,6 +50,7 @@ import java.net.URL;
import java.net.URLClassLoader;
import java.util.Iterator;
import java.util.List;
import java.util.TimeZone;
/**
*/
@ -811,4 +812,54 @@ public class QueryGranularityTest
Assert.assertNotNull(String.valueOf(i), Class.forName(className, true, loader));
}
}
@Test
public void testTruncateKathmandu() throws Exception
{
final DateTimeZone tz = DateTimeZone.forTimeZone(TimeZone.getTimeZone("Asia/Kathmandu"));
final DateTime date = new DateTime("2011-03-15T21:42:23.898+05:45", tz);
final PeriodGranularity year = new PeriodGranularity(new Period("P1Y"), null, tz);
final PeriodGranularity hour = new PeriodGranularity(new Period("PT1H"), null, tz);
final PeriodGranularity twoHour = new PeriodGranularity(new Period("PT2H"), null, tz);
Assert.assertEquals(
new DateTime("2011-01-01T00:00:00.000+05:45", tz),
year.toDateTime(year.bucketStart(date).getMillis())
);
Assert.assertEquals(
new DateTime("2011-03-15T21:00:00.000+05:45", tz),
hour.toDateTime(hour.bucketStart(date).getMillis())
);
Assert.assertEquals(
new DateTime("2011-03-15T20:00:00.000+05:45", tz),
twoHour.toDateTime(twoHour.bucketStart(date).getMillis())
);
}
@Test
public void testTruncateDhaka() throws Exception
{
final DateTimeZone tz = DateTimeZone.forTimeZone(TimeZone.getTimeZone("Asia/Dhaka"));
final DateTime date = new DateTime("2011-03-15T21:42:23.898+06:00", tz);
final PeriodGranularity year = new PeriodGranularity(new Period("P1Y"), null, tz);
final PeriodGranularity hour = new PeriodGranularity(new Period("PT1H"), null, tz);
final PeriodGranularity twoHour = new PeriodGranularity(new Period("PT2H"), null, tz);
Assert.assertEquals(
new DateTime("2011-01-01T00:00:00.000+06:00", tz),
year.toDateTime(year.bucketStart(date).getMillis())
);
Assert.assertEquals(
new DateTime("2011-03-15T21:00:00.000+06:00", tz),
hour.toDateTime(hour.bucketStart(date).getMillis())
);
Assert.assertEquals(
new DateTime("2011-03-15T20:00:00.000+06:00", tz),
twoHour.toDateTime(twoHour.bucketStart(date).getMillis())
);
}
}