Skip to content

Instantly share code, notes, and snippets.

@vozerov
Created February 15, 2019 11:05
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vozerov/c3578e727e511cd99fd7e9af3e348e14 to your computer and use it in GitHub Desktop.
Save vozerov/c3578e727e511cd99fd7e9af3e348e14 to your computer and use it in GitHub Desktop.
Calculate working interval between 2 timestamps in elasticsearch
// 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;
@sladinat
Copy link

Not everything is perfect, but it does what it promises.

@maverickfinn
Copy link

Thanks a lot))))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment