mirror of
https://github.com/haraldk/TwelveMonkeys.git
synced 2025-08-04 03:55:28 -04:00
TM-138: DateUtil doesn't support sub-hour offset timezones.
This commit is contained in:
parent
9b71a0cba7
commit
203b330c99
@ -142,7 +142,7 @@ public final class DateUtil {
|
|||||||
* @param pTime time
|
* @param pTime time
|
||||||
* @return the time rounded to the closest second.
|
* @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;
|
return (pTime / SECOND) * SECOND;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -152,7 +152,7 @@ public final class DateUtil {
|
|||||||
* @param pTime time
|
* @param pTime time
|
||||||
* @return the time rounded to the closest minute.
|
* @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;
|
return (pTime / MINUTE) * MINUTE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -162,9 +162,20 @@ public final class DateUtil {
|
|||||||
* @param pTime time
|
* @param pTime time
|
||||||
* @return the time rounded to the closest hour.
|
* @return the time rounded to the closest hour.
|
||||||
*/
|
*/
|
||||||
public static long roundToHour(long pTime) {
|
public static long roundToHour(final long pTime) {
|
||||||
// TODO: What if timezone offset is sub hour? Are there any? I think so...
|
return roundToHour(pTime, TimeZone.getDefault());
|
||||||
return ((pTime / HOUR) * HOUR);
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
* @param pTime time
|
||||||
* @return the time rounded to the closest day.
|
* @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());
|
return roundToDay(pTime, TimeZone.getDefault());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -184,7 +195,7 @@ public final class DateUtil {
|
|||||||
* @param pTimeZone the timezone to use when rounding
|
* @param pTimeZone the timezone to use when rounding
|
||||||
* @return the time rounded to the closest day.
|
* @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);
|
int offset = pTimeZone.getOffset(pTime);
|
||||||
return (((pTime + offset) / DAY) * DAY) - offset;
|
return (((pTime + offset) / DAY) * DAY) - offset;
|
||||||
}
|
}
|
||||||
|
@ -29,11 +29,15 @@
|
|||||||
package com.twelvemonkeys.lang;
|
package com.twelvemonkeys.lang;
|
||||||
|
|
||||||
import org.junit.Test;
|
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.Calendar;
|
||||||
|
import java.util.List;
|
||||||
import java.util.TimeZone;
|
import java.util.TimeZone;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* DateUtilTest
|
* DateUtilTest
|
||||||
@ -42,9 +46,30 @@ import static org.junit.Assert.*;
|
|||||||
* @author last modified by $Author: haraldk$
|
* @author last modified by $Author: haraldk$
|
||||||
* @version $Id: DateUtilTest.java,v 1.0 11.04.12 16:21 haraldk Exp$
|
* @version $Id: DateUtilTest.java,v 1.0 11.04.12 16:21 haraldk Exp$
|
||||||
*/
|
*/
|
||||||
|
@RunWith(Parameterized.class)
|
||||||
public class DateUtilTest {
|
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);
|
calendar.setTimeInMillis(time);
|
||||||
|
|
||||||
return calendar;
|
return calendar;
|
||||||
@ -74,6 +99,15 @@ public class DateUtilTest {
|
|||||||
assertEquals(0, calendar.get(Calendar.MINUTE));
|
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
|
@Test
|
||||||
public void testRoundToDay() {
|
public void testRoundToDay() {
|
||||||
Calendar calendar = getCalendar(DateUtil.roundToDay(System.currentTimeMillis()));
|
Calendar calendar = getCalendar(DateUtil.roundToDay(System.currentTimeMillis()));
|
||||||
@ -84,6 +118,16 @@ public class DateUtilTest {
|
|||||||
assertEquals(0, calendar.get(Calendar.HOUR_OF_DAY));
|
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
|
@Test
|
||||||
public void testCurrentTimeSecond() {
|
public void testCurrentTimeSecond() {
|
||||||
Calendar calendar = getCalendar(DateUtil.currentTimeSecond());
|
Calendar calendar = getCalendar(DateUtil.currentTimeSecond());
|
||||||
@ -106,6 +150,7 @@ public class DateUtilTest {
|
|||||||
assertEquals(0, calendar.get(Calendar.MILLISECOND));
|
assertEquals(0, calendar.get(Calendar.MILLISECOND));
|
||||||
assertEquals(0, calendar.get(Calendar.SECOND));
|
assertEquals(0, calendar.get(Calendar.SECOND));
|
||||||
assertEquals(0, calendar.get(Calendar.MINUTE));
|
assertEquals(0, calendar.get(Calendar.MINUTE));
|
||||||
|
// assertEquals((TimeZone.getDefault().getOffset(calendar.getTimeInMillis()) / DateUtil.MINUTE) % 60, calendar.get(Calendar.MINUTE));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -114,6 +159,7 @@ public class DateUtilTest {
|
|||||||
|
|
||||||
assertEquals(0, calendar.get(Calendar.MILLISECOND));
|
assertEquals(0, calendar.get(Calendar.MILLISECOND));
|
||||||
assertEquals(0, calendar.get(Calendar.SECOND));
|
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.MINUTE));
|
||||||
assertEquals(0, calendar.get(Calendar.HOUR_OF_DAY));
|
assertEquals(0, calendar.get(Calendar.HOUR_OF_DAY));
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user