mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 14:47:53 +01:00
refactor api.startOfDay to a cron.js function not exposed on api
This commit is contained in:
@@ -47,3 +47,24 @@ export function startOfWeek (options) {
|
||||
return moment(o.now).startOf('week');
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
This is designed for use with any date that has an important time portion (e.g., when comparing the current date-time with the previous cron's date-time for determing if cron should run now).
|
||||
It changes the time portion of the date-time to be the Custom Day Start hour, so that the date-time is now the user's correct start of day.
|
||||
It SUBTRACTS a day if the date-time's original hour is before CDS (e.g., if your CDS is 5am and it's currently 4am, it's still the previous day).
|
||||
This is NOT suitable for manipulating any dates that are displayed to the user as a date with no time portion, such as a Daily's Start Dates (e.g., a Start Date of today shows only the date, so it should be considered to be today even if the hidden time portion is before CDS).
|
||||
*/
|
||||
|
||||
export function startOfDay (options) {
|
||||
if (options === null) {
|
||||
options = {};
|
||||
}
|
||||
let o = sanitizeOptions(options);
|
||||
let dayStart = moment(o.now).startOf('day').add({ hours: o.dayStart });
|
||||
|
||||
if (moment(o.now).hour() < o.dayStart) {
|
||||
dayStart.subtract({ days: 1 });
|
||||
}
|
||||
return dayStart;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import {
|
||||
DAY_MAPPING, // temporary, pending further refactoring
|
||||
sanitizeOptions, // temporary, pending further refactoring
|
||||
startOfDay, // temporary, pending further refactoring
|
||||
} from '../../common/script/cron';
|
||||
|
||||
var $w, _, api, content, i18n, moment, preenHistory, sortOrder,
|
||||
@@ -73,23 +74,6 @@ api.planGemLimits = {
|
||||
------------------------------------------------------
|
||||
*/
|
||||
|
||||
api.startOfDay = function(options) {
|
||||
var dayStart, o;
|
||||
if (options == null) {
|
||||
options = {};
|
||||
}
|
||||
o = sanitizeOptions(options);
|
||||
dayStart = moment(o.now).startOf('day').add({
|
||||
hours: o.dayStart
|
||||
});
|
||||
if (moment(o.now).hour() < o.dayStart) {
|
||||
dayStart.subtract({
|
||||
days: 1
|
||||
});
|
||||
}
|
||||
return dayStart;
|
||||
};
|
||||
|
||||
/*
|
||||
Absolute diff from "yesterday" till now
|
||||
*/
|
||||
@@ -100,9 +84,9 @@ api.daysSince = function(yesterday, options) {
|
||||
options = {};
|
||||
}
|
||||
o = sanitizeOptions(options);
|
||||
return api.startOfDay(_.defaults({
|
||||
return startOfDay(_.defaults({
|
||||
now: o.now
|
||||
}, o)).diff(api.startOfDay(_.defaults({
|
||||
}, o)).diff(startOfDay(_.defaults({
|
||||
now: yesterday
|
||||
}, o)), 'days');
|
||||
};
|
||||
@@ -121,7 +105,7 @@ api.shouldDo = function(day, dailyTask, options) {
|
||||
return false;
|
||||
}
|
||||
o = sanitizeOptions(options);
|
||||
startOfDayWithCDSTime = api.startOfDay(_.defaults({
|
||||
startOfDayWithCDSTime = startOfDay(_.defaults({
|
||||
now: day
|
||||
}, o));
|
||||
taskStartDate = moment(dailyTask.startDate).zone(o.timezoneOffset);
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
import {
|
||||
DAY_MAPPING,
|
||||
startOfWeek,
|
||||
startOfDay,
|
||||
} from '../../common/script/cron';
|
||||
|
||||
let expect = require('expect.js');
|
||||
@@ -1190,7 +1191,7 @@ describe('Cron', () => {
|
||||
let fstr = 'YYYY-MM-DD HH: mm: ss';
|
||||
|
||||
it('startOfDay before dayStart', () => {
|
||||
let start = shared.startOfDay({
|
||||
let start = startOfDay({
|
||||
now: moment('2014-10-09 02: 30: 00'),
|
||||
dayStart,
|
||||
});
|
||||
@@ -1198,7 +1199,7 @@ describe('Cron', () => {
|
||||
expect(start.format(fstr)).to.eql('2014-10-08 04: 00: 00');
|
||||
});
|
||||
it('startOfDay after dayStart', () => {
|
||||
let start = shared.startOfDay({
|
||||
let start = startOfDay({
|
||||
now: moment('2014-10-09 05: 30: 00'),
|
||||
dayStart,
|
||||
});
|
||||
@@ -1501,17 +1502,17 @@ describe('Helper', () => {
|
||||
let today = '2013-01-01 00: 00: 00';
|
||||
let zone = moment(today).zone();
|
||||
|
||||
expect(shared.startOfDay({
|
||||
expect(startOfDay({
|
||||
now: new Date(2013, 0, 1, 0),
|
||||
}, {
|
||||
timezoneOffset: zone,
|
||||
}).format(fstr)).to.eql(today);
|
||||
expect(shared.startOfDay({
|
||||
expect(startOfDay({
|
||||
now: new Date(2013, 0, 1, 5),
|
||||
}, {
|
||||
timezoneOffset: zone,
|
||||
}).format(fstr)).to.eql(today);
|
||||
expect(shared.startOfDay({
|
||||
expect(startOfDay({
|
||||
now: new Date(2013, 0, 1, 23, 59, 59),
|
||||
timezoneOffset: zone,
|
||||
}).format(fstr)).to.eql(today);
|
||||
|
||||
Reference in New Issue
Block a user