@atomic-utils/time
Version:
109 lines • 3.58 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.roundToXHour = exports.minutesUntilStrategyEvent = exports.minutesUntilStrategyEnd = exports.minutesUntilStrategyStart = exports.minutesUntil = exports.daysUntil = exports.MONTH_NAMES = exports.TIME_MILISECS = exports.TIME_MINUTES = void 0;
exports.TIME_MINUTES = {
_7days: 7 * 24 * 60,
};
exports.TIME_MILISECS = {
_24hours: 24 * 60 * 60 * 1000,
_1hour: 60 * 60 * 1000,
_1min: 60 * 1000,
};
exports.MONTH_NAMES = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December',
];
/**
* Calculate the number of days between
* current time and a particular day of week
* @param {Date} t current time
* @param {DayOfWeek} d day of week
* @returns {number} number of days
*/
const daysUntil = (t, d) => {
if (d === -1 || d === 7) {
return 0;
}
let dayDiff = d - t.getUTCDay();
if (dayDiff < 0)
dayDiff += 7;
return dayDiff;
};
exports.daysUntil = daysUntil;
/**
* Calculate the minutes until a certain time and day
*
* Note: time of day is derived from t, and day of
* week is derived from numDays
* @param {Date} t current time
* @param {number} numDays number of days until strategy day
* @returns {number} number of minutes
*/
const minutesUntil = (t, numDays) => {
const minutes = t.getUTCMinutes();
const hours = t.getUTCHours();
return numDays * 24 * 60 - minutes - hours * 60;
};
exports.minutesUntil = minutesUntil;
/**
* Calculate how many minutes until strategy startAt
* @param {Date} t current time
* @param {IStrategy} s The strategy
* @returns {number} number of minutes
*/
const minutesUntilStrategyStart = (t, s) => {
return (0, exports.minutesUntilStrategyEvent)(t, s, 'start');
};
exports.minutesUntilStrategyStart = minutesUntilStrategyStart;
/**
* Calculate how many minutes until strategy endAt
* @param {Date} t current time
* @param {IStrategy} s The strategy
* @returns {number} number of minutes
*/
const minutesUntilStrategyEnd = (t, s) => {
return (0, exports.minutesUntilStrategyEvent)(t, s, 'end');
};
exports.minutesUntilStrategyEnd = minutesUntilStrategyEnd;
/**
* Calculate how many minutes until strategy event
* @param {Date} t current time
* @param {IStrategy} s The strategy
* @returns {number} number of minutes
*/
const minutesUntilStrategyEvent = (t, s, event) => {
// hourly strategies' endAt are ignored – stop monitoring after an hour.
if (event === 'end' && s.entryConditions.recurring == 'hourly') {
const nearestXHour = (0, exports.roundToXHour)(t, 1);
return (+nearestXHour - +t) / exports.TIME_MILISECS._1min;
}
const minutesUntilEvent = (0, exports.minutesUntil)(t, (0, exports.daysUntil)(t, s.entryConditions.dayOfWeek)) +
(event === 'start' ? s.entryConditions.startAt : s.entryConditions.endAt);
const finalMinutesUntilEvent = minutesUntilEvent <= 0
? minutesUntilEvent + exports.TIME_MINUTES._7days
: minutesUntilEvent;
return finalMinutesUntilEvent;
};
exports.minutesUntilStrategyEvent = minutesUntilStrategyEvent;
/**
* roundtoXHour rounds up to the nearest X Hour (i.e. round to nearest 4 hour)
* @param date current Date
* @param x multiplier
* @returns Date
*/
const roundToXHour = (date, x) => {
const p = x * 60 * 60 * 1000;
return new Date(Math.ceil(date.getTime() / p) * p);
};
exports.roundToXHour = roundToXHour;
//# sourceMappingURL=strategy.js.map
;