HabitRPG/habitrpg-shared — pull request #336 by brandonjreid (2014-10-10 16:07:27+00:00)
Title: Fixed cron dayStart option.

This has been said before, but I think I fixed the cron issues with dayStart.

The issue comes when the last cron was after midnight and before dayStart. Then when you attempt a cron again on the same day, but after dayStart, it doesn't compute the daysSince correctly. I adjusted startOfDay to take dayStart into account. So, when dayStart = 4 and lastCron = '2014-10-09 01:30:00' then startOfDay will return '2014-10-08 04:00:00' since technically our day doesn't start until 4am. This makes the code for both daysSince and shouldDo simpler because we just alter which day we consider a certain timestamp to be at.

In daysSince I didn't use the diff 'true' parameter or use a smaller diff interval with Math.ceil as was suggested earlier because I compute the startDay of both yesterday and now. So, both dates will be set to dayStart. They might have a difference in date, but not in time, so the diff will always be a whole number.

I wrote a number of test cases for startDay and daysSince to verify they are correct. Some of the test cases in Cron->dailies->new day were incorrect. For example: "due yesterday due today pre-dayStart checked" was expecting cron not to run even though dayStart = 4, lastCron = '2014-10-09 03:00:00', and now = '2014-10-10 03:00:00'. The incorrect tests were assuming that lastCron always happened after dayStart, even though lastCron was being set before dayStart.

I removed the test "calculates day differences with dayStart properly" because with the fixed cron the test would fail if you ran it between midnight and dayStart and pass other times. The new tests I created should cover the same cases as this test, so I just removed it.

I also fixed the test "calculates the start of the day" for the time at 23:59:59. The issue here was the date is in January, but the function wasn't given a timezone, so it defaults to now's timezone. So, the test case fails if you run it while in daylight savings time because the timezone offset is different. Passing in the timezone for January fixes this issue.

Should fix (crossing my fingers) issue HabitRPG/habitrpg#1057