Scripted Field for calculating Total Time in Current Status for Business Days

Hi

I need a scripted field to get the total business days in the current status

Below is a scripted field that will retrieve the Issue Total Time for all days in the Current Status.

import com.atlassian.jira.component.ComponentAccessor

def changeHistoryManager = ComponentAccessor.changeHistoryManager
def currentStatusName = issue?.status?.name

def rt = [0L]
changeHistoryManager.getChangeItemsForField (issue, “status”).reverse().each {item →

def timeDiff = System.currentTimeMillis() - item.created.getTime()
if (item.fromString == currentStatusName) {
rt << -timeDiff
}
if (item.toString == currentStatusName){
rt << timeDiff
}
}
def total = rt.sum() as Long
return (total / 1000) as long ?: 0L

In the above script, how to remove weekends from all days.

Hello,
I will recommend you to use the LocalDate (Java Platform SE 8 ) class.
I will do the following:

  • With the timestamp in item.created.getTime(), i will convert it to LocalDate
LocalDate initialStatusDate = item.created.getTime().toLocalDateTime().toLocalDate();

then i will obtain the actual date with LocalDate.now()

now you can make a loop until initialStatusDate == LocalDate.now() and you will be able to control if the day is a weekend or not getting the value of getDayOfWeek()
The advantage of using that class is that is also really easy to add days to the date using the method plusDays(int days)

With that information you can have an index that only increase when the day is a business day, and you will obtain the expected value.

Hi imadero,
Thanks for your reply.


Getting the above error when using LocalDate
I am unfamiliar with scripting, so if you could provide the complete script, it would be extremely helpful.
Thanks

Hi.
The dependency should be added as import java.time.LocalDate and to convert the date of the change to LocalDate, you should do it in that way, LocalDate initialStatusDate = item.created.toLocalDateTime().toLocalDate()
The last time, i added a .getTime() that was unnecessary

I’m afraid I can provide the complete script but i can guide you in the process. For creating custom scripts to customers, in Adaptavist we have a paid service called Scripting Service where the customers could request any development that they need and we will do it for you.
You can open a request here if you are interested. Jira Service Management

Thanks, Ivan.

Thanks @imadero for letting me know the procedure.

Not sure if you have gotten the answer you need yet, but I though I would share this Scripted field code that calculates the difference between two date fields in working days. You should be able to adapt this fairly easily to execute workingDays() for each time period the issue is in the status of interest.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField
import org.apache.log4j.Level
import org.apache.log4j.Logger
import java.lang.Integer
import com.atlassian.jira.datetime.DateTimeFormatter
import java.time.*
import java.time.temporal.*
final Logger log = Logger.getLogger("com.joby.melville")
log.setLevel(Level.WARN)

final customFieldManager = ComponentAccessor.getCustomFieldManager()
final startDateFieldName = 'Start date'
final endDateFieldName = 'End date'

def long workingDays(start, end) {
    def cdays = ChronoUnit.DAYS.between(start, end) + 1
    // remove weekends
    def wdays = cdays - 2 * (cdays / 7 as Integer) 
    // accommodate remainder days
    if (cdays % 7 != 0) {
        def startDayOfWeek = start.getDayOfWeek().getValue()
        def endDayOfWeek = end.getDayOfWeek().getValue()
        log.debug ("StartD: $startDayOfWeek EndD: $endDayOfWeek")
        if (startDayOfWeek == 7 || endDayOfWeek == 7) // Both cannot be Sunday or remainder would be zero
            wdays -= 1
        else if (endDayOfWeek < startDayOfWeek)       // Another weekend is included
            wdays -= 2
    }
    log.debug ("cdays: $cdays wdays: $wdays")
    return wdays
}
def startDateCF = customFieldManager.getCustomFieldObjects(issue).findByName(startDateFieldName)
def endDateCF = customFieldManager.getCustomFieldObjects(issue).findByName(endDateFieldName)
if (!startDateCF or !endDateCF)
    return null
def startDate = issue.getCustomFieldValue(startDateCF)
def endDate = issue.getCustomFieldValue(endDateCF)
log.debug("Start date: ${startDate} End date: ${endDate}")
if (!startDate || !endDate || endDate.before(startDate)) {
    return null
}
return workingDays(startDate.toLocalDateTime(), endDate.toLocalDateTime())
1 Like

Perfectly Worked @chris.melville
I will update with status dates.
Thanks

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.