Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Default BLiP TimeZoneOffset = São Paulo (GMT-3)
- var DEFAULT_OFFSET = 3;
- // Receive the variables as parameters
- function run(offset, start, end, workDays) {
- offset = parseInt(offset) + DEFAULT_OFFSET;
- let today = nowUTC(offset);
- let startDate = utcDate(start, offset);
- let endDate = utcDate(end, offset);
- return ((today - startDate) > 0) && ((endDate - today) > 0) && isWorkDay(today, workDays);
- }
- //Get now UTC Date
- function nowUTC(offset){
- let now = new Date;
- let utc_timestamp = Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(),
- now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds(), now.getUTCMilliseconds());
- return new Date(utc_timestamp + offset * 3600 * 1000);
- }
- //Get UTC Date
- function utcDate(timeString, offset){
- let now = new Date;
- let hour = getValueByString('hour', timeString);
- let minutes = getValueByString('minutes', timeString)
- hour += DEFAULT_OFFSET;
- let utc_timestamp = Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(),
- hour, minutes, 0, 0);
- return new Date(utc_timestamp + offset * 3600 * 1000);
- }
- //Get hour/minute by string with pattern HH:mm
- function getValueByString(type, timeString){
- if(type === 'hour'){
- return parseInt(timeString.substring(0, timeString.indexOf(':')));
- }
- else if(type === 'minutes'){
- return parseInt(timeString.substring(timeString.indexOf(':') + 1, timeString.length));
- }
- return 0;
- }
- //Get if today is a work day
- function isWorkDay(today, workDays){
- workDays = workDays.split(',');
- return workDays.indexOf(today.getDay().toString()) >= 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement