Announcement

Collapse
No announcement yet.

Elapsed Time Function

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Elapsed Time Function

    I am trying to write an elapsed time sub that will display the
    time since a remembered start time. I have got seconds working
    but kind of got stuck on minutes, one thought was to convert
    all times to elapsed seconds then do the math to extrapolate
    Minutes and hours. I am drawing a blank on how to make this work.

    Note: I am not asking any one to write code, I will be more then
    happy to get a pseudo code strategy or even a hint on a better
    direction.

    Thanks
    Brian

    On a side note.. I am learning PB after years of VB experience.
    I just wrote an Excel Macro to pull data out of 10000 or so
    unique text files, it took about 45 minutes to run. I think I
    will rewrite it in PBCC to output to a comma del text file and
    see what the speed diff is. From what I have read here it
    should be significantly faster. Since this is something I will
    reuse in the future it should be worth it. Also I noticed that quite
    a few of my favorite functions I have written in VB copy over and
    run in PB without modification. Something that makes me very
    Happy
    BS


    '================
    'Start PB Dos Code
    '================

    'this is a test bas to try out the function and and see what
    ' it does. Once it works I will copy it to the main bas.

    DIM Start AS STRING
    DIM x AS INTEGER

    Start = TIME$

    FOR x = 0 TO 65
    CLS
    LOCATE 2,1
    DispTimeLeft Start, "09:00:00"
    SLEEP 1
    NEXT x
    PRINT "done"

    SUB DispTimeLeft(StartTime AS STRING)

    DIM CurTime AS STRING
    DIM CurHr AS INTEGER
    DIM CurMn AS INTEGER
    DIM CurSc AS INTEGER

    DIM StartHr AS INTEGER
    DIM StartMn AS INTEGER
    DIM StartSc AS INTEGER

    DIM DiffHr AS INTEGER
    DIM DiffMn AS INTEGER
    DIM DiffSc AS INTEGER

    'get current time
    CurTime = TIME$

    'Parse out Hours, Minutes and Seconds into int's
    CurHr = VAL(MID$(CurTime,1,2))
    CurMn = VAL(MID$(CurTime,4,2))
    CurSc = VAL(MID$(CurTime,7,2))

    StartHr = VAL(MID$(StartTime,1,2))
    StartMn = VAL(MID$(StartTime,4,2))
    StartSc = VAL(MID$(StartTime,7,2))

    'Calculates diff of start seconds to current Seconds
    IF CurSc>=StartSc THEN
    DiffSc = CurSc-StartSc
    ELSE
    DiffSc = (CurSc+60)-StartSc
    END IF

    'Calculate diff of start minutes to current minutes
    IF CurMn>=StartMn THEN
    DiffMn = CurMn-StartMn
    ELSE
    DiffMn = (CurMn+60)-StartMn
    END IF

    'NOTE: Not sure how to mix minutes and seconds, not to mention hours

    'Display results for now
    PRINT "Current = "+TRIM$(STR$(CurHR))+":"+TRIM$(STR$(CurMN))+":"+TRIM$(STR$(CurSC))
    PRINT "Start = "+TRIM$(STR$(StartHR))+":"+TRIM$(STR$(StartMN))+":"+TRIM$(STR$(StartSC))
    PRINT "Diffrence = "+TRIM$(STR$(DiffHR))+":"+TRIM$(STR$(DiffMN))+":"+TRIM$(STR$(DiffSC))

    'Other stuff to come later...

    END SUB
    '================
    'End PB Dos Code
    '================


    ------------------

  • #2
    File dateunit.bas in samples folder of pb-dos compiler:
    Code:
    =============================================================================
    '
    '  Date routines for PowerBASIC
    '  Copyright (c) 1995-97 by PowerBASIC, Inc.  All Rights Reserved.
    '     
    ...
    '  DateMath       -  Add/Subtract days to a given date   
    ...
       Elapsed        -  Return the number of seconds between two time strings
    Lots of other stuff in there, too.
    Michael Mattias
    Tal Systems Inc. (retired)
    Racine WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      Integer (or long) variables: n, hours, minutes, seconds.

      n = number of elapsed seconds
      (Use Timer function instead of Time$.)

      hours = n\3600
      minutes = (n - hours*3600)\60
      seconds = n - hours*3600 - minutes*60

      (The backwards slash is integer division.)

      [This message has been edited by Mark Hunter (edited May 13, 2006).]
      Politically incorrect signatures about immigration patriots are forbidden. Googling “immigration patriots” is forbidden. Thinking about Googling ... well, don’t even think about it.

      Comment


      • #4
        Something that is shorter mathematically, although probably not more efficient computationally is:

        hours = n \ 3600
        minutes = (n MOD 3600) \ 60
        seconds = n MOD 60


        I'm doing this from memory, but I think it works. Minutes is equal to the number of seconds not included in any hour (the modulus) divided by 60, and the seconds is the number of seconds that is not included in any full minute.

        ------------------




        [This message has been edited by Michael Torrie (edited May 13, 2006).]

        Comment


        • #5
          Yes, that gives the same result, and it’s easier to see the generalization:

          days = n \ 60*60*24
          hours = (n Mod 60*60*24) \ 60*60
          minutes = (n Mod 60*60) \ 60
          seconds = n Mod 60
          Politically incorrect signatures about immigration patriots are forbidden. Googling “immigration patriots” is forbidden. Thinking about Googling ... well, don’t even think about it.

          Comment


          • #6
            Here is what I finally got to work. Note I did not have
            access to Dateunit.lib but I did find Mark McDonalds Lib
            elsewhere on this site. which provided nSeconds that greatly
            simplified my life. Any suggestions to improve the following
            code is greatly appreciated.
            Thanks for all the Help
            Brian

            '----------------Start PB Code--------------
            $INCLUDE "R:\PB35\DBLIB\ELAPTIME\MLIB\MLIB.inc"

            '-----------------------------------
            'ElaseTime
            'Brian Sheely
            '05-15-2006
            'Inputs: StartTime = Start time to generate elapsed time from
            ' EndTime = End time to generate elapsed time from
            'Output: PB standard format time string "hh:mm:ss"
            'Returns the time elapsed from the start time to the end time.
            ' uses Mark McDonald's "nSeconds" Function from MLIB located at
            ' http://www.powerbasic.com/files/pub/pbdos/library/
            '-----------------------------------
            FUNCTION ElapseTime(StartTime AS STRING, EndTime AS STRING)AS STRING

            DIM DiffHr AS LONG
            DIM DiffMn AS LONG
            DIM DiffSc AS LONG

            DIM tempDbl AS DOUBLE

            DIM DispHr AS STRING
            DIM DispMn AS STRING
            DIM DispSc AS STRING

            DiffSc = nSeconds(StartTime, EndTime)

            tempDbl = DiffSc/60

            DiffMn = ParseDblToLong(tempDbl)
            DiffSc = DiffSc MOD 60

            tempDbl = ABS(DiffMn/60)

            DiffHr = ParseDblToLong(tempDbl)
            DiffMn = DiffMn MOD 60

            IF LEN(TRIM$(STR$(DiffHr))) < 2 THEN
            DispHr = "0"+TRIM$(STR$(DiffHr))
            ELSE
            DispHr = TRIM$(STR$(DiffHr))
            END IF

            IF LEN(TRIM$(STR$(DiffMn))) < 2 THEN
            DispMn = "0"+TRIM$(STR$(DiffMn))
            ELSE
            DispMn = TRIM$(STR$(DiffMn))
            END IF

            IF LEN(TRIM$(STR$(DiffSc))) < 2 THEN
            DispSc = "0"+TRIM$(STR$(DiffSc))
            ELSE
            DispSc = TRIM$(STR$(DiffSc))
            END IF

            FUNCTION = DispHr+":"+DispMn+":"+DispSc

            END FUNCTION

            '-----------------------------------
            'ParseDblToLong
            'Brian Sheely
            '05-15-2006
            'Inputs: DblIn = Double Float
            'Output: Long int of DblIn value with everything after the decimal removed
            ' Used to eliminate rounding errors associated with time string manipulation
            '-----------------------------------
            FUNCTION ParseDblToLong(DblIn AS DOUBLE) AS LONG

            DIM TempStr AS STRING
            DIM BuildStr AS STRING
            DIM x AS INTEGER

            TempStr = TRIM$(STR$(DblIn))

            'if the number is small enough, str adds "E-2" as an exponent representation
            ' Since I want whole numbers unrounded I can safely force the output to a
            ' zero for this application
            IF MID$(tempStr,17,1) = "E" THEN
            FUNCTION = 0
            EXIT FUNCTION
            END IF

            BuildStr = ""

            'Search for the decimal and discard all numerals after and
            ' including the decimal
            FOR x = 1 TO LEN(TempStr)
            IF MID$(TempStr,x,1)<>"." THEN
            BuildStr = BuildStr + MID$(TempStr,x,1)
            ELSE
            EXIT FOR
            END IF
            NEXT x

            FUNCTION = VAL(BuildStr)

            END FUNCTION

            '-----------------End PB Code----------------

            ------------------

            Comment


            • #7
              Keep it simple. The following is modified from a PBDOS program I once wrote.
              Code:
              Declare Function Normalize(Integer) As String
              Declare Function ElapseTime(String, String) As String
               
              'test program
              
              Dim a As String, b As String
               
              a = "05:12:58"
              b = "06:10:40"
               
              Cls
              Print a
              Print b
              Print ElapseTime(a, b)
              '---------------------------------------------------
               
              Function Normalize(n As Integer) As String
               If n < 10 Then
                Function = "0" + Trim$(Str$(n))
               Else
                Function =       Trim$(Str$(n))
               End If
              End Function
               
              Function ElapseTime(StartTime As String, EndTime As String) As String
               Dim hor0 As Integer, min0 As Integer, sec0 As Integer
               Dim hor1 As Integer, min1 As Integer, sec1 As Integer
               
               hor0 = Val(Mid$(starttime,1,2))
               min0 = Val(Mid$(starttime,4,2))
               sec0 = Val(Mid$(starttime,7,2))
               
               hor1 = Val(Mid$(endtime,  1,2))
               min1 = Val(Mid$(endtime,  4,2))
               sec1 = Val(Mid$(endtime,  7,2))
               
               If hor1 < hor0 Then      'if hours decreased, 
                Incr hor1,24            'assume went over just one day
               End If                   '(or could flag as an error, Function = "")
               
               If min1 < min0 Then      'if minutes decreased,
                Incr min1,60            'borrow 60 minutes from the hours
                Decr hor1
                If hor1 < 0 Then Function = "" :Exit Function   'flag as error
               End If
               
               If sec1 < sec0 Then      'if seconds decreased,
                Incr sec1,60            'borrow 60 seconds from the minutes
                Decr min1
                If min1 < 0 Then Function = "" :Exit Function    'flag as error
               End If
               
               Function = Normalize(hor1 - hor0) + ":" + Normalize(min1 - min0) + ":" + Normalize(sec1 - sec0)
              End Function
              [This message has been edited by Mark Hunter (edited May 15, 2006).]
              Politically incorrect signatures about immigration patriots are forbidden. Googling “immigration patriots” is forbidden. Thinking about Googling ... well, don’t even think about it.

              Comment

              Working...
              X