Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // constants
- // holidays or vacations, date format must be the same
- Set HOLIDAYS = new HashSet(['02.02.2019', '03.02.2019']);
- // date format need by date check
- def SIMPLE_DATE_FORMAT = new SimpleDateFormat('dd.MM.YYYY');
- // time/date constants
- long DAY = 24 * 60 * 60 * 1000, HOUR = 60 * 60 * 1000;
- int START_WORK_HOURS = 07, END_WORK_HOURS = 16;
- int WORK_DAY_HOURS = END_WORK_HOURS - START_WORK_HOURS;
- long WORK_DAY_AS_MILLIS = WORK_DAY_HOURS * HOUR;
- long diff = 0;
- // date value definition and extraction
- def createdAt = doc['created_at'];
- def firstRespondedAt = doc['stats.first_responded_at'];
- // handling for corrupt or invalid data
- if(createdAt==null || firstRespondedAt== null){
- return -1L;
- }
- if (firstRespondedAt.size() == 0) {
- return -1L;
- }
- // initialisation of calendar's, necessary for iteration over calendar
- Date dateStart = new Date(createdAt.date.millis);
- Calendar startCalendar = new GregorianCalendar();
- startCalendar.setTime(dateStart);
- Date dateEnd = new Date(firstRespondedAt.date.millis);
- Calendar endCalendar = new GregorianCalendar();
- endCalendar.setTime(dateEnd);
- // simple check: are date values in the same day
- boolean sameDay = startCalendar.get(Calendar.DAY_OF_YEAR) == endCalendar.get(Calendar.DAY_OF_YEAR) && endCalendar.get(Calendar.YEAR) == endCalendar.get(Calendar.YEAR);
- Date resultTemp = startCalendar.getTime();
- String resultDate = SIMPLE_DATE_FORMAT.format(resultTemp);
- //calculation of expected start work hours for start date
- Calendar startCalendarExpected = (Calendar) endCalendar.clone();
- startCalendarExpected.set(Calendar.HOUR_OF_DAY, START_WORK_HOURS);
- startCalendarExpected.set(Calendar.MINUTE, 0);
- startCalendarExpected.set(Calendar.SECOND, 0);
- //calculation of expected end work hours for start date
- Calendar endCalendarExpected = (Calendar) startCalendar.clone();
- endCalendarExpected.set(Calendar.HOUR_OF_DAY, END_WORK_HOURS);
- endCalendarExpected.set(Calendar.MINUTE, 0);
- endCalendarExpected.set(Calendar.SECOND, 0);
- if(!HOLIDAYS.contains(resultDate)) {
- if(sameDay){
- if(endCalendar.getTimeInMillis()>endCalendarExpected.getTimeInMillis()&&startCalendar.getTimeInMillis()>endCalendarExpected.getTimeInMillis()){
- return 0L;
- }else if(startCalendar.getTimeInMillis()<startCalendarExpected.getTimeInMillis()&&endCalendar.getTimeInMillis()<startCalendarExpected.getTimeInMillis()){
- return 0L;
- }else{
- long start = startCalendar.getTimeInMillis()<startCalendarExpected.getTimeInMillis()? startCalendarExpected.getTimeInMillis() : startCalendar.getTimeInMillis();
- long end = endCalendar.getTimeInMillis()>endCalendarExpected.getTimeInMillis()? endCalendarExpected.getTimeInMillis() : endCalendar.getTimeInMillis();
- return end - start;
- }
- }
- long tempDiff = endCalendarExpected.getTimeInMillis() - startCalendar.getTimeInMillis();
- if (tempDiff > 0) {
- diff += tempDiff > WORK_DAY_AS_MILLIS ? WORK_DAY_AS_MILLIS : tempDiff;
- }
- }
- startCalendar.add(Calendar.DATE, 1);
- while (sameDay && startCalendar.before(endCalendar)) {
- resultTemp = startCalendar.getTime();
- resultDate = SIMPLE_DATE_FORMAT.format(resultTemp);
- if (!HOLIDAYS.contains(resultDate)) {
- diff += WORK_DAY_AS_MILLIS;
- }
- startCalendar.add(Calendar.DATE, 1);
- }
- Date result = endCalendar.getTime();
- resultDate = SIMPLE_DATE_FORMAT.format(result);
- if (!HOLIDAYS.contains(resultDate)) {
- long tempDiff = endCalendar.getTimeInMillis() - startCalendarExpected.getTimeInMillis();
- if (tempDiff > 0) {
- diff += tempDiff > WORK_DAY_AS_MILLIS ? WORK_DAY_AS_MILLIS : tempDiff;
- }
- }
- return diff;
Add Comment
Please, Sign In to add comment