Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,365,071
Community Members
 
Community Events
168
Community Groups

Scriptrunner - Date math differences between environments

I'm having some trouble figuring out why the same code run from the Script Console in our Dev and Prod environments are yielding two different results. This section of code: 

latestDate = epicIssue.dueDate as Date
startColDt = earliestDate[0] as Date

return "Latest Date: " + latestDate + " minus Start Date: " + startColDt + " equals " + (latestDate - startColDt)

In our Dev environment, this returns a Day calculation: 
Latest Date: 2021-05-08 00:00:00.0 minus Start Date: 2021-02-15 00:00:00.0 equals 82

In Prod, this same exact code returns a milliseconds calculation: 
Latest Date: 2021-10-08 00:00:00.0 minus Start Date: 2021-03-08 00:00:00.0 equals 18486000000

I've crashed our prod server because the rest of the script now produces a table with 18 billion columns when it should only make a table with 82! We are running SR version 6.18 in Dev and 6.12 in Prod. Could this be the reason for the difference or is there somethign else? Is there something I can do to ensure that the result is formatted as days? 

 

Thanks!

1 answer

1 accepted

0 votes
Answer accepted
Hana Kučerová Community Leader Apr 15, 2021

Hi @John Hastings-Kimball ,

please, what is the definition of earliestDate?

Hana Kučerová Community Leader Apr 15, 2021

I think it will be the best if you paste here the whole code, if possible. Thank you.

Hana, 

Here is the whole code: 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter
import groovy.xml.MarkupBuilder

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.RendererManager
import java.sql.Timestamp
import java.time.temporal.ChronoUnit
import java.time.Duration


def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def issueService = ComponentAccessor.issueService
def searchService = ComponentAccessor.getComponent(SearchService)
def issueManager = ComponentAccessor.getIssueManager()
def cfm = ComponentAccessor.getCustomFieldManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def baseURL = ComponentAccessor.getApplicationProperties().getString("jira.baseurl")

//def issue = context.issue as Issue
def issue = issueManager.getIssueObject("VII-152")

//get jira project and Initiatives from
def jiraProjFld = cfm.getCustomFieldObject(18715)
def jiraProj = issue.getCustomFieldValue(jiraProjFld)

def impFld = cfm.getCustomFieldObjectsByName("Implementation")
def implementation = issue.getCustomFieldValue(impFld[0]) as Issue


// edit this query to suit
def query = jqlQueryParser.parseQuery("project ='" + jiraProj.name + "' AND issuetype = Epic AND 'Implementation' = " + issue.key + " ORDER BY 'Start Date' ASC")
def search = searchService.search(user, query, PagerFilter.getUnlimitedFilter())

if(search.results){
Date latestDate = new Date()
Date earliestDate = new Date()

//query is sorted by start date so grab the start date of the first issue
def firstEpicIssue = search.results[0] as Issue
def startFld = cfm.getCustomFieldObjectsByName("Start Date")
def startVal = firstEpicIssue.getCustomFieldValue(startFld[0]) as Date
if (startVal) {
earliestDate = startVal
}

//find the latest end date
search.results.each { epicObj ->
def epicIssue = epicObj as Issue
def endVal = epicIssue.dueDate as Date
if (endVal && endVal > latestDate) {
latestDate = endVal
}
}

return "Latest Date: " + latestDate + " minus Start Date: " + earliestDate + " equals " + (latestDate - earliestDate)
}

 

 

It looks like SR v6.18 updated groovy so i suspect this is the issue. 

Thanks

I'm not sure if you can subtract two dates like that "(latestDate - earliestDate)"

I always used Joda-Time in Scriptrunner to do arithmetic with dates, see below an example of how to get number of days between two dates.

https://kodejava.org/how-do-i-get-number-of-days-between-two-dates-in-joda/

Using Joda-Time you would need to do something as below:

 

//Import JodaTime
import org.joda.time.Days
import org.joda.time.DateTime

.... (your code)

int daysBetween = Days.daysBetween(new DateTime(latestDate) ,new DateTime(earliestDate)).getDays();

 

Hana Kučerová Community Leader Apr 17, 2021

Alternatively, a lot of people do something like:

def duration
use (groovy.time.TimeCategory) {
duration = (latestDate - earliestDate).days
}

Thank you @Hana Kučerová and @Italo _Modus Create_

Interestingly, we upgraded our production environment over the weekend and SR was updated to 6.19 and the above script is returning days now. I definitely suspect the update of groovy in 6.18 and later to have fixed this. I have also confirmed that your suggestions also work so i'll be sure to use one of them in the final code to be certain that i'm returning days going forward!

Like # people like this

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events