Elapsed Time Example Involving Different Days (115989)



The information in this article applies to:

  • Microsoft Visual FoxPro for Windows 3.0
  • Microsoft FoxPro for MS-DOS 2.0
  • Microsoft FoxPro for MS-DOS 2.5
  • Microsoft FoxPro for MS-DOS 2.5a
  • Microsoft FoxPro for MS-DOS 2.5b
  • Microsoft FoxPro for MS-DOS 2.6
  • Microsoft FoxPro for Windows 2.5
  • Microsoft FoxPro for Windows 2.5a
  • Microsoft FoxPro for Windows 2.5b
  • Microsoft FoxPro for Windows 2.6
  • Microsoft FoxPro for Macintosh 2.5b
  • Microsoft FoxPro for Macintosh 2.5c

This article was previously published under Q115989

SUMMARY

The function shown below determines the number of seconds that have elapsed between two different times. This function will take into account switching to the next day.

MORE INFORMATION

   PROCEDURE elaptime
   PARAMETER m.startdate, m.enddate, m.starttime, m.endtime
   * pass startdate, an 8-character starttime (HH:MM:SS), an enddate,
   * and an 8-character endtime
   * perform parameter type checking: return value of -1 indicates
   * invalid parameter type
   * Example of using the function elaptime:
   * TempVariable=ElapTime({01/01/94},{01/02/94},"00:01:00",00:01:00")

   IF TYPE('m.startdate') # "D" .OR. ;
       TYPE('m.enddate') # "D" .OR. ;
       (TYPE('m.starttime') # "C" .AND. LEN(m.starttime) # 8) .OR. ;
       (TYPE('m.endtime') # "C" .AND. LEN(m.endtime) # 8)
       RETURN -1
   ENDIF

   * if startdate > enddate, or same date and starttime > endtime,
   * return 0
   IF m.startdate > m.enddate .OR. (m.startdate = m.enddate .AND. ;
        m.starttime > m.endtime)
       RETURN 0
   ENDIF

   #DEFINE secsperday 86400

   * determine whether the endtime is earlier or later than the
   * starttime
   IF m.endtime >= m.starttime
       m.elapsecs = (m.enddate - m.startdate) * secsperday + ;
           (time2secs(m.endtime) - time2secs(m.starttime))
   ELSE
       m.elapsdays = 0
       IF m.enddate - m.startdate > 0
           m.elapsdays = m.enddate - m.startdate - 1
       ENDIF
       m.elapsecs = (m.elapsdays * secsperday) + ;
           (secsperday - time2secs(m.starttime)) + ;
           time2secs(m.endtime)
   ENDIF

   RETURN m.elapsecs


   FUNCTION time2secs
   * passed a C8 HH:MM:SS string
   * returns the number of seconds into the day this is
   PARAMETER m.passedtime

   m.retsecs = VAL(RIGHT(m.passedtime, 2)) + ;
       60 * VAL(SUBSTR(m.passedtime, 4, 2)) + ;
       3600 * VAL(LEFT(m.passedtime, 2))
   RETURN m.retsecs
				

Modification Type:MajorLast Reviewed:12/3/2003
Keywords:kbcode KB115989