Here is one I modified from a version found on the Internet some time ago...
/* REXX
USAGE:
>>----- timefunc(func,--parm1--+--------+------><
| |
+-,parm2-+
Valid function codes:
TS - Convert time to seconds (single day limit)
ST - Convert seconds to time (multi-day allowed)
TD - Calculate difference between two times (24 hour limit)
SD - Calculate diff in seconds between two times (24 hour limit)
Results returned:
TS - The value of input parm1 in seconds
ST - The time value (hh:mm:ss+dd) of parm1
TD - The difference (hh:mm:ss) between parm1(lower) and parm2
SD - The seconds difference (nnnnn) between parm1(lower) and parm2
*/
Trace o;
Parse Upper Arg func,p1,p2,.;
Call Init;
Call ValidParms;
If err = "000000" Then
Select
When func = "TS" Then tf = TS(h1,m1,s1);
When func = "ST" Then tf = ST(p1);
When func = "TD" Then tf = TD("T");
When func = "SD" Then tf = TD("S");
End
Else
tf = "!"err;
Return tf;
/*--------------------------------------------------------------------*
* All Subroutines start here *
*--------------------------------------------------------------------*/
/*----------------------------------------------------------*
* Initialize variables *
*----------------------------------------------------------*/
Init:
Parse Value "00 00 00 00 00 00",
With hh1 mm1 ss1 hh2 mm2 ss2;
err = "000000";
Return;
/*----------------------------------------------------------*
* Validate function and parms *
* Error code ff1122 *
* where ff is the function error code *
* 11 is the error code for parm 1 *
* 22 is the error code for parm 2 *
*----------------------------------------------------------*/
ValidParms:
If Wordpos(func,"TS ST TD SD") > 0 Then /* validate function */
Do;
If (func = "TD") | (func = "SD") | (func = "TS") Then
Do;
Parse Value p1 with t1 " +" d1 "D";
Parse Value Valitime(t1) with rc +2 +1 h1 +2 m1 +2 s1;
err = Bitor(err,Right(rc,4,"0"));
End;
If (func = "TD") | (func = "SD") Then
Do;
Parse Value p2 with t2 " +" d2 "D";
Parse Value Valitime(t2) with rc +2 +1 h2 +2 m2 +2 s2;
err = Bitor(err,Right(rc,6,"0"));
End;
If (func = "ST") Then
If Datatype(p1,"W") = 0 Then
err = Bitor(err,"0031"); /* Not Numeric */
Else
If p1 = 0 Then
err = Bitor(err,"0032");/* Not greater than zero */
End; /* Do */
Else
err = Bitor(err,"11");
Return;
/*----------------------------------------------------------*
* Time to Seconds (Single Day) *
*----------------------------------------------------------*/
TS:
Return (((arg(1) * 60) + arg(2)) * 60) + arg(3);
/*----------------------------------------------------------*
* Seconds to Time (Multi Day) *
*----------------------------------------------------------*/
ST:
sec = Arg(1);
Do days = 0 While sec > 86399; sec = sec - 86400; End; /* num days*/
Return Right(sec % 3600,2,0) || ":" ||,
Right(((sec // 3600) % 60),2) || ":" ||,
Right(((sec // 3600) // 60),2) || " +"days"D";
/*----------------------------------------------------------*
* Time Difference (24 hour max) *
*----------------------------------------------------------*/
TD:
sec = TS(h2,m2,s2) - TS(h1,m1,s1);
sec = IIF(sec > 0,sec,sec + 86400);
Return IIF(Arg(1) = "T",ST(sec),sec);
This uses an external call that I use quite often called IIF:
/*- REXX -------------------------------------------------------------*
* Name : IIF *
* Function : Function to return one of two values, based on condition*
* Author : Don Johnson *
* Params. : Result of condition, value if true, value if false *
* Called by: Function call from REXX programs *
* Calls : Nothing else. *
* Changes : *
* DEJ 15 Sep 2009 Initial Version. *
* *
*--------------------------------------------------------------------*
* Processing - This function will test for the existence of the first*
* argument. This argument will be the result of a condition that is *
* evaluated at the point of the function call, and will be 0 or 1. *
* If arg 1 exists, and if it is 1 (true), then the value of arg 2 is *
* returned; otherwise the value of arg 3 is returned. *
* If arg 1 is omitted, the condition is assumed true and the value *
* of arg 2 is returned. *
*--------------------------------------------------------------------*/
If ((Arg(1, 'E')) & (Arg(1) = 1)) | (Arg(1, 'O')) Then
Return Arg(2) ;
Else
Return Arg(3);
Good luck!
*don*
On Fri, Dec 3, 2010 at 7:26 AM, Baraniecki, Ray <
[email protected]> wrote:
> Does anyone have a simple Rexx routine to calculate time duration? I have
> the start time and the end time and would like to obtain the duration. I
> realized (too late) that this is not just a simple math exercise.
>
> Thanks;
>
>
> Ray Baraniecki
> Morgan Stanley Smith Barney
> 1 New York Plaza - 18th Floor
> New York, NY 10004
> Telephone: 212-276-5641
> Cell: 917-597-5692
>
>
>
> --------------------------------------------------------------------------
> Important Notice to Recipients:
>
> The sender of this e-mail is an employee of Morgan Stanley Smith Barney
> LLC. If you have received this communication in error, please destroy all
> electronic and paper copies and notify the sender immediately. Erroneous
> transmission is not intended to waive confidentiality or privilege. Morgan
> Stanley Smith Barney reserves the right, to the extent permitted under
> applicable law, to monitor electronic communications. This message is
> subject to terms available at the following link:
> http://www.morganstanley.com/disclaimers/mssbemail.html. If you cannot
> access this link, please notify us by reply message and we will send the
> contents to you. By messaging with Morgan Stanley Smith Barney you consent
> to the foregoing.
>
> ----------------------------------------------------------------------
> For IBM-MAIN subscribe / signoff / archive access instructions,
> send email to [email protected] with the message: GET IBM-MAIN INFO
> Search the archives at http://bama.ua.edu/archives/ibm-main.html
>
----------------------------------------------------------------------
For IBM-MAIN subscribe / signoff / archive access instructions,
send email to [email protected] with the message: GET IBM-MAIN INFO
Search the archives at http://bama.ua.edu/archives/ibm-main.html