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
Hi @Peter Kaufmann ,
Since your are using string values, you would have to create a custom function to sum on them.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Glad you found a solution !
Is the output formatted as well ? (e.g. 4w 5d 3h 2m)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Nice, glad you shared, I did not know about that one. :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.