## Case Duration ( x day(s) x hr(s) x min(s) x sec(s) )

Solved a problem in an elegant manner and want to show off your code? Know a hard-to-find feature? Post it here for the benefit of others. Questions don't belong here.
SteveH
Posts: 77
Joined: Thu May 02, 2013 2:54 pm
Has thanked: 4 times
Been thanked: 2 times

### Case Duration ( x day(s) x hr(s) x min(s) x sec(s) )

I built a Support Ticket case type in Proclaim which required an accurate Case Duration. As the life span of the cases in the case type were significantly shorter than that of a claim being run by a fee earner, using the CaseDuration maths field in Personal Injury simply wasn't enough, so I came up with this code to get me the case duration in days, hrs, mins and seconds.

To get this to work, you are going to need to create 2 new alpha fields.
â€¢ File Opened Time-Seconds
â€¢ File Closed Time-Seconds

When a case is opened, youâ€™ll need to run the following command to fill in the File Opened Time-Seconds alpha field. This should be in the same Maths field/Linked action where the PUT( TODAY , {File Opened Date.Date} ) is.

Code: Select all

``````PUT( TIME-SECONDS , {File Opened Time-Seconds.Text} )
``````
The same will apply to the File Closed Time-Seconds when you come to close the file.

The following code will provide you with the x day(s) x hr(s) x min(s) x sec(s).

Code: Select all

``````# CaseDurationDays
#
# Get the number of days case is opened
v-a = DAYS-FROM-TODAY( {File Opened Date.Date} )
IF {File Closed Date.Date} = "" OR {File Closed Date.Date} = ? THEN
v-b = DAYS-FROM-TODAY( TODAY )
ELSE
v-b = DAYS-FROM-TODAY( {File Closed Date.Date} )
END
#
v-c = v-b - v-a
# Convert minus figure to positive
IF v-c < 0 THEN
v-c = ( v-c ) * ( -1 )
END
#
# Get the time in Seconds
v-FOTS = {File Opened Time-Seconds.Text}
IF {File Closed Date.Date} = "" OR {File Closed Date.Date} = "?" THEN
v-FCTS = TIME-SECONDS
ELSE
v-FCTS = {File Closed Time-Seconds.Text}
END
#
v-TS1 = ( v-FCTS ) - ( v-FOTS )
#
# Calculate number of days in seconds
IF v-c >= 1 THEN
v-DaySec = ( 86400 )
v-Tdays = ( v-c ) * ( v-DaySec )
v-TS2 = ( v-TS1 ) + ( v-Tdays )
ELSE
v-TS2 = v-TS1
END
#
# Convert minus figure to positive
IF v-TS2 < 0 THEN
v-TS2 = ( v-TS2 ) * ( -1 )
END
#
#
# Calculate days
v-TSdays1 = ( v-TS2 ) / ( 60 )
v-TSdays2 = ( v-TSdays1 ) / ( 60 )
v-TSdays3 = ( v-TSdays2 ) / ( 24 )
v-TSdaysR = ROUND( v-TSdays3 , 0 , -1 )
# Calculate hrs
v-TShrs1 = ( v-TSdays3 ) - ( v-TSdaysR )
v-TShrs2 = ( v-TShrs1 ) * ( 24 )
v-TShrsR = ROUND( v-TShrs2 , 0 , -1 )
# Calculate mins
v-TSmins1 = ( v-TShrs2 ) - ( v-TShrsR )
v-TSmins2 = ( v-TSmins1 ) * ( 60 )
v-TSminsR = ROUND( v-TSmins2 , 0 , -1 )
# Calculate sec
v-TSsec1 = ( v-TSmins2 ) - ( v-TSminsR )
v-TSsec2 = ( v-TSsec1 ) * ( 60 )
v-TSsecR = ROUND( v-TSsec2 , 0 , -1 )
#
v-days = TEXT( v-TSdaysR ) + TEXT( " day(s)  " )
v-hrs = TEXT( v-TShrsR ) + TEXT( " hr(s)  " )
v-min = TEXT( v-TSminsR ) + TEXT( " min(s)  " )
v-sec = TEXT( v-TSsecR ) + TEXT( " sec(s)" )
v-text = TEXT( v-days ) + TEXT( v-hrs ) + TEXT( v-min ) + TEXT( v-sec )
#
PUT( v-text , {Ticket Duration.Text} )
#
# MESSAGE( v-text )
# MESSAGE( "v-a =" , v-a , NEWLINE , "v-b =" , v-b , NEWLINE , "v-c =" , v-c , NEWLINE , "v-FOTS =" , v-FOTS , NEWLINE , "v-FCTS" , v-FCTS , NEWLINE , "v-TS1 =" , v-TS1 , NEWLINE , "v-TS2 =" , v-TS2 , NEWLINE , "v-daysec =" , v-daysec , NEWLINE , "v-Tdays =" , v-Tdays , NEWLINE , "v-TSdays1 =" , v-TSdays1 , NEWLINE , "v-TSdays2 =" , v-TSdays2 , NEWLINE , "v-TSdays3 =" , v-TSdays3 , NEWLINE , "v-TSdaysR =" , v-TSdaysR , NEWLINE , "v-TShrs1 =" , v-TShrs1 , NEWLINE , "v-TShrs2 =" , v-TShrs2 , NEWLINE , "v-TShrsR =" , v-TShrsR , NEWLINE , "v-TSmins1 =" , v-TSmins1 , NEWLINE , "v-TSmins2 =" , v-TSmins2 , NEWLINE , "v-TSminsR =" , v-TSminsR , NEWLINE , "v-TSsec1 =" , v-TSsec1 , NEWLINE , "v-TSsec2 =" , v-TSsec2 , NEWLINE , "v-TSsecR =" , v-TSsecR , NEWLINE , NEWLINE , "v-text =" , v-text )
``````
Ok so to try and break it down:

