Setting custom start of the day (#621)

pull/655/head
Kristian Tashkov 5 years ago committed by GitHub
parent 720f98f9bd
commit d997b1378d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -92,6 +92,7 @@ public class BaseAndroidTest extends TestCase
testContext = InstrumentationRegistry.getInstrumentation().getContext(); testContext = InstrumentationRegistry.getInstrumentation().getContext();
DateUtils.setFixedLocalTime(FIXED_LOCAL_TIME); DateUtils.setFixedLocalTime(FIXED_LOCAL_TIME);
DateUtils.setStartDayOffset(0, 0);
setResolution(2.0f); setResolution(2.0f);
setTheme(R.style.AppBaseTheme); setTheme(R.style.AppBaseTheme);
setLocale("en", "US"); setLocale("en", "US");

@ -25,6 +25,7 @@ import org.isoron.androidbase.*
import org.isoron.uhabits.core.database.* import org.isoron.uhabits.core.database.*
import org.isoron.uhabits.core.reminders.* import org.isoron.uhabits.core.reminders.*
import org.isoron.uhabits.core.ui.* import org.isoron.uhabits.core.ui.*
import org.isoron.uhabits.core.utils.*
import org.isoron.uhabits.utils.* import org.isoron.uhabits.utils.*
import org.isoron.uhabits.widgets.* import org.isoron.uhabits.widgets.*
import java.io.* import java.io.*
@ -59,6 +60,7 @@ class HabitsApplication : Application() {
db.renameTo(File(db.absolutePath + ".invalid")) db.renameTo(File(db.absolutePath + ".invalid"))
DatabaseUtils.initializeDatabase(context) DatabaseUtils.initializeDatabase(context)
} }
DateUtils.setStartDayOffset(3, 0)
widgetUpdater = component.widgetUpdater widgetUpdater = component.widgetUpdater
widgetUpdater.startListening() widgetUpdater.startListening()

