diff --git a/script/index.coffee b/script/index.coffee index 50a9f98b14..d37f2d342a 100644 --- a/script/index.coffee +++ b/script/index.coffee @@ -31,7 +31,10 @@ api.startOfWeek = api.startOfWeek = (options={}) -> api.startOfDay = (options={}) -> o = sanitizeOptions(options) - moment(o.now).startOf('day').add('h', o.dayStart) + dayStart = moment(o.now).startOf('day').add('h', o.dayStart) + # if between midnight and Custom Day Start, jump back to previous day + dayStart.subtract('day', 1) if moment(o.now).isBefore(dayStart) + return dayStart dayMapping = {0:'su',1:'m',2:'t',3:'w',4:'th',5:'f',6:'s'} @@ -1239,4 +1242,3 @@ api.wrap = (user, main=true) -> get: -> tasks = user.habits.concat(user.dailys).concat(user.todos).concat(user.rewards) _.object(_.pluck(tasks, "id"), tasks) - diff --git a/tests/algos.mocha.coffee b/tests/algos.mocha.coffee index a86a5f6301..94178e890b 100644 --- a/tests/algos.mocha.coffee +++ b/tests/algos.mocha.coffee @@ -487,6 +487,8 @@ describe 'Helper', -> expect(shared.startOfDay({now: new Date(2013, 0, 1, 0)}).format('YYYY-MM-DD HH:mm')).to.eql '2013-01-01 00:00' expect(shared.startOfDay({now: new Date(2013, 0, 1, 5)}).format('YYYY-MM-DD HH:mm')).to.eql '2013-01-01 00:00' expect(shared.startOfDay({now: new Date(2013, 0, 1, 23, 59, 59)}).format('YYYY-MM-DD HH:mm')).to.eql '2013-01-01 00:00' + # between midnight and custom day start + expect(shared.startOfDay({now: new Date(2013, 0, 2, 2), dayStart: 4}).format('YYYY-MM-DD HH:mm')).to.eql '2013-01-01 04:00' it 'counts pets', -> pets = {}