nse-business-time
Version:
Calculate Business Time for NSE,BSE,MCX and NFO
104 lines (76 loc) • 2.97 kB
JavaScript
const moment = require('moment');
const momentz = require('moment-timezone');
const _cnst = require('../utils/constants');
const _open_scheduling = require('./open-and-scheduling-time');
const _holidayList = require('../utils/holiday-list');
function getLastWorkingDay(){
let yesterday = moment().add(-1, 'days').valueOf();
let weekday = moment(yesterday).isoWeekday();
while([6,7].indexOf(weekday) !== -1){
yesterday = moment(yesterday).add(-1, 'days').valueOf();
weekday = moment(yesterday).isoWeekday();
}
return yesterday;
}
function getNextWorkingDay(exchange, today){
let nextDay = moment(today).add(1, 'days').valueOf();
while(_holidayList.isHoliday(exchange, nextDay)){
nextDay = moment(nextDay).add(1, 'days').valueOf();
}
return nextDay;
}
function covnertToString(milliseconds){
return momentz(milliseconds).tz(_cnst.TIME_ZONE).format(_cnst.DATE_FORMAT);
}
function marketOpenForExchange(exchange, time){
let startDate= momentz.tz(covnertToString(time) + " " + _cnst.O_MKT_START[exchange], _cnst.DATE_TIME_FORMAT, _cnst.TIME_ZONE).valueOf();
return startDate;
}
function getNextOpenTime(exchange, currentTime=0){
if(currentTime === 0){
currentTime = momentz().tz(_cnst.TIME_ZONE).valueOf();
}
if(_cnst.O_MKT_TIMING.OPEN[exchange] === 0 || !_cnst.O_MKT_TIMING.OPEN[exchange]){
_open_scheduling.marketOpenTime (currentTime);
_open_scheduling.marketCloseTime(currentTime);
}
let start = _cnst.O_MKT_TIMING.OPEN[exchange];
if(currentTime <= start){
return start;
}else{
let nextDay = getNextWorkingDay(exchange, currentTime);
return marketOpenForExchange(exchange, nextDay);
}
}
function getLastOpenTime(exchange, currentTime=0){
if(currentTime === 0){
currentTime = momentz().tz(_cnst.TIME_ZONE).valueOf();
}
if(_cnst.O_MKT_TIMING.OPEN[exchange] === 0 || !_cnst.O_MKT_TIMING.OPEN[exchange]){
marketOpenTime (currentTime);
marketCloseTime(currentTime);
}
let start = _cnst.O_MKT_TIMING.OPEN[exchange];
if(currentTime >= start){
return start;
}else{
let nextDay = getLastWorkingDay();
return marketOpenForExchange(exchange, nextDay);
}
}
function getCurrentOpenTime(exchange, currentTime=0){
if(currentTime === 0){
currentTime = momentz().tz(_cnst.TIME_ZONE).valueOf();
}
if(_cnst.O_MKT_TIMING.OPEN[exchange] === 0 || !_cnst.O_MKT_TIMING.OPEN[exchange]){
marketOpenTime (currentTime);
marketCloseTime(currentTime);
}
let start = _cnst.O_MKT_TIMING.OPEN[exchange];
return start;
}
exports.getLastWorkingDay = getLastWorkingDay;
exports.getNextWorkingDay = getNextWorkingDay;
exports.getNextOpenTime = getNextOpenTime;
exports.getLastOpenTime = getLastOpenTime;
exports.getCurrentOpenTime = getCurrentOpenTime;