Automatically fix invalid timestamps, instead of crashing

pull/542/head
Alinson S. Xavier 6 years ago
parent 938739d535
commit 315bddea96

@ -30,7 +30,6 @@ import static org.isoron.uhabits.core.utils.StringUtils.*;
public final class Timestamp public final class Timestamp
{ {
public static final long DAY_LENGTH = 86400000; public static final long DAY_LENGTH = 86400000;
public static final Timestamp ZERO = new Timestamp(0); public static final Timestamp ZERO = new Timestamp(0);
@ -39,10 +38,13 @@ public final class Timestamp
public Timestamp(long unixTime) public Timestamp(long unixTime)
{ {
if (unixTime < 0 || unixTime % DAY_LENGTH != 0) if (unixTime < 0)
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Invalid unix time: " + unixTime); "Invalid unix time: " + unixTime);
if (unixTime % DAY_LENGTH != 0)
unixTime = (unixTime / DAY_LENGTH) * DAY_LENGTH;
this.unixTime = unixTime; this.unixTime = unixTime;
} }

@ -22,6 +22,7 @@ package org.isoron.uhabits.core.models;
import org.isoron.uhabits.core.*; import org.isoron.uhabits.core.*;
import org.isoron.uhabits.core.utils.*; import org.isoron.uhabits.core.utils.*;
import org.junit.*; import org.junit.*;
import org.mockito.internal.verification.*;
import static junit.framework.TestCase.assertFalse; import static junit.framework.TestCase.assertFalse;
import static org.hamcrest.MatcherAssert.*; import static org.hamcrest.MatcherAssert.*;
@ -64,5 +65,10 @@ public class TimestampTest extends BaseUnitTest
assertThat(t.daysUntil(t.minus(300)), equalTo(-300)); assertThat(t.daysUntil(t.minus(300)), equalTo(-300));
} }
@Test
public void testInexact() throws Exception
{
Timestamp t = new Timestamp(1578054764000L);
assertThat(t.getUnixTime(), equalTo(1578009600000L));
}
} }

Loading…
Cancel
Save