Guest User

Untitled

a guest
Feb 17th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. // constants
  2.  
  3. // holidays or vacations, date format must be the same
  4. Set HOLIDAYS = new HashSet(['02.02.2019', '03.02.2019']);
  5.  
  6. // date format need by date check
  7. def SIMPLE_DATE_FORMAT = new SimpleDateFormat('dd.MM.YYYY');
  8.  
  9. // time/date constants
  10. long DAY = 24 * 60 * 60 * 1000, HOUR = 60 * 60 * 1000;
  11. int START_WORK_HOURS = 07, END_WORK_HOURS = 16;
  12. int WORK_DAY_HOURS = END_WORK_HOURS - START_WORK_HOURS;
  13. long WORK_DAY_AS_MILLIS = WORK_DAY_HOURS * HOUR;
  14.  
  15. long diff = 0;
  16.  
  17. // date value definition and extraction
  18.  
  19. def createdAt = doc['created_at'];
  20. def firstRespondedAt = doc['stats.first_responded_at'];
  21.  
  22. // handling for corrupt or invalid data
  23.  
  24. if(createdAt==null || firstRespondedAt== null){
  25. return -1L;
  26. }
  27. if (firstRespondedAt.size() == 0) {
  28. return -1L;
  29. }
  30.  
  31. // initialisation of calendar's, necessary for iteration over calendar
  32.  
  33. Date dateStart = new Date(createdAt.date.millis);
  34. Calendar startCalendar = new GregorianCalendar();
  35. startCalendar.setTime(dateStart);
  36.  
  37. Date dateEnd = new Date(firstRespondedAt.date.millis);
  38. Calendar endCalendar = new GregorianCalendar();
  39. endCalendar.setTime(dateEnd);
  40.  
  41. // simple check: are date values in the same day
  42.  
  43. boolean sameDay = startCalendar.get(Calendar.DAY_OF_YEAR) == endCalendar.get(Calendar.DAY_OF_YEAR) && endCalendar.get(Calendar.YEAR) == endCalendar.get(Calendar.YEAR);
  44. Date resultTemp = startCalendar.getTime();
  45.  
  46. String resultDate = SIMPLE_DATE_FORMAT.format(resultTemp);
  47.  
  48. //calculation of expected start work hours for start date
  49. Calendar startCalendarExpected = (Calendar) endCalendar.clone();
  50. startCalendarExpected.set(Calendar.HOUR_OF_DAY, START_WORK_HOURS);
  51. startCalendarExpected.set(Calendar.MINUTE, 0);
  52. startCalendarExpected.set(Calendar.SECOND, 0);
  53.  
  54. //calculation of expected end work hours for start date
  55. Calendar endCalendarExpected = (Calendar) startCalendar.clone();
  56. endCalendarExpected.set(Calendar.HOUR_OF_DAY, END_WORK_HOURS);
  57. endCalendarExpected.set(Calendar.MINUTE, 0);
  58. endCalendarExpected.set(Calendar.SECOND, 0);
  59.  
  60. if(!HOLIDAYS.contains(resultDate)) {
  61. if(sameDay){
  62. if(endCalendar.getTimeInMillis()>endCalendarExpected.getTimeInMillis()&&startCalendar.getTimeInMillis()>endCalendarExpected.getTimeInMillis()){
  63. return 0L;
  64. }else if(startCalendar.getTimeInMillis()<startCalendarExpected.getTimeInMillis()&&endCalendar.getTimeInMillis()<startCalendarExpected.getTimeInMillis()){
  65. return 0L;
  66. }else{
  67. long start = startCalendar.getTimeInMillis()<startCalendarExpected.getTimeInMillis()? startCalendarExpected.getTimeInMillis() : startCalendar.getTimeInMillis();
  68. long end = endCalendar.getTimeInMillis()>endCalendarExpected.getTimeInMillis()? endCalendarExpected.getTimeInMillis() : endCalendar.getTimeInMillis();
  69. return end - start;
  70. }
  71. }
  72. long tempDiff = endCalendarExpected.getTimeInMillis() - startCalendar.getTimeInMillis();
  73. if (tempDiff > 0) {
  74. diff += tempDiff > WORK_DAY_AS_MILLIS ? WORK_DAY_AS_MILLIS : tempDiff;
  75. }
  76. }
  77. startCalendar.add(Calendar.DATE, 1);
  78. while (sameDay && startCalendar.before(endCalendar)) {
  79. resultTemp = startCalendar.getTime();
  80. resultDate = SIMPLE_DATE_FORMAT.format(resultTemp);
  81. if (!HOLIDAYS.contains(resultDate)) {
  82. diff += WORK_DAY_AS_MILLIS;
  83.  
  84. }
  85. startCalendar.add(Calendar.DATE, 1);
  86. }
  87.  
  88. Date result = endCalendar.getTime();
  89. resultDate = SIMPLE_DATE_FORMAT.format(result);
  90. if (!HOLIDAYS.contains(resultDate)) {
  91. long tempDiff = endCalendar.getTimeInMillis() - startCalendarExpected.getTimeInMillis();
  92. if (tempDiff > 0) {
  93. diff += tempDiff > WORK_DAY_AS_MILLIS ? WORK_DAY_AS_MILLIS : tempDiff;
  94. }
  95. }
  96. return diff;
Add Comment
Please, Sign In to add comment