Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Getting Time Difference [modified]

Getting Time Difference [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
databasedebugginghelpquestion
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AmbiguousName
    wrote on last edited by
    #1

    hello guys...I made this small console app inwhich I can get the difference (duration) between two times. I then store them into database as string. But the problem is that sometimes it shows some weird difference which I am unable to trace. Here is what I have

        SYSTEMTIME t,t2,t3;
    
        GetSystemTime(&t);  //gets current time on button click
        GetSystemTime(&t2);  //gets current time on second button click after some time
    
        //Get the difference between two times
        t3.wHour = t2.wHour - t.wHour;
        t3.wMinute = t2.wMinute - t.wMinute;
    t3.wSecond = t2.wSecond - t.wSecond;
    
        //function to store time in SqlServer Database, inwhich I do some concatination and store time in DB
        //omitting those to keep things simple
    GetMyDateTime2(t,t2,t3);  
    

    But here is one of the durations that I got from this calculation

    2 3/3/2011 21:57:57 3/3/2011 21:58:3 0:1:65482_(duration)_ D:\Recordings\SecondRec.wav

    what can be the problem?? thnx

    modified on Friday, March 4, 2011 5:08 AM

    C C D A 4 Replies Last reply
    0
    • A AmbiguousName

      hello guys...I made this small console app inwhich I can get the difference (duration) between two times. I then store them into database as string. But the problem is that sometimes it shows some weird difference which I am unable to trace. Here is what I have

          SYSTEMTIME t,t2,t3;
      
          GetSystemTime(&t);  //gets current time on button click
          GetSystemTime(&t2);  //gets current time on second button click after some time
      
          //Get the difference between two times
          t3.wHour = t2.wHour - t.wHour;
          t3.wMinute = t2.wMinute - t.wMinute;
      t3.wSecond = t2.wSecond - t.wSecond;
      
          //function to store time in SqlServer Database, inwhich I do some concatination and store time in DB
          //omitting those to keep things simple
      GetMyDateTime2(t,t2,t3);  
      

      But here is one of the durations that I got from this calculation

      2 3/3/2011 21:57:57 3/3/2011 21:58:3 0:1:65482_(duration)_ D:\Recordings\SecondRec.wav

      what can be the problem?? thnx

      modified on Friday, March 4, 2011 5:08 AM

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      The problem is your calculations are wrong (do you remember 'carry'?) and the SYSTEMTIME fields are 16 bit WORDS. Try

      WORD wCarry;
      if (t2.wSecond >= t1.wSecond )
      {
      wCarry = 0;
      t3.wSecond = t2.wSecond - t1.wSecond;
      }
      else
      {
      wCarry = 1;
      t3.wSecond = 60 + t2.wSecond - t1.wSecond;
      }
      if (t2.wMinute >= t1.wMinute + wCarry)
      {
      wCarry = 0;
      t3.wMinute = t2.wMinute - t1.wMinute;
      }
      else
      {
      wCarry = 1;
      t3.wMinute = 60 + t2.wMinute - t1.wMinute;
      }
      if (t2.wHour >= t1.wHour + wCarry)
      {
      t3.wHour = t2.wHour - t1.wHour;
      }
      else
      {
      // handle error.
      }

      Please note the above code would have troubles on button clicks crossing the date boundary... :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      1 Reply Last reply
      0
      • A AmbiguousName

        hello guys...I made this small console app inwhich I can get the difference (duration) between two times. I then store them into database as string. But the problem is that sometimes it shows some weird difference which I am unable to trace. Here is what I have

            SYSTEMTIME t,t2,t3;
        
            GetSystemTime(&t);  //gets current time on button click
            GetSystemTime(&t2);  //gets current time on second button click after some time
        
            //Get the difference between two times
            t3.wHour = t2.wHour - t.wHour;
            t3.wMinute = t2.wMinute - t.wMinute;
        t3.wSecond = t2.wSecond - t.wSecond;
        
            //function to store time in SqlServer Database, inwhich I do some concatination and store time in DB
            //omitting those to keep things simple
        GetMyDateTime2(t,t2,t3);  
        

        But here is one of the durations that I got from this calculation

        2 3/3/2011 21:57:57 3/3/2011 21:58:3 0:1:65482_(duration)_ D:\Recordings\SecondRec.wav

        what can be the problem?? thnx

        modified on Friday, March 4, 2011 5:08 AM

        C Offline
        C Offline
        Cool_Dev
        wrote on last edited by
        #3

        If you can use CTime and CTimeSpan MFC classes, no need to bother about calculations.

        1 Reply Last reply
        0
        • A AmbiguousName

          hello guys...I made this small console app inwhich I can get the difference (duration) between two times. I then store them into database as string. But the problem is that sometimes it shows some weird difference which I am unable to trace. Here is what I have

              SYSTEMTIME t,t2,t3;
          
              GetSystemTime(&t);  //gets current time on button click
              GetSystemTime(&t2);  //gets current time on second button click after some time
          
              //Get the difference between two times
              t3.wHour = t2.wHour - t.wHour;
              t3.wMinute = t2.wMinute - t.wMinute;
          t3.wSecond = t2.wSecond - t.wSecond;
          
              //function to store time in SqlServer Database, inwhich I do some concatination and store time in DB
              //omitting those to keep things simple
          GetMyDateTime2(t,t2,t3);  
          

          But here is one of the durations that I got from this calculation

          2 3/3/2011 21:57:57 3/3/2011 21:58:3 0:1:65482_(duration)_ D:\Recordings\SecondRec.wav

          what can be the problem?? thnx

          modified on Friday, March 4, 2011 5:08 AM

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          overloaded Name wrote:

          hello guys...I made this small console app inwhich I can get the difference (duration) between two times.

          You might also check out difftime().

          "One man's wage rise is another man's price increase." - Harold Wilson

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          "Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather

          1 Reply Last reply
          0
          • A AmbiguousName

            hello guys...I made this small console app inwhich I can get the difference (duration) between two times. I then store them into database as string. But the problem is that sometimes it shows some weird difference which I am unable to trace. Here is what I have

                SYSTEMTIME t,t2,t3;
            
                GetSystemTime(&t);  //gets current time on button click
                GetSystemTime(&t2);  //gets current time on second button click after some time
            
                //Get the difference between two times
                t3.wHour = t2.wHour - t.wHour;
                t3.wMinute = t2.wMinute - t.wMinute;
            t3.wSecond = t2.wSecond - t.wSecond;
            
                //function to store time in SqlServer Database, inwhich I do some concatination and store time in DB
                //omitting those to keep things simple
            GetMyDateTime2(t,t2,t3);  
            

            But here is one of the durations that I got from this calculation

            2 3/3/2011 21:57:57 3/3/2011 21:58:3 0:1:65482_(duration)_ D:\Recordings\SecondRec.wav

            what can be the problem?? thnx

            modified on Friday, March 4, 2011 5:08 AM

            A Offline
            A Offline
            Andrew Brock
            wrote on last edited by
            #5

            By far, the simplest way is GetSystemTimeAsFileTime[^]. This returns a FILETIME[^] struct which contians 2 DWORDs which you can then drop directly into a ULARGE_INTEGER[^].

            FILETIME ftStart, ftEnd; //Increments every 100 nanoseconds
            GetSystemTimeAsFileTime(&ftStart);
            //do something
            GetSystemTimeAsFileTime(&ftEnd);
            ULARGE_INTEGER liStart = { ftStart.dwLowDateTime, ftStart.dwHighDateTime };
            ULARGE_INTEGER liEnd = { ftEnd.dwLowDateTime, ftStart.dwEndDateTime };

            printf("Duration: %I64u ms\n", (liEnd - liStart) / (10 * 1000));

            or, if you dont mind using casts since ULARGE_INTEGER and FILETIME have the same structure:

            ULARGE_INTEGER liStart, liEnd;
            GetSystemTimeAsFileTime((FILETIME *)&liStart);
            //do something
            GetSystemTimeAsFileTime((FILETIME *)&liEnd);

            printf("Duration: %I64u ms\n", (liEnd - liStart) / (10 * 1000));

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups