Are you in the loop? Keep up with the latest by making sure you're subscribed to Community Announcements. Just click Watch and select Articles.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Calculation of the sum of two time custom fields

Edited

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 Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Oct 24, 2019

Hi @Peter Kaufmann ,

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

Hi @Antoine Berry ,

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

Thank you!

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Oct 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

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 Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Oct 30, 2019

Glad you found a solution ! 

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

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Oct 30, 2019

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

TAGS
AUG Leaders

Atlassian Community Events