Hello Fitzroy!
We have approached this as follows. We have a standalone script called DURATION that takes passed parameters and then populates a target field (in this case, ESTDUR) with a calculated value per our business rules.
Sample code to invoke the script looks like this:
from java.util import HashMap;
## get a hash-map to pass values to duration script
ctx = HashMap();
## determine how many milliseconds are between the estimated dates
## we use target for this calculation
time_diff = mbo.getDate("TARGCOMPDATE").getTime() - mbo.getDate("TARGSTARTDATE").getTime()
## put values into the hash-map
ctx.put("p_diff", time_diff);
ctx.put("duration_field", "ESTDUR");
ctx.put("mbo", mbo);
## call the duration script
service.invokeScript("DURATION", ctx);
The actual script body looks like this:
## determine the time differential from passed parameters
has_duration = False;
X = 0;
## first, see if we were passed a calculated millisecond value...
try:
if (p_diff != None):
X = p_diff;
has_duration = True;
except: xx = 1; ## catch and swallow exception
## next, see if we were passed start and finish values...
try:
if (p_start != None and p_finish != None):
X = p_finish.getTime() - p_start.getTime();
has_duration = True;
except: xx= 1; ## catch and swallow exception
## values for actually calculating duration from a time differential
elapsedhours = 0;
elapsedminutes = 0;
minuteinmillis = 1000 * 60;
hourinmillis = 1000 * 60 * 60;
## do the calculation
elapsedhours = X / hourinmillis
X = X - elapsedhours * hourinmillis
elapsedminutes = X / minuteinmillis
## if there is a duration, create in HH:MM format (front-fill minutes with 0 as needed)
if (has_duration): p_duration = str(elapsedhours) + ":" + str(elapsedminutes).zfill(2);
## if there is a start, but no finish
elif (p_start != None and p_finish == None): p_duration = "ONGOING";
## if there is no start
elif (p_start == None and p_finish == None): p_duration = "NOT STARTED";
## default to empty
else: p_duration = "";
try:
if (duration_field != None):
mbo.setValue(duration_field, p_duration, 11L);
except: x = 1; ## catch and swallow exception
Hopefully this is helpful!
--Jade