Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Default BLiP TimeZoneOffset = São Paulo (GMT-3)
  2. var DEFAULT_OFFSET = 3;
  3.  
  4. // Receive the variables as parameters
  5. function run(offset, start, end, workDays) {
  6.  
  7.     offset = parseInt(offset) + DEFAULT_OFFSET;
  8.  
  9.     let today = nowUTC(offset);
  10.     let startDate = utcDate(start, offset);
  11.     let endDate = utcDate(end, offset);
  12.     return ((today - startDate) > 0) && ((endDate - today) > 0) && isWorkDay(today, workDays);
  13. }
  14.  
  15. //Get now UTC Date
  16. function nowUTC(offset){
  17.     let now = new Date;
  18.     let utc_timestamp = Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(),
  19.         now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds(), now.getUTCMilliseconds());
  20.  
  21.     return new Date(utc_timestamp + offset * 3600 * 1000);
  22. }
  23.  
  24. //Get UTC Date
  25. function utcDate(timeString, offset){
  26.     let now = new Date;
  27.  
  28.     let hour = getValueByString('hour', timeString);
  29.     let minutes = getValueByString('minutes', timeString)
  30.  
  31.     hour += DEFAULT_OFFSET;
  32.  
  33.     let utc_timestamp = Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(),
  34.         hour, minutes, 0, 0);
  35.     return new Date(utc_timestamp + offset * 3600 * 1000);
  36. }
  37.  
  38. //Get hour/minute by string with pattern HH:mm
  39. function getValueByString(type, timeString){
  40.  
  41.     if(type === 'hour'){
  42.         return parseInt(timeString.substring(0, timeString.indexOf(':')));
  43.     }
  44.        
  45.     else if(type === 'minutes'){
  46.         return parseInt(timeString.substring(timeString.indexOf(':') + 1, timeString.length));
  47.     }
  48.  
  49.     return 0;
  50. }
  51.  
  52. //Get if today is a work day
  53. function isWorkDay(today, workDays){
  54.     workDays = workDays.split(',');
  55.  
  56.     return workDays.indexOf(today.getDay().toString()) >= 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement