Some more Time-Functions

Post Reply
User avatar
ukoenig
Posts: 3981
Joined: Wed Dec 19, 2007 6:40 pm
Location: Germany
Contact:

Some more Time-Functions

Post by ukoenig »

Some more Time-Functions

Code: Select all


/* FUNCNAME     FT_ELAPMIN()
 *  Return difference, in minutes, between two mil format times.
 *  SYNTAX
 *     FT_ELAPMIN( <cTIME1>, <cTIME2> ) -> nMINUTES
 *  ARGUMENTS
 *     <cTIME1, cTIME2>  character strings of military form "hhmm",
 *         where 0<=hh<24.
 *  RETURNS
 *     <nMINUTES>
 *  DESCRIPTION
 *     Finds the arithmetic difference between time two times 
 *     (time 2 - time 1).
 *     If time 2 is smaller than time 1, a NEGATIVE value is returned.
 *  EXAMPLES
 *     FT_ELAPMIN( "1718", "2040" ) ->  322
 *     FT_ELAPMIN( "2040", "1718" ) -> -322
 */

FUNCTION FT_ELAPMIN(cTIME1,cTIME2)
RETURN ((VAL(LEFT(cTIME2,2))*60) + (VAL(RIGHT(cTIME2,2)))) - ;
         ((VAL(LEFT(cTIME1,2))*60) + (VAL(RIGHT(cTIME1,2))))

// -----------------------------------------------------------

/*  FUNCNAME     FT_ELTIME()
 *     Compute difference between times in hours, minutes, seconds.
 *  SYNTAX
 *     FT_ELTIME( <cTime1>, <cTime2> ) -> cDiff
 *  ARGUMENTS
 *     <cTime1, cTime2>  character strings representing times in
 *        hh:mm:ss format.
 *  RETURNS
 *     <cDiff>  character string representing time difference in
 *        hh:mm:ss format.
 *  DESCRIPTION
 *     Return the abs. difference between two times in hh:mm:ss format
 *     in character hours, minutes and seconds (hh:mm:ss).
 *  EXAMPLES
 *     FT_ELTIME( "22:40:12", "23:55:17" ) -> 01:15:05
 *     FT_ELTIME( "23:55:17", "22:40:12" ) -> 01:15:05
 */

FUNCTION FT_ELTIME(cTIME1,cTIME2)
LOCAL  nTIME1, nTIME2, nDELSECS, nHRS, nMINS, nSECS, nSECS1, nSECS2

nSECS1   := (val(substr(cTIME1,1,2)) * 3600) +;
              (val(substr(cTIME1,4,2)) * 60) + (val(substr(cTIME1,7)))
nSECS2   := (val(substr(cTIME2,1,2)) * 3600) +;
              (val(substr(cTIME2,4,2)) * 60) + (val(substr(cTIME2,7)))
nDELSECS := abs(nSECS2 - nSECS1)
nHRS     := int(nDELSECS / 3600)
nMINS    := int((nDELSECS - nHRS * 3600) / 60)
nSECS    := nDELSECS - (nHRS * 3600) - (nMINS * 60)

RETURN right("00" + ltrim(str(nHRS)),2) + ;
		":" + ;
		right("00" + ltrim(str(nMINS)),2) + ;
		":" + ;
		right("00" + ltrim(str(nSECS)),2)

// --------------------------------------------------------------

/*  FUNCNAME
 *     FT_MIN2DHM()
 *     Convert numeric minutes to days, hours and minutes.
 * SYNTAX
 *     FT_MIN2DHM( <nMinutes> ) -> aDHM_
 * ARGUMENTS
 *     <nMinutes>  the number of minutes.
 *  RETURNS
 *     <aDHM_>
 *        where: 
 *           aDHM_[1] = cDAYS, aDHM_[2] = cHours, aDHM_[3] = cMinutes
 *  DESCRIPTION
 *     Converts numeric minutes into a character array containing
 *     days, hours & minutes.
 *  EXAMPLES
 *     aDHM_ = MIN2DHM(16789) -> aDHM_[1] = 11, aDHM_[2] = 15,     
* aDHM_[3] = 49
 */

FUNCTION FT_MIN2DHM(nMINS)
LOCAL aDHM_[3]

aDHM_[1] = ltrim((str(int(nMINS/1440))))
aDHM_[2] = ltrim(str(int((nMINS%1440)/60)))
aDHM_[3] = ltrim(str(int((nMINS%1440)%60)))

RETURN aDHM_

// ---------------------------------------------------

/* FUNCNAME    FT_MIL2CIV()
 *     Convert time in military format to civilian format.
 *  SYNTAX
 *     FT_MIL2CIV( <cCIVTIME> ) -> dMILTIME
 *  ARGUMENTS
 *     <cMILTIME>  character string of form hhmm, where 0<=hh<24.
 *  RETURNS
 *     <cCIVTIME>  character string of form hh:mm (am,pm,n or m), 
 *        where 0<hh<12.
 *  DESCRIPTION
 *     Converts time from military to civilian format
 *  EXAMPLES
 *     FT_MIL2CIV( "1640" ) ->  4:40 pm
 *
 *     FT_MIL2CIV( "0440" ) ->  4:40 am
 *
 *     FT_MIL2CIV( "1200" ) -> 12:00 n
 *
 *     FT_MIL2CIV( "0000" ) and FT_MIL2CIV( "2400" ) -> 12:00 m
 *
 *     Caution:  leading blanks are irrelevant.
 */

FUNCTION FT_MIL2CIV(cMILTIME)
LOCAL cHRS,cMINS,nHRS,cCIVTIME

nHRS  := val(LEFT(cMILTIME,2))
cMINS := right(cMILTIME,2)

DO CASE
   CASE (nHRS == 24 .OR. nHRS == 0) .AND. (cMINS == "00")  // Midnight
      cCIVTIME = "12:00 m"
  CASE (nHRS == 12)                                       // Noon to 12:59pm
   IF cMINS == "00"
     cCIVTIME = "12:00 n"
  ELSE
     cCIVTIME = "12:" + cMINS + " pm"
  ENDIF
CASE (nHRS < 12)                                        // AM
   IF nHRS == 0
       cHRS = "12"
   ELSE
       cHRS = right("  " + ltrim(str(int(nHRS))),2)
   ENDIF
   cCIVTIME = cHRS + ":" + cMINS + " am"
OTHERWISE                                             // PM
   cCIVTIME = right("  " + ltrim(str(int(nHRS - 12))), 2) + ;
	":" + cMINS + " pm"
ENDCASE

RETURN cCIVTIME

// ------------------------------------

/*  FUNCNAME     FT_CIV2MIL()
 *     Convert usual civilian format time to military time.
 *  SYNTAX
 *     FT_CIV2MIL( <cCIVTIME> ) -> cMILTIME
 *  ARGUMENTS
 *     <cCIVTIME>  character string of form hh:mm (am,pm,n or m), 
 *        where 0<hh<12.
 *  RETURNS
 *     <cMILTIME>  character string of form hhmm, where 0<=hh<24.
 *  DESCRIPTION
 *     Converts time from 12-hour civilian format to military.
 *  EXAMPLES
 *     FT_CIV2MIL( " 5:40 pm" ) -> 1740
 *
 *     FT_CIV2MIL( " 5:40 am" ) -> 0540
 *
 *     FT_CIV2MIL( "12:00 n" ) -> 1200
 *
 *     FT_CIV2MIL( "12:00 m" ) -> 0000
 *     
 *     Caution:  leading blanks are irrelevant; p,a,n,m must be preceded by
 *               one and only one space.
 */

FUNCTION FT_CIV2MIL(cTIME)
LOCAL cKEY, cMILTIME

// Insure leading 0's
cTIME = REPLICATE("0", 3 - at(":", ltrim(cTIME))) + ltrim(cTIME)

// Adjust for popular use of '12' for first hour after noon and midnight
if left(ltrim(cTIME),2) == "12"
   cTIME = stuff(cTIME, 1, 2, "00")
endif

// am, pm, noon or midnight
cKEY = substr(ltrim(cTIME), 7, 1)

DO CASE
   CASE upper(cKEY) == "N"       // noon
       IF left(cTIME,2) + substr(cTIME,4,2) == "0000" 
	cMILTIME = "1200"
       ELSE
	cMILTIME = "    "
       ENDIF
   CASE upper(cKEY) == "M"         // midnight
      IF left(cTIME,2) + substr(cTIME,4,2) == "0000" 
	cMILTIME = "0000"
      ELSE
	cMILTIME = "    "
      ENDIF
   CASE upper(cKEY) == "A"           // am
     cMILTIME = right("00" + ltrim(str(val(left(cTIME,2)))),2) + ;
	 substr(cTIME,4,2)
   CASE upper(cKEY) == "P"           // pm
	cMILTIME = right("00" + ltrim(str(val(left(cTIME,2))+12)),2) + ;
	 substr(cTIME,4,2)
   OTHERWISE
       cMILTIME = "    "                   // error
ENDCASE

RETURN cMILTIME

// --------------------------------------------

/*  FUNCNAME     FT_SYS2MIL()
 *     Convert system time to military time format.
 *  SYNTAX
 *     FT_SYS2MIL() -> cMILTIME
 *  ARGUMENTS
 *     none
 *  RETURNS
 *     <cMILTIME>  character string of form hhmm, where 0<=hh<24.
 *  DESCRIPTION
 *     Return current system time as character string in military format.
 *  EXAMPLES
 *     FT_SYS2MIL() -> 1623
 */

FUNCTION FT_SYS2MIL()
RETURN left(stuff(time(),3,1,""),4)

Regards
Uwe :lol:
Since 1995 ( the first release of FW 1.9 )
i work with FW.
If you have any questions about special functions, maybe i can help.
Post Reply