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

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

0 votes
Answer accepted
Antoine Berry Community Leader 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 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 Oct 30, 2019

Glad you found a solution ! 

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

Antoine Berry Community Leader Oct 30, 2019

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

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...

233 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