Files
habitica/common/script/cron.js
2015-11-22 11:05:03 +10:00

73 lines
2.5 KiB
JavaScript

/*
------------------------------------------------------
Cron and time / day functions
------------------------------------------------------
*/
import _ from 'lodash';
import moment from 'moment';
export const DAY_MAPPING = {
0: 'su',
1: 'm',
2: 't',
3: 'w',
4: 'th',
5: 'f',
6: 's',
};
/*
Each time we perform date maths (cron, task-due-days, etc), we need to consider user preferences.
Specifically {dayStart} (custom day start) and {timezoneOffset}. This function sanitizes / defaults those values.
{now} is also passed in for various purposes, one example being the test scripts scripts testing different "now" times.
*/
export function sanitizeOptions (o) {
let ref = Number(o.dayStart);
let dayStart = !_.isNaN(ref) && ref >= 0 && ref <= 24 ? ref : 0;
let timezoneOffset = o.timezoneOffset ? Number(o.timezoneOffset) : Number(moment().zone());
// TODO: check and clean timezoneOffset as for dayStart (e.g., might not be a number)
let now = o.now ? moment(o.now).zone(timezoneOffset) : moment().zone(timezoneOffset);
// return a new object, we don't want to add "now" to user object
return {
dayStart,
timezoneOffset,
now,
};
}
export function startOfWeek (options = {}) {
let o = sanitizeOptions(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 = {}) {
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;
}
/*
Absolute diff from "yesterday" till now
*/
export function daysSince (yesterday, options = {}) {
let o = sanitizeOptions(options);
return startOfDay(_.defaults({ now: o.now }, o)).diff(startOfDay(_.defaults({ now: yesterday }, o)), 'days');
}