Workaround for GPS week rollover (happening every 19.7 years).

Fixes #155.

https://en.wikipedia.org/wiki/GPS_Week_Number_Rollover
This commit is contained in:
Dennis Guse
2020-04-23 20:46:06 +02:00
parent 5681938a9e
commit b7b7bdf19b
3 changed files with 67 additions and 2 deletions
@@ -0,0 +1,52 @@
package de.dennisguse.opentracks.util;
import org.junit.Assert;
import org.junit.Test;
import de.dennisguse.opentracks.content.data.TrackPoint;
public class TrackPointUtilsTest {
@Test
public void fixTime_none() {
// given
long time = System.currentTimeMillis();
TrackPoint trackPoint = new TrackPoint();
trackPoint.setTime(time);
// when
TrackPointUtils.fixTime(trackPoint);
// then
Assert.assertEquals(time, trackPoint.getTime());
}
@Test
public void fixTime_0() {
// given
long time = System.currentTimeMillis();
TrackPoint trackPoint = new TrackPoint();
trackPoint.setTime(0);
// when
TrackPointUtils.fixTime(trackPoint);
// then
Assert.assertEquals(time, trackPoint.getTime(), 1000);
}
@Test
public void fixTime_gpsWeekRollover() {
// given
long time = System.currentTimeMillis();
TrackPoint trackPoint = new TrackPoint();
trackPoint.setTime(time - 1024 * UnitConversions.ONE_WEEK_MS);
// when
TrackPointUtils.fixTime(trackPoint);
// then
Assert.assertEquals(time, trackPoint.getTime());
}
}
@@ -17,13 +17,23 @@ public class TrackPointUtils {
}
/**
* Ancient fix for phones that do not set the time in {@link android.location.Location}.
* 1. Ancient fix for phones that do not set the time in {@link android.location.Location}.
* 2. Fix for GPS time rollover happening every 19.7 years: https://en.wikipedia.org/wiki/GPS_Week_Number_Rollover
*/
//TODO Necessary?
public static void fixTime(@NonNull TrackPoint trackPoint) {
if (trackPoint.getTime() == 0L) {
Log.w(TAG, "Time of provided location was 0. Using current time.");
trackPoint.setTime(System.currentTimeMillis());
return;
}
{
long timeDiff = Math.abs(trackPoint.getTime() - System.currentTimeMillis());
if (timeDiff > 1023 * UnitConversions.ONE_WEEK_MS) {
Log.w(TAG, "GPS week rollover.");
trackPoint.setTime(trackPoint.getTime() + 1024 * UnitConversions.ONE_WEEK_MS);
}
}
}
@@ -29,6 +29,9 @@ public class UnitConversions {
// 1 second in milliseconds
public static final long ONE_SECOND_MS = UnitConversions.S_TO_MS;
public static final long ONE_MINUTE_MS = (long) (UnitConversions.MIN_TO_S * UnitConversions.S_TO_MS);
public static final long ONE_HOUR_MS = 60 * ONE_MINUTE_MS;
public static final long ONE_DAY_MS = 24 * ONE_HOUR_MS;
public static final long ONE_WEEK_MS = 7 * ONE_DAY_MS;
// multiplication factor to convert milliseconds to seconds
public static final double MS_TO_S = 1d / S_TO_MS;