From 0f009a41e1d1abfd733b48fe0c760647904d25c7 Mon Sep 17 00:00:00 2001 From: Niraja Mishra Date: Wed, 28 Feb 2018 00:09:50 +0530 Subject: [PATCH] Fixed PeriodGranularity for Asia pacific timezones (#5410) --- .../common/granularity/PeriodGranularity.java | 5 +- .../granularity/QueryGranularityTest.java | 51 +++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/java-util/src/main/java/io/druid/java/util/common/granularity/PeriodGranularity.java b/java-util/src/main/java/io/druid/java/util/common/granularity/PeriodGranularity.java index 2df9d6c98a7..419a0879cc9 100644 --- a/java-util/src/main/java/io/druid/java/util/common/granularity/PeriodGranularity.java +++ b/java-util/src/main/java/io/druid/java/util/common/granularity/PeriodGranularity.java @@ -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; } diff --git a/processing/src/test/java/io/druid/granularity/QueryGranularityTest.java b/processing/src/test/java/io/druid/granularity/QueryGranularityTest.java index af219663629..08de914641c 100644 --- a/processing/src/test/java/io/druid/granularity/QueryGranularityTest.java +++ b/processing/src/test/java/io/druid/granularity/QueryGranularityTest.java @@ -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()) + ); + } + + }