Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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 Lobato Qualisoni

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
Community showcase
Published in Marketplace Apps & Integrations

Staying organized with Jira: best practices for a better project management

Project managers know this problem: A “mountain of work” lays in front of you, and you don’t know how and where to tackle them. Different to-dos lie ahead, but just one task after the other can be ha...

247 views 2 1
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you