Last active
August 29, 2015 14:04
-
-
Save lefnire/4963054242e03d675723 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/script/index.coffee b/script/index.coffee | |
index 71b153b..1fd6591 100644 | |
--- a/script/index.coffee | |
+++ b/script/index.coffee | |
@@ -30,9 +30,10 @@ api.dotGet = (obj,path)-> | |
{now} is also passed in for various purposes, one example being the test scripts scripts testing different "now" times | |
### | |
sanitizeOptions = (o) -> | |
- dayStart = if (!_.isNaN(+o.dayStart) and 0 <= +o.dayStart <= 24) then +o.dayStart else 0 | |
- timezoneOffset = if o.timezoneOffset then +(o.timezoneOffset) else +moment().zone() | |
- now = if o.now then moment(o.now).zone(timezoneOffset) else moment(+new Date).zone(timezoneOffset) | |
+ # Removing some unecessary sanitization, as values are sanitized before they hit the database | |
+ dayStart = if 0 <= +o.dayStart <= 24 then +o.dayStart else 0 | |
+ timezoneOffset = if o.timezoneOffset then +o.timezoneOffset else +moment().zone() | |
+ now = if o.now then moment(o.now).zone(timezoneOffset) else moment(new Date).zone(timezoneOffset) | |
# return a new object, we don't want to add "now" to user object | |
{dayStart, timezoneOffset, now} | |
@@ -49,13 +50,13 @@ api.dayMapping = {0:'su',1:'m',2:'t',3:'w',4:'th',5:'f',6:'s'} | |
### | |
Absolute diff from "yesterday" till now | |
### | |
-api.daysSince = (yesterday, options = {}) -> | |
+api.daysSince = (lastCron, options = {}) -> | |
o = sanitizeOptions options | |
- ReturnValue = Math.abs api.startOfDay(_.defaults {now:yesterday}, o).diff(o.now, 'days') | |
- HourCheck = moment(yesterday).zone(o.timezoneOffset) | |
- if HourCheck.hour() < o.dayStart and o.now.hour() >= o.dayStart then ReturnValue++ | |
- if HourCheck.hour() < o.dayStart and o.now.hour() < o.dayStart and HourCheck.diff(o.now,'d') != 0 then ReturnValue++ | |
- return ReturnValue | |
+ ct = Math.ceil(moment(o.now).diff(lastCron, 'hours') / 24) # Switching from diff('days') to ceil(diff('hours')/24) is the primary fixing change! | |
+ ct = Math.abs ct # In case the switch timezones into the future, gotta figure out how to handle that | |
+ if 0 < o.now.hour() < o.dayStart then ct-- | |
+ ct=0 if ct < 0 | |
+ return ct | |
### | |
Should the user do this taks on this date, given the task's repeat options and user.preferences.dayStart? | |
diff --git a/test/algos.mocha.coffee b/test/algos.mocha.coffee | |
index 5baa789..f4abc2e 100644 | |
--- a/test/algos.mocha.coffee | |
+++ b/test/algos.mocha.coffee | |
@@ -90,6 +90,7 @@ expectGainedPoints = (before, after, taskType) -> | |
# daily & todo histories handled on cron | |
expectNoChange = (before,after) -> | |
+ _.each [before,after],(obj)-> delete obj.stats.buffs | |
_.each $w('stats items gear dailys todos rewards flags preferences'), (attr)-> | |
expect(after[attr]).to.eql before[attr] | |
@@ -668,7 +669,9 @@ describe 'Helper', -> | |
yesterday = new Date(2014, 2, 1, 4) | |
dayStart = 6 | |
now = new Date(2014, 2, 2, 4) | |
- expect(shared.daysSince(yesterday, {now, dayStart})).to.eql 1 | |
+ expect(shared.daysSince(yesterday, {now, dayStart})).to.eql 0 | |
+ # This should be 0. 24 hours have passed, but we haven't yet hit dayStart. Imagine the user has created an account | |
+ # yesterday at 4am, sets their dayStart to 6, logs in today at 4am - cron shouldn't run yet. | |
it 'Checks Two days Apart Yesterday two days ago after dayStart today before', -> | |
yesterday = new Date(2014, 2, 4, 8) | |
dayStart = 6 | |
@@ -684,6 +687,8 @@ describe 'Helper', -> | |
dayStart = 6 | |
now = new Date(2014, 2, 6, 8) | |
expect(shared.daysSince(yesterday, {now, dayStart})).to.eql 3 | |
+ # This is the one that gets me. Shouldn't this be 2? Only 2 full days have passed, assuming a user created an account | |
+ # on 2/4 and logged in again on 2/6 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Actually, for handling timezone traveling, wouldn't this be handled properly in the first place, since all time objects stored are calculated against the users' timezones? Ie, maybe we can remove the
Math.abs
part