# Get the number of days case is opened

Code: Select all

``````# Get the number of days case is opened
v-a = DAYS-FROM-TODAY( {File Opened Date.Date} )
IF {File Closed Date.Date} = "" OR {File Closed Date.Date} = ? THEN
v-b = DAYS-FROM-TODAY( TODAY )
ELSE
v-b = DAYS-FROM-TODAY( {File Closed Date.Date} )
END
#
v-c = v-b - v-a
# Convert minus figure to positive
IF v-c < 0 THEN
v-c = ( v-c ) * ( -1 )
END
``````
You may recognise this section of codeâ€¦ it is the code used in the â€˜CaseDurationâ€™ maths field in the Personal Injury case type that works out the number of days between 2 dates.

# Get the time in Seconds

Code: Select all

``````# Get the time in Seconds
v-FOTS = {File Opened Time-Seconds.Text}
IF {File Closed Date.Date} = "" OR {File Closed Date.Date} = "?" THEN
v-FCTS = TIME-SECONDS
ELSE
v-FCTS = {File Closed Time-Seconds.Text}
END
#
v-TS1 = ( v-FCTS ) - ( v-FOTS )
``````
Ok â€“ so this is where we start to use the File Opened Time-Seconds and File Closed Time-Seconds fields that we created. In this section we need to find the difference between the 2 time sets.

Take note, where the case isnâ€™t closed yet, then we use the system time as an alternative (similar to the PI Case Duration where days are calculated).

# Calculate number of days in seconds

Code: Select all

``````# Calculate number of days in seconds
IF v-c >= 1 THEN
v-DaySec = ( 86400 )
v-Tdays = ( v-c ) * ( v-DaySec )
v-TS2 = ( v-TS1 ) + ( v-Tdays )
ELSE
v-TS2 = v-TS1
END
#
# Convert minus figure to positive
IF v-TS2 < 0 THEN
v-TS2 = ( v-TS2 ) * ( -1 )
END
``````
In this bit we are using the days that we obtained at the beginning of the code to calculate the total days in seconds. We then use our time difference to get a total time in seconds between the start date and end date.

The v-DaySec bit that you see is calculated by:
60 = 60 (60 seconds in 1 minute)
60 x 60 = 3600 (3600 seconds in an hour)
3600 x 24 = 86400 (86400 seconds in 24 hours, or 1 day)

Iâ€™ve just placed this directly into a variable in Proclaim and skipped the calculation above. As 86400 will always be constant to equal 1 full day in seconds, we only need to use this to calculate the total number of days in seconds using the v-c variable we obtained earlier.

# Calculate days

Code: Select all

``````# Calculate days
v-TSdays1 = ( v-TS2 ) / ( 60 )
v-TSdays2 = ( v-TSdays1 ) / ( 60 )
v-TSdays3 = ( v-TSdays2 ) / ( 24 )
v-TSdaysR = ROUND( v-TSdays3 , 0 , -1 )
``````
So now we begin to break the seconds down to start calculating our Days, Hrs, Mins and Secs.

You divide the total number of seconds over the period by 60 to get minutes. Then 60 again to get the hours, then divide by 24 to get the days.

