Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Calculation of the sum of two time custom fields

Peter Kaufmann October 23, 2019

Hi community,

I'm trying to calculate the sum of two custom fields with a time entry like: 1w 5d 3h 2m and 13w 3d 19h 58m.

Here my code:

import com.atlassian.jira.component.ComponentAccessor;

def customField1 = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_11111");
def customField2 = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_22222");

def value1 = (String)issue.getCustomFieldValue(customField1);
def value2 = (String)issue.getCustomFieldValue(customField2);

return value1 + value2

 

But the result is: 1w 5d 3h 2m13w 3d 19h 58m

How can I get the right sum of: 15w 1d 23h?

 

Greetings,

Peter

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
Antoine Berry
Community Champion
October 24, 2019

Hi @Peter Kaufmann ,

Since your are using string values, you would have to create a custom function to sum on them.

Peter Kaufmann October 24, 2019

Hi @Antoine Berry ,

okay...and how do I do that? Could you give me an example, please!

Thank you!

Antoine Berry
Community Champion
October 28, 2019

Hi @Peter Kaufmann ,

I am not the best with regexps, but had a little fun with this case and it seems to work as expected : 

private String sumTimes(String timeLogged1, String timeLogged2){
def minsCatcher = '[0-9]*(?=m)'
def hoursCatcher = '[0-9]*(?=h)'
def daysCatcher = '[0-9]*(?=d)'
def weeksCatcher = '[0-9]*(?=w)'

def nbMins1 = (timeLogged1 =~ minsCatcher).find()?(timeLogged1 =~ minsCatcher):"0"
def nbHours1 = (timeLogged1 =~ hoursCatcher).find()?(timeLogged1 =~ hoursCatcher):"0"
def nbDays1 = (timeLogged1 =~ daysCatcher).find()?(timeLogged1 =~ daysCatcher):"0"
def nbWeeks1 = (timeLogged1 =~ weeksCatcher).find()?(timeLogged1 =~ weeksCatcher):"0"

def nbMins2 = (timeLogged2 =~ minsCatcher).find()?(timeLogged2 =~ minsCatcher):"0"
def nbHours2 = (timeLogged2 =~ hoursCatcher).find()?(timeLogged2 =~ hoursCatcher):"0"
def nbDays2 = (timeLogged2 =~ daysCatcher).find()?(timeLogged2 =~ daysCatcher):"0"
def nbWeeks2 = (timeLogged2 =~ weeksCatcher).find()?(timeLogged2 =~ weeksCatcher):"0"

int nbMinsTotal = (nbMins1[0]==""?0:nbMins1[0].toInteger()) + (nbMins2[0]==""?0:nbMins2[0].toInteger())
int nbHoursTotal = (nbHours1[0]==""?0:nbHours1[0].toInteger()) + (nbHours2[0]==""?0:nbHours2[0].toInteger())
int nbDaysTotal = (nbDays1[0]==""?0:nbDays1[0].toInteger()) + (nbDays2[0]==""?0:nbDays2[0].toInteger())
int nbWeeksTotal = (nbWeeks1[0]==""?0:nbWeeks1[0].toInteger()) + (nbWeeks2[0]==""?0:nbWeeks2[0].toInteger())

int finalNbMins = nbMinsTotal%60
int nbHoursToAdd = nbMinsTotal/60
int finalNbHours = (nbHoursTotal + nbHoursToAdd)%24
int nbDaysToAdd = (nbHoursTotal + nbHoursToAdd)/24
int finalNbDays = (nbDaysTotal + nbDaysToAdd)%7
int nbWeeksToAdd = (nbDaysTotal + nbDaysToAdd)/7
int finalNbWeeks = nbWeeksTotal + nbWeeksToAdd

String finalTime = ((finalNbWeeks!=0)?finalNbWeeks+"w ":"")+((finalNbDays!=0)?finalNbDays+"d ":"")+((finalNbHours!=0)?finalNbHours+"h ":"")+((finalNbMins!=0)?finalNbMins+"m":"")
return finalTime.trim()
}

Let me know if that works for you !

Antoine

Peter Kaufmann October 30, 2019

Dear Antoine,

thanks for your help. In the meantime I got a simple code. I convert the time in seconds and than I calculated the sum. After this, I convert it back with:

return DateUtils.getDurationString(sum)

Now it is working. Nevertheless, thank you Antoine.

 

Best regards,

Peter

Antoine Berry
Community Champion
October 30, 2019

Glad you found a solution ! 

Is the output formatted as well ? (e.g. 4w 5d 3h 2m)

Peter Kaufmann October 30, 2019

Yes, it is!

Antoine Berry
Community Champion
October 30, 2019

Nice, glad you shared, I did not know about that one. :)

TAGS
AUG Leaders

Atlassian Community Events