Resolving bug when calculating the start of an UTC week

Former-commit-id: 3e6b8124b4eecb0579894a78fe40715c3d2fe1ed
This commit is contained in:
Daniel Collin 2016-02-09 16:14:39 +01:00
parent 5a90e67222
commit 54ea6d3c01

View file

@ -1,6 +1,7 @@
package se.hal.util; package se.hal.util;
import java.util.Calendar; import java.util.Calendar;
import java.util.TimeZone;
public class TimeUtility { public class TimeUtility {
public static final long SECOND_IN_MS = 1000; public static final long SECOND_IN_MS = 1000;
@ -12,10 +13,8 @@ public class TimeUtility {
public static final long INFINITY = Long.MAX_VALUE; //sort of true public static final long INFINITY = Long.MAX_VALUE; //sort of true
public static long getTimestampPeriodStart_UTC(long periodLengthInMs, long timestamp) throws NumberFormatException{ public static long getTimestampPeriodStart_UTC(long periodLengthInMs, long timestamp) throws NumberFormatException{
if(periodLengthInMs < 0 || timestamp < 0) //return timestamp - (timestamp % periodLengthInMs);
throw new NumberFormatException("argument must be positive"); return getTimestampPeriodStart(periodLengthInMs, timestamp, Calendar.getInstance(TimeZone.getTimeZone("UTC")));
return timestamp - (timestamp % periodLengthInMs);
} }
/** /**
@ -25,10 +24,13 @@ public class TimeUtility {
* @return * @return
*/ */
public static long getTimestampPeriodStart_LOCAL(long periodLengthInMs, long timestamp) throws NumberFormatException{ public static long getTimestampPeriodStart_LOCAL(long periodLengthInMs, long timestamp) throws NumberFormatException{
return getTimestampPeriodStart(periodLengthInMs, timestamp, Calendar.getInstance());
}
private static long getTimestampPeriodStart(long periodLengthInMs, long timestamp, Calendar cal) throws NumberFormatException{
if(periodLengthInMs < 0 || timestamp < 0) if(periodLengthInMs < 0 || timestamp < 0)
throw new NumberFormatException("argument must be positive"); throw new NumberFormatException("argument must be positive");
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(timestamp); cal.setTimeInMillis(timestamp);
boolean clear = false; boolean clear = false;