@ -396,7 +396,7 @@ public class BarChart extends ScrollableChart
private float getMaxDayWidth() private float getMaxDayWidth()
{ {
float maxDayWidth = 0; float maxDayWidth = 0;
GregorianCalendar day = DateUtils.getStartOfTodayCalendar(); GregorianCalendar day = DateUtils.getStartOfTodayCalendarWithOffset();
for (int i = 0; i < 28; i++) for (int i = 0; i < 28; i++)
{ {
@ -411,7 +411,7 @@ public class BarChart extends ScrollableChart
private float getMaxMonthWidth() private float getMaxMonthWidth()
{ {
float maxMonthWidth = 0; float maxMonthWidth = 0;
GregorianCalendar day = DateUtils.getStartOfTodayCalendar(); GregorianCalendar day = DateUtils.getStartOfTodayCalendarWithOffset();
for (int i = 0; i < 12; i++) for (int i = 0; i < 12; i++)
{ {

@ -153,7 +153,7 @@ public class FrequencyChart extends ScrollableChart
pGraph.setColor(primaryColor); pGraph.setColor(primaryColor);
prevRect.setEmpty(); prevRect.setEmpty();
GregorianCalendar currentDate = DateUtils.getStartOfTodayCalendar(); GregorianCalendar currentDate = DateUtils.getStartOfTodayCalendarWithOffset();
currentDate.set(Calendar.DAY_OF_MONTH, 1); currentDate.set(Calendar.DAY_OF_MONTH, 1);
currentDate.add(Calendar.MONTH, -nColumns + 2 - getDataOffset()); currentDate.add(Calendar.MONTH, -nColumns + 2 - getDataOffset());
@ -274,7 +274,7 @@ public class FrequencyChart extends ScrollableChart
private float getMaxMonthWidth() private float getMaxMonthWidth()
{ {
float maxMonthWidth = 0; float maxMonthWidth = 0;
GregorianCalendar day = DateUtils.getStartOfTodayCalendar(); GregorianCalendar day = DateUtils.getStartOfTodayCalendarWithOffset();
for (int i = 0; i < 12; i++) for (int i = 0; i < 12; i++)
{ {

@ -148,7 +148,7 @@ public class HistoryChart extends ScrollableChart
final Timestamp timestamp = positionToTimestamp(x, y); final Timestamp timestamp = positionToTimestamp(x, y);
if (timestamp == null) return false; if (timestamp == null) return false;
Timestamp today = DateUtils.getToday(); Timestamp today = DateUtils.getTodayWithOffset();
int newValue = YES_MANUAL; int newValue = YES_MANUAL;
int offset = timestamp.daysUntil(today); int offset = timestamp.daysUntil(today);
if (offset < checkmarks.length) if (offset < checkmarks.length)
@ -522,20 +522,20 @@ public class HistoryChart extends ScrollableChart
Calendar date = (Calendar) baseDate.clone(); Calendar date = (Calendar) baseDate.clone();
date.add(Calendar.DAY_OF_YEAR, offset); date.add(Calendar.DAY_OF_YEAR, offset);
if (DateUtils.getStartOfDay(date.getTimeInMillis()) > if (DateUtils.getStartOfDayWithOffset(date.getTimeInMillis()) >
DateUtils.getStartOfToday()) return null; DateUtils.getStartOfTodayWithOffset()) return null;
return new Timestamp(date.getTimeInMillis()); return new Timestamp(date.getTimeInMillis());
} }
private void updateDate() private void updateDate()
{ {
baseDate = DateUtils.getStartOfTodayCalendar(); baseDate = DateUtils.getStartOfTodayCalendarWithOffset();
baseDate.add(Calendar.DAY_OF_YEAR, -(getDataOffset() - 1) * 7); baseDate.add(Calendar.DAY_OF_YEAR, -(getDataOffset() - 1) * 7);
nDays = (nColumns - 1) * 7; nDays = (nColumns - 1) * 7;
int realWeekday = int realWeekday =
DateUtils.getStartOfTodayCalendar().get(Calendar.DAY_OF_WEEK); DateUtils.getStartOfTodayCalendarWithOffset().get(Calendar.DAY_OF_WEEK);
todayPositionInColumn = todayPositionInColumn =
(7 + realWeekday - firstWeekday) % 7; (7 + realWeekday - firstWeekday) % 7;

@ -357,7 +357,7 @@ public class ScoreChart extends ScrollableChart
private float getMaxDayWidth() private float getMaxDayWidth()
{ {
float maxDayWidth = 0; float maxDayWidth = 0;
GregorianCalendar day = DateUtils.getStartOfTodayCalendar(); GregorianCalendar day = DateUtils.getStartOfTodayCalendarWithOffset();
for (int i = 0; i < 28; i++) for (int i = 0; i < 28; i++)
{ {
@ -372,7 +372,7 @@ public class ScoreChart extends ScrollableChart
private float getMaxMonthWidth() private float getMaxMonthWidth()
{ {
float maxMonthWidth = 0; float maxMonthWidth = 0;
GregorianCalendar day = DateUtils.getStartOfTodayCalendar(); GregorianCalendar day = DateUtils.getStartOfTodayCalendarWithOffset();
for (int i = 0; i < 12; i++) for (int i = 0; i < 12; i++)
{ {

@ -56,7 +56,7 @@ class CheckmarkPanelView(
@Synchronized @Synchronized
override fun setupButtons() { override fun setupButtons() {
val today = DateUtils.getToday() val today = DateUtils.getTodayWithOffset()
buttons.forEachIndexed { index, button -> buttons.forEachIndexed { index, button ->
val timestamp = today.minus(index + dataOffset) val timestamp = today.minus(index + dataOffset)

@ -172,7 +172,7 @@ class HabitCardView(
} }
fun triggerRipple(timestamp: Timestamp) { fun triggerRipple(timestamp: Timestamp) {
val today = DateUtils.getToday() val today = DateUtils.getTodayWithOffset()
val offset = timestamp.daysUntil(today) - dataOffset val offset = timestamp.daysUntil(today) - dataOffset
val button = checkmarkPanel.buttons[offset] val button = checkmarkPanel.buttons[offset]
val y = button.height / 2.0f val y = button.height / 2.0f

@ -106,7 +106,7 @@ class HeaderView(
} }
fun draw(canvas: Canvas) { fun draw(canvas: Canvas) {
val day = getStartOfTodayCalendar() val day = getStartOfTodayCalendarWithOffset()
val width = dim(R.dimen.checkmarkWidth) val width = dim(R.dimen.checkmarkWidth)
val height = dim(R.dimen.checkmarkHeight) val height = dim(R.dimen.checkmarkHeight)
val isReversed = prefs.isCheckmarkSequenceReversed val isReversed = prefs.isCheckmarkSequenceReversed

@ -67,7 +67,7 @@ class NumberPanelView(
@Synchronized @Synchronized
override fun setupButtons() { override fun setupButtons() {
val today = DateUtils.getToday() val today = DateUtils.getTodayWithOffset()
buttons.forEachIndexed { index, button -> buttons.forEachIndexed { index, button ->
val timestamp = today.minus(index + dataOffset) val timestamp = today.minus(index + dataOffset)

@ -153,7 +153,7 @@ public class OverviewCard extends HabitCard
ScoreList scores = habit.getScores(); ScoreList scores = habit.getScores();
Timestamp today = DateUtils.getToday(); Timestamp today = DateUtils.getTodayWithOffset();
Timestamp lastMonth = today.minus(30); Timestamp lastMonth = today.minus(30);
Timestamp lastYear = today.minus(365); Timestamp lastYear = today.minus(365);

@ -134,7 +134,7 @@ public class TargetCard extends HabitCard
public void onPostExecute() public void onPostExecute()
{ {
if (isCanceled()) return; if (isCanceled()) return;
Calendar cal = DateUtils.getStartOfTodayCalendar(); Calendar cal = DateUtils.getStartOfTodayCalendarWithOffset();
int daysInMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH); int daysInMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
int daysInQuarter = 91; int daysInQuarter = 91;
int daysInYear = cal.getActualMaximum(Calendar.DAY_OF_YEAR); int daysInYear = cal.getActualMaximum(Calendar.DAY_OF_YEAR);

@ -48,7 +48,7 @@ class FireSettingReceiver : BroadcastReceiver() {
.build() .build()
allHabits = app.component.habitList allHabits = app.component.habitList
val args = SettingUtils.parseIntent(intent, allHabits) ?: return val args = SettingUtils.parseIntent(intent, allHabits) ?: return
val timestamp = DateUtils.getToday() val timestamp = DateUtils.getTodayWithOffset()
val controller = component.widgetController val controller = component.widgetController
when (args.action) { when (args.action) {

@ -38,7 +38,7 @@ class IntentParser
fun copyIntentData(source: Intent, destination: Intent) { fun copyIntentData(source: Intent, destination: Intent) {
destination.data = source.data; destination.data = source.data;
destination.putExtra("timestamp", source.getLongExtra("timestamp", DateUtils.getToday().unixTime)) destination.putExtra("timestamp", source.getLongExtra("timestamp", DateUtils.getTodayWithOffset().unixTime))
} }
private fun parseHabit(uri: Uri): Habit { private fun parseHabit(uri: Uri): Habit {
@ -48,7 +48,7 @@ class IntentParser
} }
private fun parseTimestamp(intent: Intent): Timestamp { private fun parseTimestamp(intent: Intent): Timestamp {
val today = DateUtils.getToday().unixTime val today = DateUtils.getTodayWithOffset().unixTime
var timestamp = intent.getLongExtra("timestamp", today) var timestamp = intent.getLongExtra("timestamp", today)
timestamp = DateUtils.getStartOfDay(timestamp) timestamp = DateUtils.getStartOfDay(timestamp)

@ -65,7 +65,7 @@ public class ReminderReceiver extends BroadcastReceiver
Log.i(TAG, String.format("Received intent: %s", intent.toString())); Log.i(TAG, String.format("Received intent: %s", intent.toString()));
Habit habit = null; Habit habit = null;
long today = DateUtils.getStartOfToday(); long today = DateUtils.getStartOfTodayWithOffset();
if (intent.getData() != null) if (intent.getData() != null)
habit = habits.getById(parseId(intent.getData())); habit = habits.getById(parseId(intent.getData()));

@ -64,7 +64,7 @@ class WidgetUpdater
} }
fun scheduleStartDayWidgetUpdate() { fun scheduleStartDayWidgetUpdate() {
val timestamp = DateUtils.getStartOfTomorrow() val timestamp = DateUtils.getStartOfTomorrowWithOffset()
intentScheduler.scheduleWidgetUpdate(timestamp); intentScheduler.scheduleWidgetUpdate(timestamp);
} }

@ -47,7 +47,7 @@ public class BaseAndroidJVMTest
{ {
long fixed_local_time = 1422172800000L; long fixed_local_time = 1422172800000L;
DateUtils.setFixedLocalTime(fixed_local_time); DateUtils.setFixedLocalTime(fixed_local_time);
DateUtils.setStartDayOffset(0, 0);
modelFactory = new MemoryModelFactory(); modelFactory = new MemoryModelFactory();
habitList = spy(modelFactory.buildHabitList()); habitList = spy(modelFactory.buildHabitList());
fixtures = new HabitFixtures(modelFactory, habitList); fixtures = new HabitFixtures(modelFactory, habitList);
@ -59,6 +59,7 @@ public class BaseAndroidJVMTest
public void tearDown() public void tearDown()
{ {
DateUtils.setFixedLocalTime(null); DateUtils.setFixedLocalTime(null);
DateUtils.setStartDayOffset(0, 0);
} }
public long timestamp(int year, int month, int day) public long timestamp(int year, int month, int day)

@ -55,7 +55,7 @@ public abstract class CheckmarkList
{ {
if (reps.length == 0) throw new IllegalArgumentException(); if (reps.length == 0) throw new IllegalArgumentException();
Timestamp today = DateUtils.getToday(); Timestamp today = DateUtils.getTodayWithOffset();
Timestamp begin = reps[0].getTimestamp(); Timestamp begin = reps[0].getTimestamp();
if (intervals.size() > 0) begin = Timestamp.oldest(begin, intervals.get(0).begin); if (intervals.size() > 0) begin = Timestamp.oldest(begin, intervals.get(0).begin);
@ -181,7 +181,7 @@ public abstract class CheckmarkList
if (oldestRep == null) return new int[0]; if (oldestRep == null) return new int[0];
Timestamp fromTimestamp = oldestRep.getTimestamp(); Timestamp fromTimestamp = oldestRep.getTimestamp();
Timestamp toTimestamp = DateUtils.getToday(); Timestamp toTimestamp = DateUtils.getTodayWithOffset();
return getValues(fromTimestamp, toTimestamp); return getValues(fromTimestamp, toTimestamp);
} }
@ -211,7 +211,7 @@ public abstract class CheckmarkList
public synchronized final Checkmark getToday() public synchronized final Checkmark getToday()
{ {
compute(); compute();
Timestamp today = DateUtils.getToday(); Timestamp today = DateUtils.getTodayWithOffset();
return getByInterval(today, today).get(0); return getByInterval(today, today).get(0);
} }
@ -325,7 +325,7 @@ public abstract class CheckmarkList
*/ */
protected final synchronized void compute() protected final synchronized void compute()
{ {
final Timestamp today = DateUtils.getToday(); final Timestamp today = DateUtils.getTodayWithOffset();
Checkmark newest = getNewestComputed(); Checkmark newest = getNewestComputed();
if (newest != null && newest.getTimestamp().equals(today)) return; if (newest != null && newest.getTimestamp().equals(today)) return;
@ -335,6 +335,8 @@ public abstract class CheckmarkList
if (oldestRep == null) return; if (oldestRep == null) return;
final Timestamp from = oldestRep.getTimestamp(); final Timestamp from = oldestRep.getTimestamp();
if (from.isNewerThan(today)) return;
Repetition reps[] = habit Repetition reps[] = habit
.getRepetitions() .getRepetitions()
.getByInterval(from, today) .getByInterval(from, today)
@ -364,7 +366,7 @@ public abstract class CheckmarkList
{ {
if (reps.length == 0) return; if (reps.length == 0) return;
Timestamp today = DateUtils.getToday(); Timestamp today = DateUtils.getTodayWithOffset();
Timestamp begin = reps[0].getTimestamp(); Timestamp begin = reps[0].getTimestamp();
int nDays = begin.daysUntil(today) + 1; int nDays = begin.daysUntil(today) + 1;
@ -392,7 +394,7 @@ public abstract class CheckmarkList
public List<Checkmark> getAll() { public List<Checkmark> getAll() {
Repetition oldest = habit.getRepetitions().getOldest(); Repetition oldest = habit.getRepetitions().getOldest();
if(oldest == null) return new ArrayList<>(); if(oldest == null) return new ArrayList<>();
return getByInterval(oldest.getTimestamp(), DateUtils.getToday()); return getByInterval(oldest.getTimestamp(), DateUtils.getTodayWithOffset());
} }
static final class Interval static final class Interval

@ -135,7 +135,7 @@ public abstract class RepetitionList
public HashMap<Timestamp, Integer[]> getWeekdayFrequency() public HashMap<Timestamp, Integer[]> getWeekdayFrequency()
{ {
List<Repetition> reps = List<Repetition> reps =
getByInterval(Timestamp.ZERO, DateUtils.getToday()); getByInterval(Timestamp.ZERO, DateUtils.getTodayWithOffset());
HashMap<Timestamp, Integer[]> map = new HashMap<>(); HashMap<Timestamp, Integer[]> map = new HashMap<>();
for (Repetition r : reps) for (Repetition r : reps)

@ -71,7 +71,7 @@ public abstract class ScoreList implements Iterable<Score>
*/ */
public double getTodayValue() public double getTodayValue()
{ {
return getValue(DateUtils.getToday()); return getValue(DateUtils.getTodayWithOffset());
} }
/** /**
@ -224,7 +224,7 @@ public abstract class ScoreList implements Iterable<Score>
Repetition oldestRep = habit.getRepetitions().getOldest(); Repetition oldestRep = habit.getRepetitions().getOldest();
if (oldestRep == null) return; if (oldestRep == null) return;
Timestamp today = DateUtils.getToday(); Timestamp today = DateUtils.getTodayWithOffset();
compute(oldestRep.getTimestamp(), today); compute(oldestRep.getTimestamp(), today);
} }

@ -67,7 +67,7 @@ public abstract class StreakList
public synchronized void rebuild() public synchronized void rebuild()
{ {
Timestamp today = DateUtils.getToday(); Timestamp today = DateUtils.getTodayWithOffset();
Timestamp beginning = findBeginning(); Timestamp beginning = findBeginning();
if (beginning == null || beginning.isNewerThan(today)) return; if (beginning == null || beginning.isNewerThan(today)) return;

@ -121,7 +121,7 @@ public class ReminderScheduler implements CommandRunner.Listener
return; return;
} }
long timestamp = getStartOfDay(removeTimezone(reminderTime)); long timestamp = getStartOfDayWithOffset(removeTimezone(reminderTime));
sys.log("ReminderScheduler", sys.log("ReminderScheduler",
String.format( String.format(
Locale.US, Locale.US,

@ -332,7 +332,7 @@ public class HabitCardListCache implements CommandRunner.Listener
newData.copyScoresFrom(data); newData.copyScoresFrom(data);
newData.copyCheckmarksFrom(data); newData.copyCheckmarksFrom(data);
Timestamp dateTo = DateUtils.getToday(); Timestamp dateTo = DateUtils.getTodayWithOffset();
Timestamp dateFrom = dateTo.minus(checkmarkCount - 1); Timestamp dateFrom = dateTo.minus(checkmarkCount - 1);
if (runner != null) runner.publishProgress(this, -1); if (runner != null) runner.publishProgress(this, -1);

@ -37,10 +37,25 @@ public abstract class DateUtils
private static Locale fixedLocale = null; private static Locale fixedLocale = null;
private static int startDayHourOffset = 0;
private static int startDayMinuteOffset = 0;
/**
* Number of milliseconds in one minute.
*/
public static final long MINUTE_LENGTH = 60 * 1000;
/**
* Number of milliseconds in one hour.
*/
public static final long HOUR_LENGTH = 60 * MINUTE_LENGTH;
/** /**
* Number of milliseconds in one day. * Number of milliseconds in one day.
*/ */
public static final long DAY_LENGTH = 24 * 60 * 60 * 1000; public static final long DAY_LENGTH = 24 * HOUR_LENGTH;
public static long applyTimezone(long localTimestamp) public static long applyTimezone(long localTimestamp)
{ {
@ -165,24 +180,41 @@ public abstract class DateUtils
return new Timestamp(getStartOfToday()); return new Timestamp(getStartOfToday());
} }
@NonNull
public static Timestamp getTodayWithOffset()
{
return new Timestamp(getStartOfTodayWithOffset());
}
public static long getStartOfDay(long timestamp) public static long getStartOfDay(long timestamp)
{ {
return (timestamp / DAY_LENGTH) * DAY_LENGTH; return (timestamp / DAY_LENGTH) * DAY_LENGTH;
} }
public static long getStartOfDayWithOffset(long timestamp)
{
long offset = startDayHourOffset * HOUR_LENGTH + startDayMinuteOffset * MINUTE_LENGTH;
return getStartOfDay(timestamp - offset);
}
public static long getStartOfToday() public static long getStartOfToday()
{ {
return getStartOfDay(getLocalTime()); return getStartOfDay(getLocalTime());
} }
public static long getStartOfTomorrow() public static long getStartOfTomorrowWithOffset()
{
return getUpcomingTimeInMillis(startDayHourOffset, startDayMinuteOffset);
}
public static long getStartOfTodayWithOffset()
{ {
return getUpcomingTimeInMillis(0, 0); return getStartOfDayWithOffset(getLocalTime());
} }
public static long millisecondsUntilTomorrow() public static long millisecondsUntilTomorrowWithOffset()
{ {
return getStartOfTomorrow() - getLocalTime(); return getStartOfTomorrowWithOffset() - getLocalTime();
} }
public static GregorianCalendar getStartOfTodayCalendar() public static GregorianCalendar getStartOfTodayCalendar()
@ -190,6 +222,11 @@ public abstract class DateUtils
return getCalendar(getStartOfToday()); return getCalendar(getStartOfToday());
} }
public static GregorianCalendar getStartOfTodayCalendarWithOffset()
{
return getCalendar(getStartOfTodayWithOffset());
}
private static TimeZone getTimezone() private static TimeZone getTimezone()
{ {
if(fixedTimeZone != null) return fixedTimeZone; if(fixedTimeZone != null) return fixedTimeZone;
@ -217,6 +254,12 @@ public abstract class DateUtils
fixedLocale = locale; fixedLocale = locale;
} }
public static void setStartDayOffset(int hourOffset, int minuteOffset)
{
startDayHourOffset = hourOffset;
startDayMinuteOffset = minuteOffset;
}
private static Locale getLocale() private static Locale getLocale()
{ {
if(fixedLocale != null) return fixedLocale; if(fixedLocale != null) return fixedLocale;

@ -56,7 +56,7 @@ public class MidnightTimer
{ {
executor = Executors.newSingleThreadScheduledExecutor(); executor = Executors.newSingleThreadScheduledExecutor();
executor.scheduleAtFixedRate(() -> notifyListeners(), executor.scheduleAtFixedRate(() -> notifyListeners(),
DateUtils.millisecondsUntilTomorrow() + 1000, DateUtils.millisecondsUntilTomorrowWithOffset() + 1000,
DateUtils.DAY_LENGTH, TimeUnit.MILLISECONDS); DateUtils.DAY_LENGTH, TimeUnit.MILLISECONDS);
} }

@ -78,6 +78,7 @@ public class BaseUnitTest
public void setUp() throws Exception public void setUp() throws Exception
{ {
DateUtils.setFixedLocalTime(FIXED_LOCAL_TIME); DateUtils.setFixedLocalTime(FIXED_LOCAL_TIME);
DateUtils.setStartDayOffset(0, 0);
modelFactory = new MemoryModelFactory(); modelFactory = new MemoryModelFactory();
habitList = spy(modelFactory.buildHabitList()); habitList = spy(modelFactory.buildHabitList());
@ -91,6 +92,7 @@ public class BaseUnitTest
{ {
validateMockitoUsage(); validateMockitoUsage();
DateUtils.setFixedLocalTime(null); DateUtils.setFixedLocalTime(null);
DateUtils.setStartDayOffset(0, 0);
} }
public long unixTime(int year, int month, int day) public long unixTime(int year, int month, int day)

@ -29,8 +29,7 @@ import static java.util.Calendar.*;
import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertEquals;
import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.*; import static org.hamcrest.MatcherAssert.*;
import static org.isoron.uhabits.core.utils.DateUtils.applyTimezone; import static org.isoron.uhabits.core.utils.DateUtils.*;
import static org.isoron.uhabits.core.utils.DateUtils.removeTimezone;
public class DateUtilsTest extends BaseUnitTest public class DateUtilsTest extends BaseUnitTest
{ {
@ -165,12 +164,42 @@ public class DateUtilsTest extends BaseUnitTest
public void testMillisecondsUntilTomorrow() throws Exception public void testMillisecondsUntilTomorrow() throws Exception
{ {
DateUtils.setFixedTimeZone(TimeZone.getTimeZone("GMT")); DateUtils.setFixedTimeZone(TimeZone.getTimeZone("GMT"));
DateUtils.setFixedLocalTime(unixTime(2017, JANUARY, 1, 23, 59)); DateUtils.setFixedLocalTime(unixTime(2017, JANUARY, 1, 23, 59));
assertThat(DateUtils.millisecondsUntilTomorrow(), equalTo(60000L)); assertThat(DateUtils.millisecondsUntilTomorrowWithOffset(), equalTo(MINUTE_LENGTH));
DateUtils.setFixedLocalTime(unixTime(2017, JANUARY, 1, 20, 0)); DateUtils.setFixedLocalTime(unixTime(2017, JANUARY, 1, 20, 0));
assertThat(DateUtils.millisecondsUntilTomorrow(), equalTo(14400000L)); assertThat(DateUtils.millisecondsUntilTomorrowWithOffset(), equalTo(4 * HOUR_LENGTH));
DateUtils.setStartDayOffset(3, 30);
DateUtils.setFixedLocalTime(unixTime(2017, JANUARY, 1, 23, 59));
assertThat(DateUtils.millisecondsUntilTomorrowWithOffset(), equalTo(3 * HOUR_LENGTH + 31 * MINUTE_LENGTH));
DateUtils.setFixedLocalTime(unixTime(2017, JANUARY, 2, 1, 0));
assertThat(DateUtils.millisecondsUntilTomorrowWithOffset(), equalTo(2 * HOUR_LENGTH + 30 * MINUTE_LENGTH));
}
@Test
public void testGetTodayWithOffset() throws Exception
{
assertThat(DateUtils.getTodayWithOffset(), equalTo(new Timestamp(FIXED_LOCAL_TIME)));
DateUtils.setStartDayOffset(9, 0);
assertThat(
DateUtils.getTodayWithOffset(),
equalTo(new Timestamp(FIXED_LOCAL_TIME - DAY_LENGTH)));
}
@Test
public void testGetStartOfDayWithOffset() throws Exception
{
long timestamp = unixTime(2020, SEPTEMBER, 3);
assertThat(
DateUtils.getStartOfDayWithOffset(timestamp + HOUR_LENGTH),
equalTo(timestamp));
DateUtils.setStartDayOffset(3, 30);
assertThat(
DateUtils.getStartOfDayWithOffset(timestamp + 3 * HOUR_LENGTH + 29 * MINUTE_LENGTH),
equalTo(timestamp - DAY_LENGTH));
} }
@Test @Test

Loading…
Cancel
Save