Hi gang,
I know Jaimie has a great example of calculating total time an issue's been in a particular status:
https://scriptrunner.adaptavist.com/latest/jira/scripted-fields.html#_total_time_this_issue_has_been_in_progress
I've been trying to modify it to only count duration during weekdays (no weekends). I've seen some examples, but I can't get my version to work. I feel like I am close, but missing something. Can someone take a look at my script, and suggest what I need to change? Thanks!!
PS – if someone has a way to use jiraDurationUtils to parse the duration to keep out weekends. that would be good to know. Wasn't successful via that approach either.
import com.atlassian.jira.component.ComponentAccessor
import static java.util.GregorianCalendar.*
import com.atlassian.jira.issue.history.ChangeItemBean
enableCache = { -> false }
def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()
def myStatus = "In Progress"
List<Long> rt = [0L]
def changeItems = changeHistoryManager.getChangeItemsForField(issue, "status")
changeItems.reverse().each { ChangeItemBean item ->
item.toString == myStatus
def timeStart = System.currentTimeMillis()
def timeEnd = item.created.getTime()
def c1 = new GregorianCalendar()
c1.setTimeInMillis(timeStart)
int w1 = c1.get(DAY_OF_WEEK);
c1.add(DAY_OF_WEEK, -w1);
def c2 = new GregorianCalendar()
c2.setTimeInMillis(timeEnd)
int w2 = c2.get(DAY_OF_WEEK);
c2.add(DAY_OF_WEEK, -w2);
//end Saturday to start Saturday
def days = (c2.getTimeInMillis() - c1.getTimeInMillis()) / (1000 * 60 * 60 * 24) as long
def daysWithoutWeekendDays = days - (days * 2 / 7) as long
// Adjust days to add on (w2) and days to subtract (w1) so that Saturday
// and Sunday are not included
if (w1 == SUNDAY && w2 != SATURDAY) {
w1 = MONDAY;
} else if (w1 == SATURDAY && w2 != SUNDAY) {
w1 = FRIDAY;
}
if (w2 == SUNDAY) {
w2 = MONDAY;
} else if (w2 == SATURDAY) {
w2 = FRIDAY;
}
//convert to seconds for Duration
def timeDiff = (daysWithoutWeekendDays - w1 + w2) * (60 * 60 * 24)
// def timeDiff = timeStart - timeEnd
if (item.fromString == myStatus) {
rt << -timeDiff
}
if (item.toString == myStatus) {
rt << timeDiff
}
}
def total = rt.sum() as Long
return total ?: 0L