Announcement

Collapse
No announcement yet.

Checking the timer after PC's midnight jump

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

  • Checking the timer after PC's midnight jump

    Hello Community!

    My question:

    LOCAL mytimeVar AS SINGLE
    mytimeVar = TIMER

    later on:

    IF TIMER > mytimeVar THEN
    'do something
    END IF

    All this works fine until the machine jumps over midnight.
    Then TIMER is always of lower value than mytimeVar created before midnight.
    The results are inpredictible.

    Is there any standard-practice to overcome that problem?
    Maybe GetTickCount....

    Thank You for help as always and greetings from Germany
    Heinz Grandjean
    Heinz Grandjean
    http://www.estwgj.com/

  • #2
    Solution: Don't use TIMER.

    Try something like FILETIME in the Win API, it is a 64bit value that will always get bigger. Only issues you may need to deal with there are Timezone and DST issues. There are also other APIs to get things like System Uptime and such that would work too.
    sigpic
    Mobile Solutions
    Sys Analyst and Development

    Comment


    • #3
      What is the longest stretch your app might run?

      GetTickCount does have a 'rollover' point, but that is 49.7 days since your computer booted up.

      The best bet for timing over longer periods may be to get the local time into a QUAD variable, and then re-check the actual time and get it into a QUAD for timing purposes.

      Edit: Looks like Roger beat me to it
      Adam Drake
      PowerBASIC

      Comment


      • #4
        The best bet for timing over longer periods may be to get the local time into a QUAD variable, and then re-check the actual time and get it into a QUAD for timing purposes.


        Waitable Timer Object Demo June 2005
        Michael Mattias
        Tal Systems (retired)
        Port Washington WI USA
        [email protected]
        http://www.talsystems.com

        Comment


        • #5
          GetLocalTime() + SystemTimeToFileTime() gives you actual time in nS...
          Regards,
          Peter

          Comment


          • #6
            If you have the latest PB Compiler (8/4), you can use this line of code to drop a FILETIME UDT into a QUAD:

            Code:
            DIM q AS QUAD
            DIM ft AS FILETIME
            q = MAK(QUAD, ft.dwLowDateTime, ft.HighDateTime)
            Adam Drake
            PowerBASIC

            Comment


            • #7
              Another way:

              LOCAL mytimeVar AS SINGLE, test as single
              mytimeVar = TIMER

              later on:

              test = abs(timer-mytimevar)
              IF TEST THEN
              'do something
              END IF
              "There are two novels that can change a bookish fourteen-year old's life: The Lord of the Rings and Atlas Shrugged. One is a childish fantasy that often engenders a lifelong obsession with its unbelievable heroes, leading to an emotionally stunted, socially crippled adulthood, unable to deal with the real world. The other, of course, involves orcs." - John Rogers

              Comment


              • #8
                Here's another
                Code:
                LOCAL mytimeVar AS DOUBLE
                mytimeVar = TIMER
                 
                'later on:
                IF TIMER - mytimeVar < 0 THEN
                  'do something after midnight
                  'elapsed time = TIMER - mytimeVar + 86400
                END IF
                Last edited by Dave Biggs; 5 Feb 2008, 06:40 PM. Reason: correction
                Rgds, Dave

                Comment


                • #9
                  Here is another variant.
                  You won't have to care about those
                  "jumps over midnight" before year 28,880

                  Code:
                   
                   LOCAL qUtc1 AS QUAD
                   LOCAL qUtc2 AS QUAD
                   GetSystemTimeAsFileTime(BYVAL VARPTR(qUtc1)) '100-nanosecond intervals since January 1, 1601
                   SLEEP 1234
                   GetSystemTimeAsFileTime(BYVAL VARPTR(qUtc2)) '100-nanosecond intervals since January 1, 1601
                   MessageBox %HWND_DESKTOP, FORMAT$((qUtc2 - qUtc1) / 10000000, "###,###.###") & _
                                             " second elapsed", "Time elapsed", 64
                  Last edited by Pierre Bellisle; 5 Feb 2008, 08:05 PM.

                  Comment


                  • #10
                    I usually just check for the time and if within the timer length, I make that the delay by waiting for midnight and skipping the timer for that one time.
                    Barry

                    Comment


                    • #11
                      Thank You

                      Hello Community!
                      After studying carefully all Your solutions the real way for me
                      seems the WIN-API's GetSystemTimeAsFileTime().
                      But if I understood correctly, the disadvantage may be, that I have to change the myUDT.myTimeVar that normally contains the individual time-values into a QUAD-type.
                      Therefore I decided to use GetTickCount which is - as far as I am right (?)-
                      the smaller brother of GetSystemTimeAsFileTime().
                      The application normally runs only some hours (maybe over midnight) and will never overrun the 49.7 days of GetTickCount.

                      Again thank You for help (I have learned much),
                      greetings from Germany,
                      Heinz Grandjean
                      Heinz Grandjean
                      http://www.estwgj.com/

                      Comment


                      • #12
                        What are you doing? Timing the run? Setting a clock on the screen? Waiting for some particular time?

                        Is this a GUI program? A console program?

                        What would subjectively be the "best" way to handle your challenge depends on what you want to accomplish.

                        Me, just looking at code which says "IF TIMER > MyTimeVar THEN... ", it sure looks like you are waiting for some time certain, in which case I'd certainly recommend the WaitableTimerObject approach.

                        If you are timing the run from start to end, and maybe at certain points along the way, I'd just get my starting and ending (and intermediate) times into SYSTEMTIME structures (GetLocalTime()) and do the math when required.
                        Michael Mattias
                        Tal Systems (retired)
                        Port Washington WI USA
                        [email protected]
                        http://www.talsystems.com

                        Comment


                        • #13
                          Curiously enough my current project requires me to format elapsed time.

                          I do this...

                          Code:
                          TYPE MonitorProcParamType
                              hEventReady    AS LONG      ' when thread function is launched
                              ftStart        AS FILETIME  '  filetime when this starts (from GetLocalTime)
                              bShowWindow    AS LONG  ' if true, the window is visible (%WS_VISIBLE set)
                          .......
                          
                          FUNCTION... 
                          
                          
                           LOCAL pqFT AS QUAD PTR, pqFTStart AS QUAD PTR
                           LOCAL st AS SYSTEMTIME, ft AS FILETIME, elapsed AS LONG, iTime AS LONG
                          
                          
                               ' put the start time into the parameter = current time
                               ' However, I think I should be using the return from the database as the time for elapsed?
                               ' elapsed time is based on this.
                          
                               GetLocalTime         lst
                               SystemTimeToFileTime lst, mp.ftStart
                          
                           .....
                          
                                    ' always update the elapsed time control
                                      GetLocalTime          st
                                      SystemTimeToFileTime  st, ft
                                      ' I need to get diff as quads
                          
                                      pqFT      = VARPTR(ft)
                                      pqFtStart = VARPTR(@pMP.ftStart)
                                      Elapsed    =  (@pQFT -  @pQFtStart ) \ 1E7   ' need elapsed in seconds
                                     ' format elapsed time into HH:MM:SS and put on the screen..
                                      CALL FormatSecondsHHMMSS (elapsed) TO szText
                                      SetDlgItemText hWnd, %ID_JOB_ELAPSED, szText
                          
                          
                          FUNCTION FormatSecondsHHMMSS (BYVAL ets AS LONG) AS STRING
                              ' convert seconds into HH:MM:SS  string (I get elapsed converted from filetime inline)
                              LOCAL hh AS LONG, mm AS LONG, ss AS LONG
                              LOCAL s AS STRING
                              LOCAL wet  AS LONG
                          
                             'hmm, 120 in gives me -1 for hh, 2 for mm, 0 for ss only hours is wrong
                          
                              wet  = ets
                          
                              ss   = wet MOD 60
                          
                              wet  = (Wet - ss) \ 60
                          
                              mm   = wet MOD 60    ' whole minutes
                          
                              hh   = wet \ 60       ' the hours portion of what's left
                          
                          
                          
                              'MSGBOX USING$ ("ets #  hh #  mm #  ss #", ets, hh, mm, ss)
                          
                                               ' this does not work  "0#:0#:0#" gets extra digit when ss>=10
                                               ' this does not work  "##:##:##" only one zero when value < 10
                              'FUNCTION = USING$("*0:*0:*0", hh,mm,ss) ' this it totally <vulgarity>  up.
                              ' OK, I give up I will do a replace
                          
                              S = USING$("##:##:##", hh,mm,ss)
                              REPLACE $SPC WITH "0" IN S
                              FUNCTION = S
                          
                          
                          END FUNCTION
                          That should be enough snippets to give you some ideas...

                          MCM
                          Michael Mattias
                          Tal Systems (retired)
                          Port Washington WI USA
                          [email protected]
                          http://www.talsystems.com

                          Comment


                          • #14
                            Hello Mr. Mattias,
                            I' ll try to answer.
                            The programme is a "real-time-application" controlling hardware-devices outside the computer. The amount of them can go up to 2.000 .
                            Each item gets commands from the applications main-thread: like set or reset position, like lock or unlock state and so on.
                            Each command is time-sensitive. A second thread is always controlling each item's individual TimeVar and and after checking a certain ellapse of time
                            (depends on individual settings: 10 sec. 20 sec. or longer) sends a corresponding command to the item and after that resets the item's timeVar.
                            Summing it up: it seems to be only a simple timing task.
                            The whole application is embedded in a DDT-GUI combined with GFX-Tools.
                            This works fine using PB Timer until the midnight change comes, then for some seconds it gets crazy, like I have explained above.
                            Thank You for Your interest,
                            Greetings,
                            Heinz Grandjean
                            Heinz Grandjean
                            http://www.estwgj.com/

                            Comment


                            • #15
                              after checking a certain ellapse of time (depends on individual settings: 10 sec. 20 sec. or longer) sends a corresponding command to the item and after that resets the item's timeVar.
                              Summing it up: it seems to be only a simple timing task.
                              SetTimer() ==> WM_TIMER message (or call a callback function) every "interval" , do your thing each time.

                              ????

                              MCM
                              Michael Mattias
                              Tal Systems (retired)
                              Port Washington WI USA
                              [email protected]
                              http://www.talsystems.com

                              Comment


                              • #16
                                49.7 days is not a limitation of how long your app runs, but how long your computer has been booted up. If you leave it on all the time, you will encounter that reset.
                                Adam Drake
                                PowerBASIC

                                Comment


                                • #17
                                  @ Mr. Drake: Thank you, I understand, the PC(s) are always switched on and off with the purpose of running that application; I forgot to express that.
                                  @Mr. Mattias: Oh, Yes, WM_TIMER is an alternative. Thanks for showing me the correct way.
                                  The application uses WM-Timer messages (by using SetTimer()) to process flashing indications inside the GUI's "control panel".
                                  I'll test it and give a short feedback.

                                  Greetings
                                  Heinz Grandjean
                                  Heinz Grandjean
                                  http://www.estwgj.com/

                                  Comment


                                  • #18
                                    When you explain the "what" (do something periodically) instead of simply asking for help with a predetermined "how" (use TIMER) you will get lots of good ideas.
                                    Michael Mattias
                                    Tal Systems (retired)
                                    Port Washington WI USA
                                    [email protected]
                                    http://www.talsystems.com

                                    Comment


                                    • #19
                                      RtlTimeToSecondsSince1970 may help
                                      "The trouble with quotes on the Internet is that you can never know if they are genuine." - Abraham Lincoln.

                                      Comment


                                      • #20
                                        Just another short &quot;How&quot;-question

                                        Hello community,

                                        as far, as I have seen: RtlTimeToSecondsSince1970 doesn't work
                                        under WIN98.
                                        Mr. Mattias solution according the SetTimer() works okay.

                                        The question:
                                        I understood that timers are limited objects.
                                        How many timers could be associated with one process simultaneously?

                                        Thanks and Greeting
                                        Heinz Grandjean
                                        Heinz Grandjean
                                        http://www.estwgj.com/

                                        Comment

                                        Working...
                                        X