From 203b330c9964ba23aa855d3f65cb8da687ec9ad7 Mon Sep 17 00:00:00 2001 From: Harald Kuhr Date: Wed, 27 May 2015 15:47:26 +0200 Subject: [PATCH] TM-138: DateUtil doesn't support sub-hour offset timezones. --- .../java/com/twelvemonkeys/lang/DateUtil.java | 25 ++++++--- .../com/twelvemonkeys/lang/DateUtilTest.java | 52 +++++++++++++++++-- 2 files changed, 67 insertions(+), 10 deletions(-) diff --git a/common/common-lang/src/main/java/com/twelvemonkeys/lang/DateUtil.java b/common/common-lang/src/main/java/com/twelvemonkeys/lang/DateUtil.java index 09e9f5e9..1234eb3c 100755 --- a/common/common-lang/src/main/java/com/twelvemonkeys/lang/DateUtil.java +++ b/common/common-lang/src/main/java/com/twelvemonkeys/lang/DateUtil.java @@ -142,7 +142,7 @@ public final class DateUtil { * @param pTime time * @return the time rounded to the closest second. */ - public static long roundToSecond(long pTime) { + public static long roundToSecond(final long pTime) { return (pTime / SECOND) * SECOND; } @@ -152,7 +152,7 @@ public final class DateUtil { * @param pTime time * @return the time rounded to the closest minute. */ - public static long roundToMinute(long pTime) { + public static long roundToMinute(final long pTime) { return (pTime / MINUTE) * MINUTE; } @@ -162,9 +162,20 @@ public final class DateUtil { * @param pTime time * @return the time rounded to the closest hour. */ - public static long roundToHour(long pTime) { - // TODO: What if timezone offset is sub hour? Are there any? I think so... - return ((pTime / HOUR) * HOUR); + public static long roundToHour(final long pTime) { + return roundToHour(pTime, TimeZone.getDefault()); + } + + /** + * Rounds the given time down to the closest hour, using the given timezone. + * + * @param pTime time + * @param pTimeZone the timezone to use when rounding + * @return the time rounded to the closest hour. + */ + public static long roundToHour(final long pTime, final TimeZone pTimeZone) { + int offset = pTimeZone.getOffset(pTime); + return ((pTime / HOUR) * HOUR) - offset; } /** @@ -173,7 +184,7 @@ public final class DateUtil { * @param pTime time * @return the time rounded to the closest day. */ - public static long roundToDay(long pTime) { + public static long roundToDay(final long pTime) { return roundToDay(pTime, TimeZone.getDefault()); } @@ -184,7 +195,7 @@ public final class DateUtil { * @param pTimeZone the timezone to use when rounding * @return the time rounded to the closest day. */ - public static long roundToDay(long pTime, TimeZone pTimeZone) { + public static long roundToDay(final long pTime, final TimeZone pTimeZone) { int offset = pTimeZone.getOffset(pTime); return (((pTime + offset) / DAY) * DAY) - offset; } diff --git a/common/common-lang/src/test/java/com/twelvemonkeys/lang/DateUtilTest.java b/common/common-lang/src/test/java/com/twelvemonkeys/lang/DateUtilTest.java index f6dce2ab..05f2268e 100644 --- a/common/common-lang/src/test/java/com/twelvemonkeys/lang/DateUtilTest.java +++ b/common/common-lang/src/test/java/com/twelvemonkeys/lang/DateUtilTest.java @@ -29,11 +29,15 @@ package com.twelvemonkeys.lang; import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import java.util.Arrays; import java.util.Calendar; +import java.util.List; import java.util.TimeZone; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; /** * DateUtilTest @@ -42,9 +46,30 @@ import static org.junit.Assert.*; * @author last modified by $Author: haraldk$ * @version $Id: DateUtilTest.java,v 1.0 11.04.12 16:21 haraldk Exp$ */ +@RunWith(Parameterized.class) public class DateUtilTest { - private static Calendar getCalendar(long time) { - Calendar calendar = Calendar.getInstance(TimeZone.getDefault()); + + private final TimeZone timeZone; + + @Parameterized.Parameters + public static List timeZones() { + return Arrays.asList(new Object[][] { + {TimeZone.getTimeZone("UTC")}, + {TimeZone.getTimeZone("CET")}, + {TimeZone.getTimeZone("IST")}, // 30 min off + }); + } + + public DateUtilTest(final TimeZone timeZone) { + this.timeZone = timeZone; + } + + private Calendar getCalendar(long time) { + return getCalendar(time, TimeZone.getDefault()); + } + + private Calendar getCalendar(long time, final TimeZone timeZone) { + Calendar calendar = Calendar.getInstance(timeZone); calendar.setTimeInMillis(time); return calendar; @@ -74,6 +99,15 @@ public class DateUtilTest { assertEquals(0, calendar.get(Calendar.MINUTE)); } + @Test + public void testRoundToHourTZ() { + Calendar calendar = getCalendar(DateUtil.roundToHour(System.currentTimeMillis(), timeZone), timeZone); + + assertEquals(0, calendar.get(Calendar.MILLISECOND)); + assertEquals(0, calendar.get(Calendar.SECOND)); + assertEquals(0, calendar.get(Calendar.MINUTE)); + } + @Test public void testRoundToDay() { Calendar calendar = getCalendar(DateUtil.roundToDay(System.currentTimeMillis())); @@ -84,6 +118,16 @@ public class DateUtilTest { assertEquals(0, calendar.get(Calendar.HOUR_OF_DAY)); } + @Test + public void testRoundToDayTZ() { + Calendar calendar = getCalendar(DateUtil.roundToDay(System.currentTimeMillis(), timeZone), timeZone); + + assertEquals(0, calendar.get(Calendar.MILLISECOND)); + assertEquals(0, calendar.get(Calendar.SECOND)); + assertEquals(0, calendar.get(Calendar.MINUTE)); + assertEquals(0, calendar.get(Calendar.HOUR_OF_DAY)); + } + @Test public void testCurrentTimeSecond() { Calendar calendar = getCalendar(DateUtil.currentTimeSecond()); @@ -106,6 +150,7 @@ public class DateUtilTest { assertEquals(0, calendar.get(Calendar.MILLISECOND)); assertEquals(0, calendar.get(Calendar.SECOND)); assertEquals(0, calendar.get(Calendar.MINUTE)); +// assertEquals((TimeZone.getDefault().getOffset(calendar.getTimeInMillis()) / DateUtil.MINUTE) % 60, calendar.get(Calendar.MINUTE)); } @Test @@ -114,6 +159,7 @@ public class DateUtilTest { assertEquals(0, calendar.get(Calendar.MILLISECOND)); assertEquals(0, calendar.get(Calendar.SECOND)); +// assertEquals((TimeZone.getDefault().getOffset(calendar.getTimeInMillis()) / DateUtil.MINUTE) % 60, calendar.get(Calendar.MINUTE)); assertEquals(0, calendar.get(Calendar.MINUTE)); assertEquals(0, calendar.get(Calendar.HOUR_OF_DAY)); }