HabitRPG/habitrpg-shared — pull request #84 by colegleason (2014-01-08 10:22:11+00:00)
Title: Custom Day Start: change condition on when we run cron

This is an attempt to fix some of the problems people have with custom
day start, especially those in habitrpg/habitrpg#1057.

I believe this is a bug in when we decide to run cron. Right now we look
for the difference between `startOfDay(user.lastCron)` and `now` to be
greater than 0. I'll explain why this is a problem.

For the following cases, let today be January 8th and yesterday be January 7th.

Consider the scenario where `dayStart = 0` and `user.lastCron` is sometime yesterday.

`lastCron.start('days').add('d', dayStart)` will just be January 7th at midnight. Then
the difference will be greater than 0 anytime after midnight on January
8th. Works as expected.

However, consider the case where `dayStart` is set to something greater than
0, like 3. Now let's say the user logs on after midnight on January 8th
but before their Custom Day Start (something common in many reports),
and cron is run.

`lasCron.start('days').add('d', dayStart)` will be January 8th at
3am. Therefore the difference won't be greater than 0 until January 9th
at 3am and their dailies, etc won't become unchecked.

Therefore, this commit adds two extra conditions to check when `daysMissed`
is equal to zero:

* cronBeforeStart: Was the last cron run before the Custom Day Start
time for today's date?
* nowAfterStart: Have we passed the Current Day Start time for today's
date?

I believe that both of these condition must be true OR daysMissed must
be greater than zero to run cron. Please, please, please double and
triple check my logic here. I don't want to make this any more of a
mess, because working with times makes my brain hurt.

(cc @lefnire)