mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
@@ -92,7 +92,7 @@ public abstract class DatabaseUtils
|
|||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
SimpleDateFormat dateFormat = DateFormats.getBackupDateFormat();
|
SimpleDateFormat dateFormat = DateFormats.getBackupDateFormat();
|
||||||
String date = dateFormat.format(DateUtils.getLocalTime());
|
String date = dateFormat.format(DateUtils.getStartOfToday());
|
||||||
String format = "%s/Loop Habits Backup %s.db";
|
String format = "%s/Loop Habits Backup %s.db";
|
||||||
String filename = String.format(format, dir.getAbsolutePath(), date);
|
String filename = String.format(format, dir.getAbsolutePath(), date);
|
||||||
|
|
||||||
|
|||||||
@@ -23,8 +23,8 @@ import org.apache.commons.lang3.builder.*;
|
|||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
import static java.util.Calendar.DAY_OF_WEEK;
|
import static java.util.Calendar.*;
|
||||||
import static org.isoron.uhabits.core.utils.StringUtils.defaultToStringStyle;
|
import static org.isoron.uhabits.core.utils.StringUtils.*;
|
||||||
|
|
||||||
public final class Timestamp
|
public final class Timestamp
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -304,8 +304,7 @@ public class HabitCardListCache implements CommandRunner.Listener
|
|||||||
newData.copyScoresFrom(data);
|
newData.copyScoresFrom(data);
|
||||||
newData.copyCheckmarksFrom(data);
|
newData.copyCheckmarksFrom(data);
|
||||||
|
|
||||||
Timestamp dateTo = new Timestamp(
|
Timestamp dateTo = DateUtils.getToday();
|
||||||
DateUtils.getStartOfDay(DateUtils.getLocalTime()));
|
|
||||||
Timestamp dateFrom = dateTo.minus(checkmarkCount - 1);
|
Timestamp dateFrom = dateTo.minus(checkmarkCount - 1);
|
||||||
|
|
||||||
runner.publishProgress(this, -1);
|
runner.publishProgress(this, -1);
|
||||||
|
|||||||
@@ -29,14 +29,25 @@ import static java.util.Calendar.*;
|
|||||||
|
|
||||||
public abstract class DateUtils
|
public abstract class DateUtils
|
||||||
{
|
{
|
||||||
|
|
||||||
private static Long fixedLocalTime = null;
|
private static Long fixedLocalTime = null;
|
||||||
|
|
||||||
private static TimeZone fixedTimeZone = null;
|
private static TimeZone fixedTimeZone = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Time of the day when the new day starts.
|
||||||
|
*/
|
||||||
|
public static final int NEW_DAY_OFFSET = 3;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Number of milliseconds in one day.
|
* Number of milliseconds in one day.
|
||||||
*/
|
*/
|
||||||
public static long DAY_LENGTH = 24 * 60 * 60 * 1000;
|
public static final long DAY_LENGTH = 24 * 60 * 60 * 1000;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Number of milliseconds in one hour.
|
||||||
|
*/
|
||||||
|
public static final long HOUR_LENGTH = 60 * 60 * 1000;
|
||||||
|
|
||||||
public static long applyTimezone(long localTimestamp)
|
public static long applyTimezone(long localTimestamp)
|
||||||
{
|
{
|
||||||
@@ -147,12 +158,13 @@ public abstract class DateUtils
|
|||||||
|
|
||||||
public static long getStartOfToday()
|
public static long getStartOfToday()
|
||||||
{
|
{
|
||||||
return getStartOfDay(DateUtils.getLocalTime());
|
return getStartOfDay(getLocalTime() - NEW_DAY_OFFSET * HOUR_LENGTH);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static long millisecondsUntilTomorrow()
|
public static long millisecondsUntilTomorrow()
|
||||||
{
|
{
|
||||||
return getStartOfToday() + DAY_LENGTH - getLocalTime();
|
return getStartOfToday() + DAY_LENGTH -
|
||||||
|
(getLocalTime() - NEW_DAY_OFFSET * HOUR_LENGTH);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static GregorianCalendar getStartOfTodayCalendar()
|
public static GregorianCalendar getStartOfTodayCalendar()
|
||||||
|
|||||||
@@ -95,7 +95,14 @@ public class BaseUnitTest
|
|||||||
public long timestamp(int year, int month, int day)
|
public long timestamp(int year, int month, int day)
|
||||||
{
|
{
|
||||||
GregorianCalendar cal = DateUtils.getStartOfTodayCalendar();
|
GregorianCalendar cal = DateUtils.getStartOfTodayCalendar();
|
||||||
cal.set(year, month, day);
|
cal.set(year, month, day, 0, 0, 0);
|
||||||
|
return cal.getTimeInMillis();
|
||||||
|
}
|
||||||
|
|
||||||
|
public long timestamp(int year, int month, int day, int hour, int minute)
|
||||||
|
{
|
||||||
|
GregorianCalendar cal = DateUtils.getStartOfTodayCalendar();
|
||||||
|
cal.set(year, month, day, hour, minute);
|
||||||
return cal.getTimeInMillis();
|
return cal.getTimeInMillis();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ import org.junit.*;
|
|||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
|
import static java.util.Calendar.*;
|
||||||
import static junit.framework.TestCase.assertFalse;
|
import static junit.framework.TestCase.assertFalse;
|
||||||
import static org.hamcrest.MatcherAssert.assertThat;
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
import static org.hamcrest.core.IsEqual.*;
|
import static org.hamcrest.core.IsEqual.*;
|
||||||
@@ -101,23 +102,20 @@ public class RepetitionListTest extends BaseUnitTest
|
|||||||
habit = fixtures.createEmptyHabit();
|
habit = fixtures.createEmptyHabit();
|
||||||
reps = habit.getRepetitions();
|
reps = habit.getRepetitions();
|
||||||
|
|
||||||
Random random = new Random();
|
Random random = new Random(123L);
|
||||||
Integer weekdayCount[][] = new Integer[12][7];
|
Integer weekdayCount[][] = new Integer[12][7];
|
||||||
Integer monthCount[] = new Integer[12];
|
Integer monthCount[] = new Integer[12];
|
||||||
|
|
||||||
Arrays.fill(monthCount, 0);
|
Arrays.fill(monthCount, 0);
|
||||||
for (Integer row[] : weekdayCount)
|
for (Integer row[] : weekdayCount) Arrays.fill(row, 0);
|
||||||
Arrays.fill(row, 0);
|
|
||||||
|
|
||||||
GregorianCalendar day = DateUtils.getStartOfTodayCalendar();
|
GregorianCalendar day = DateUtils.getStartOfTodayCalendar();
|
||||||
|
|
||||||
// Sets the current date to the end of November
|
// Sets the current date to the end of November
|
||||||
day.set(2015, 10, 30);
|
day.set(2015, NOVEMBER, 30, 12, 0, 0);
|
||||||
DateUtils.setFixedLocalTime(day.getTimeInMillis());
|
DateUtils.setFixedLocalTime(day.getTimeInMillis());
|
||||||
|
|
||||||
// Add repetitions randomly from January to December
|
// Add repetitions randomly from January to December
|
||||||
// Leaves the month of March empty, to check that it returns null
|
day.set(2015, JANUARY, 1, 0, 0, 0);
|
||||||
day.set(2015, 0, 1);
|
|
||||||
for (int i = 0; i < 365; i++)
|
for (int i = 0; i < 365; i++)
|
||||||
{
|
{
|
||||||
if (random.nextBoolean())
|
if (random.nextBoolean())
|
||||||
@@ -125,34 +123,34 @@ public class RepetitionListTest extends BaseUnitTest
|
|||||||
int month = day.get(Calendar.MONTH);
|
int month = day.get(Calendar.MONTH);
|
||||||
int week = day.get(Calendar.DAY_OF_WEEK) % 7;
|
int week = day.get(Calendar.DAY_OF_WEEK) % 7;
|
||||||
|
|
||||||
if (month != 2)
|
// Leave the month of March empty, to check that it returns null
|
||||||
{
|
if (month == MARCH) continue;
|
||||||
if (month <= 10)
|
|
||||||
{
|
reps.toggle(new Timestamp(day));
|
||||||
weekdayCount[month][week]++;
|
|
||||||
monthCount[month]++;
|
// Repetitions in December should not be counted
|
||||||
}
|
if (month == DECEMBER) continue;
|
||||||
reps.toggle(new Timestamp(day));
|
|
||||||
}
|
weekdayCount[month][week]++;
|
||||||
|
monthCount[month]++;
|
||||||
}
|
}
|
||||||
|
|
||||||
day.add(Calendar.DAY_OF_YEAR, 1);
|
day.add(Calendar.DAY_OF_YEAR, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
HashMap<Timestamp, Integer[]> freq =
|
HashMap<Timestamp, Integer[]> freq = reps.getWeekdayFrequency();
|
||||||
reps.getWeekdayFrequency();
|
|
||||||
|
|
||||||
// Repetitions until November should be counted correctly
|
// Repetitions until November should be counted correctly
|
||||||
for (int month = 0; month < 11; month++)
|
for (int month = 0; month < 11; month++)
|
||||||
{
|
{
|
||||||
day.set(2015, month, 1);
|
day.set(2015, month, 1, 0, 0, 0);
|
||||||
Integer actualCount[] = freq.get(new Timestamp(day));
|
Integer actualCount[] = freq.get(new Timestamp(day));
|
||||||
if (monthCount[month] == 0) assertThat(actualCount, equalTo(null));
|
if (monthCount[month] == 0) assertThat(actualCount, equalTo(null));
|
||||||
else assertThat(actualCount, equalTo(weekdayCount[month]));
|
else assertThat(actualCount, equalTo(weekdayCount[month]));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Repetitions in December should be discarded
|
// Repetitions in December should be discarded
|
||||||
day.set(2015, 11, 1);
|
day.set(2015, DECEMBER, 1, 0, 0, 0);
|
||||||
assertThat(freq.get(new Timestamp(day)), equalTo(null));
|
assertThat(freq.get(new Timestamp(day)), equalTo(null));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -135,4 +135,15 @@ public class DateUtilsTest extends BaseUnitTest
|
|||||||
assertThat(DateUtils.truncate(field, t1), equalTo(expected));
|
assertThat(DateUtils.truncate(field, t1), equalTo(expected));
|
||||||
assertThat(DateUtils.truncate(field, t2), equalTo(expected));
|
assertThat(DateUtils.truncate(field, t2), equalTo(expected));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMillisecondsUntilTomorrow() throws Exception
|
||||||
|
{
|
||||||
|
DateUtils.setFixedLocalTime(timestamp(2017, JANUARY, 1, 2, 59));
|
||||||
|
assertThat(DateUtils.millisecondsUntilTomorrow(), equalTo(60000L));
|
||||||
|
|
||||||
|
DateUtils.setFixedLocalTime(timestamp(2017, JANUARY, 1, 23, 0));
|
||||||
|
assertThat(DateUtils.millisecondsUntilTomorrow(), equalTo(14400000L));
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user