TM-138: DateUtil doesn't support sub-hour offset timezones.

This commit is contained in:
Harald Kuhr 2015-05-27 15:47:26 +02:00
parent 9b71a0cba7
commit 203b330c99
2 changed files with 67 additions and 10 deletions

View File

@ -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;
}

View File

@ -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<Object[]> 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));
}