Setting custom start of the day (#621)

This commit is contained in:
Kristian Tashkov
2020-09-20 03:23:00 +03:00
committed by GitHub
parent 720f98f9bd
commit d997b1378d
27 changed files with 125 additions and 45 deletions

View File

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

View File

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

View File

@@ -71,7 +71,7 @@ public abstract class ScoreList implements Iterable<Score>
*/
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();
if (oldestRep == null) return;
Timestamp today = DateUtils.getToday();
Timestamp today = DateUtils.getTodayWithOffset();
compute(oldestRep.getTimestamp(), today);
}

View File

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

View File

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

View File

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

View File

@@ -37,10 +37,25 @@ public abstract class DateUtils
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.
*/
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)
{
@@ -165,24 +180,41 @@ public abstract class DateUtils
return new Timestamp(getStartOfToday());
}
@NonNull
public static Timestamp getTodayWithOffset()
{
return new Timestamp(getStartOfTodayWithOffset());
}
public static long getStartOfDay(long timestamp)
{
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()
{
return getStartOfDay(getLocalTime());
}
public static long getStartOfTomorrow()
public static long getStartOfTomorrowWithOffset()
{
return getUpcomingTimeInMillis(0, 0);
return getUpcomingTimeInMillis(startDayHourOffset, startDayMinuteOffset);
}
public static long millisecondsUntilTomorrow()
public static long getStartOfTodayWithOffset()
{
return getStartOfTomorrow() - getLocalTime();
return getStartOfDayWithOffset(getLocalTime());
}
public static long millisecondsUntilTomorrowWithOffset()
{
return getStartOfTomorrowWithOffset() - getLocalTime();
}
public static GregorianCalendar getStartOfTodayCalendar()
@@ -190,6 +222,11 @@ public abstract class DateUtils
return getCalendar(getStartOfToday());
}
public static GregorianCalendar getStartOfTodayCalendarWithOffset()
{
return getCalendar(getStartOfTodayWithOffset());
}
private static TimeZone getTimezone()
{
if(fixedTimeZone != null) return fixedTimeZone;
@@ -217,6 +254,12 @@ public abstract class DateUtils
fixedLocale = locale;
}
public static void setStartDayOffset(int hourOffset, int minuteOffset)
{
startDayHourOffset = hourOffset;
startDayMinuteOffset = minuteOffset;
}
private static Locale getLocale()
{
if(fixedLocale != null) return fixedLocale;

View File

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