To provide a solid number, we then round the result down.

# Calculate hrs

Code: Select all

``````# Calculate hrs
v-TShrs1 = ( v-TSdays3 ) - ( v-TSdaysR )
v-TShrs2 = ( v-TShrs1 ) * ( 24 )
v-TShrsR = ROUND( v-TShrs2 , 0 , -1 )
``````
Using the previous calculations, we can now calculate our hrs. As we donâ€™t want the figure left of the decimal point, we minus the rounded off figure from the previous variable to that. In this case v-TShrs1 = ( v-TSdays3 ) - ( v-TSdaysR ).

Take note, that we arenâ€™t dividing our time any more, we are taking the remaining seconds from the previous calculation and then we are multiplying by 24 to get the hours.

Again, we round the result down to a solid number.

# Calculate mins

Code: Select all

``````# Calculate mins
v-TSmins1 = ( v-TShrs2 ) - ( v-TShrsR )
v-TSmins2 = ( v-TSmins1 ) * ( 60 )
v-TSminsR = ROUND( v-TSmins2 , 0 , -1 )
``````
Similar to before we minus the hours left of the decimal point against itself, we then multiply the result by 60 to get our minutes.

We also round the figure down for another solid number.

# Calculate sec

Code: Select all

``````# Calculate sec
v-TSsec1 = ( v-TSmins2 ) - ( v-TSminsR )
v-TSsec2 = ( v-TSsec1 ) * ( 60 )
v-TSsecR = ROUND( v-TSsec2 , 0 , -1 )
``````
Finally we minus the previous result to remove the mins, we then multiply what is left by 60 again to get our seconds, and again round it down.

# Piecing the final result together

Code: Select all

``````# Piecing the final result together
v-days = TEXT( v-TSdaysR ) + TEXT( " day(s)  " )
v-hrs = TEXT( v-TShrsR ) + TEXT( " hr(s)  " )
v-min = TEXT( v-TSminsR ) + TEXT( " min(s)  " )
v-sec = TEXT( v-TSsecR ) + TEXT( " sec(s)" )
v-text = TEXT( v-days ) + TEXT( v-hrs ) + TEXT( v-min ) + TEXT( v-sec )
``````
The final bit just pieces our results together to then give us our â€œx day(s) x hr(s) x min(s) x sec(s)â€.

I place the end result into an alpha field. Iâ€™ve done this just in case I need to report on this field later on. Using maths fields in reports can make reports run a little longer. If you wanted to, you could just use result = v-text if you werenâ€™t fussed about using a maths field directly on your screen.

Code: Select all

``````# MESSAGE( "v-a =" , v-a , NEWLINE , "v-b =" , v-b , NEWLINE , "v-c =" , v-c , NEWLINE , "v-FOTS =" , v-FOTS , NEWLINE , "v-FCTS" , v-FCTS , NEWLINE , "v-TS1 =" , v-TS1 , NEWLINE , "v-TS2 =" , v-TS2 , NEWLINE , "v-daysec =" , v-daysec , NEWLINE , "v-Tdays =" , v-Tdays , NEWLINE , "v-TSdays1 =" , v-TSdays1 , NEWLINE , "v-TSdays2 =" , v-TSdays2 , NEWLINE , "v-TSdays3 =" , v-TSdays3 , NEWLINE , "v-TSdaysR =" , v-TSdaysR , NEWLINE , "v-TShrs1 =" , v-TShrs1 , NEWLINE , "v-TShrs2 =" , v-TShrs2 , NEWLINE , "v-TShrsR =" , v-TShrsR , NEWLINE , "v-TSmins1 =" , v-TSmins1 , NEWLINE , "v-TSmins2 =" , v-TSmins2 , NEWLINE , "v-TSminsR =" , v-TSminsR , NEWLINE , "v-TSsec1 =" , v-TSsec1 , NEWLINE , "v-TSsec2 =" , v-TSsec2 , NEWLINE , "v-TSsecR =" , v-TSsecR , NEWLINE , NEWLINE , "v-text =" , v-text )
``````
The #â€™d bit of code at the very end is more for trouble shooting. If you feel that it isnâ€™t calculating properly, then remove the hash and let the code run. The message should display all the variables that have been used in the entire code allowing you to figure out where itâ€™s all gone wrong.

I also used this site http://www.timeanddate.com/date/timeduration.html to calculate the date and time to see if Proclaim has done it correctly. The site also does a brief breakdown to help check your figures.

SteveH