From a75a27ad428e7cacb42ba12c2006b3b688f61b54 Mon Sep 17 00:00:00 2001 From: Alinson Xavier Date: Tue, 25 Jul 2017 20:01:29 -0400 Subject: [PATCH] Make the day start at 3am instead of midnight Closes #50 --- .../isoron/uhabits/utils/DatabaseUtils.java | 2 +- .../isoron/uhabits/core/models/Timestamp.java | 4 +- .../habits/list/HabitCardListCache.java | 3 +- .../isoron/uhabits/core/utils/DateUtils.java | 18 +++++++-- .../org/isoron/uhabits/core/BaseUnitTest.java | 9 ++++- .../core/models/RepetitionListTest.java | 38 +++++++++---------- .../uhabits/core/utils/DateUtilsTest.java | 11 ++++++ 7 files changed, 56 insertions(+), 29 deletions(-) diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/utils/DatabaseUtils.java b/uhabits-android/src/main/java/org/isoron/uhabits/utils/DatabaseUtils.java index 8f99eb42d..e0bc5d42e 100644 --- a/uhabits-android/src/main/java/org/isoron/uhabits/utils/DatabaseUtils.java +++ b/uhabits-android/src/main/java/org/isoron/uhabits/utils/DatabaseUtils.java @@ -92,7 +92,7 @@ public abstract class DatabaseUtils throws IOException { 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 filename = String.format(format, dir.getAbsolutePath(), date); diff --git a/uhabits-core/src/main/java/org/isoron/uhabits/core/models/Timestamp.java b/uhabits-core/src/main/java/org/isoron/uhabits/core/models/Timestamp.java index 1c58c596e..99fc5051b 100644 --- a/uhabits-core/src/main/java/org/isoron/uhabits/core/models/Timestamp.java +++ b/uhabits-core/src/main/java/org/isoron/uhabits/core/models/Timestamp.java @@ -23,8 +23,8 @@ import org.apache.commons.lang3.builder.*; import java.util.*; -import static java.util.Calendar.DAY_OF_WEEK; -import static org.isoron.uhabits.core.utils.StringUtils.defaultToStringStyle; +import static java.util.Calendar.*; +import static org.isoron.uhabits.core.utils.StringUtils.*; public final class Timestamp { diff --git a/uhabits-core/src/main/java/org/isoron/uhabits/core/ui/screens/habits/list/HabitCardListCache.java b/uhabits-core/src/main/java/org/isoron/uhabits/core/ui/screens/habits/list/HabitCardListCache.java index 0b545a155..ec28965ee 100644 --- a/uhabits-core/src/main/java/org/isoron/uhabits/core/ui/screens/habits/list/HabitCardListCache.java +++ b/uhabits-core/src/main/java/org/isoron/uhabits/core/ui/screens/habits/list/HabitCardListCache.java @@ -304,8 +304,7 @@ public class HabitCardListCache implements CommandRunner.Listener newData.copyScoresFrom(data); newData.copyCheckmarksFrom(data); - Timestamp dateTo = new Timestamp( - DateUtils.getStartOfDay(DateUtils.getLocalTime())); + Timestamp dateTo = DateUtils.getToday(); Timestamp dateFrom = dateTo.minus(checkmarkCount - 1); runner.publishProgress(this, -1); diff --git a/uhabits-core/src/main/java/org/isoron/uhabits/core/utils/DateUtils.java b/uhabits-core/src/main/java/org/isoron/uhabits/core/utils/DateUtils.java index 4ab22f85c..9d2a3f476 100644 --- a/uhabits-core/src/main/java/org/isoron/uhabits/core/utils/DateUtils.java +++ b/uhabits-core/src/main/java/org/isoron/uhabits/core/utils/DateUtils.java @@ -29,14 +29,25 @@ import static java.util.Calendar.*; public abstract class DateUtils { + private static Long fixedLocalTime = 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. */ - 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) { @@ -147,12 +158,13 @@ public abstract class DateUtils public static long getStartOfToday() { - return getStartOfDay(DateUtils.getLocalTime()); + return getStartOfDay(getLocalTime() - NEW_DAY_OFFSET * HOUR_LENGTH); } public static long millisecondsUntilTomorrow() { - return getStartOfToday() + DAY_LENGTH - getLocalTime(); + return getStartOfToday() + DAY_LENGTH - + (getLocalTime() - NEW_DAY_OFFSET * HOUR_LENGTH); } public static GregorianCalendar getStartOfTodayCalendar() diff --git a/uhabits-core/src/test/java/org/isoron/uhabits/core/BaseUnitTest.java b/uhabits-core/src/test/java/org/isoron/uhabits/core/BaseUnitTest.java index b2fedc727..51c3757ce 100644 --- a/uhabits-core/src/test/java/org/isoron/uhabits/core/BaseUnitTest.java +++ b/uhabits-core/src/test/java/org/isoron/uhabits/core/BaseUnitTest.java @@ -95,7 +95,14 @@ public class BaseUnitTest public long timestamp(int year, int month, int day) { 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(); } diff --git a/uhabits-core/src/test/java/org/isoron/uhabits/core/models/RepetitionListTest.java b/uhabits-core/src/test/java/org/isoron/uhabits/core/models/RepetitionListTest.java index 42361194f..06eb153c5 100644 --- a/uhabits-core/src/test/java/org/isoron/uhabits/core/models/RepetitionListTest.java +++ b/uhabits-core/src/test/java/org/isoron/uhabits/core/models/RepetitionListTest.java @@ -27,6 +27,7 @@ import org.junit.*; import java.util.*; +import static java.util.Calendar.*; import static junit.framework.TestCase.assertFalse; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.core.IsEqual.*; @@ -101,23 +102,20 @@ public class RepetitionListTest extends BaseUnitTest habit = fixtures.createEmptyHabit(); reps = habit.getRepetitions(); - Random random = new Random(); + Random random = new Random(123L); Integer weekdayCount[][] = new Integer[12][7]; Integer monthCount[] = new Integer[12]; Arrays.fill(monthCount, 0); - for (Integer row[] : weekdayCount) - Arrays.fill(row, 0); - + for (Integer row[] : weekdayCount) Arrays.fill(row, 0); GregorianCalendar day = DateUtils.getStartOfTodayCalendar(); // 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()); // Add repetitions randomly from January to December - // Leaves the month of March empty, to check that it returns null - day.set(2015, 0, 1); + day.set(2015, JANUARY, 1, 0, 0, 0); for (int i = 0; i < 365; i++) { if (random.nextBoolean()) @@ -125,34 +123,34 @@ public class RepetitionListTest extends BaseUnitTest int month = day.get(Calendar.MONTH); int week = day.get(Calendar.DAY_OF_WEEK) % 7; - if (month != 2) - { - if (month <= 10) - { - weekdayCount[month][week]++; - monthCount[month]++; - } - reps.toggle(new Timestamp(day)); - } + // Leave the month of March empty, to check that it returns null + if (month == MARCH) continue; + + reps.toggle(new Timestamp(day)); + + // Repetitions in December should not be counted + if (month == DECEMBER) continue; + + weekdayCount[month][week]++; + monthCount[month]++; } day.add(Calendar.DAY_OF_YEAR, 1); } - HashMap freq = - reps.getWeekdayFrequency(); + HashMap freq = reps.getWeekdayFrequency(); // Repetitions until November should be counted correctly 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)); if (monthCount[month] == 0) assertThat(actualCount, equalTo(null)); else assertThat(actualCount, equalTo(weekdayCount[month])); } // 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)); } diff --git a/uhabits-core/src/test/java/org/isoron/uhabits/core/utils/DateUtilsTest.java b/uhabits-core/src/test/java/org/isoron/uhabits/core/utils/DateUtilsTest.java index 9af9c3981..57358649a 100644 --- a/uhabits-core/src/test/java/org/isoron/uhabits/core/utils/DateUtilsTest.java +++ b/uhabits-core/src/test/java/org/isoron/uhabits/core/utils/DateUtilsTest.java @@ -135,4 +135,15 @@ public class DateUtilsTest extends BaseUnitTest assertThat(DateUtils.truncate(field, t1), 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)); + + } }