Getting current time with milliseconds

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Harika
    New Member
    • Sep 2006
    • 19

    Getting current time with milliseconds

    hi all,

    I want to take current time with milli seconds in C. Does it have any header file to include to produce time in milli seconds.

    Please mail me if any of u know.

    Thank you.
  • tyreld
    New Member
    • Sep 2006
    • 144

    #2
    The "time" function in <time.h> will return the number of seconds since the epoch (ie. 00:00:00 on January 1, 1970, Coordinated Universal Time). The type of "time_t" is generally typedefed as "long int" on POSIX compliant systems. Finally, if we recall that a millisecond is 1/1000 of a second we can easily calculate the number of milliseconds since the epoch.

    Code:
    <time.h>
    
    ...
    
    time_t msec = time(NULL) * 1000;

    Comment

    • arne
      Recognized Expert Contributor
      • Oct 2006
      • 315

      #3
      Originally posted by Harika
      hi all,

      I want to take current time with milli seconds in C. Does it have any header file to include to produce time in milli seconds.

      Please mail me if any of u know.

      Thank you.
      You may try the "gettimeofd ay" function. It reports the time down to microsecond level. The function is included via <sys/time.h>.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        If you are using Windows rather than standard C you can call

        GetSystemTimeAs FileTime(...)

        with returns the current time as the number of 100-nanosecond intervals that have passed since January 1, 1601.

        Comment

        • Harika
          New Member
          • Sep 2006
          • 19

          #5
          Originally posted by Banfa
          If you are using Windows rather than standard C you can call

          GetSystemTimeAs FileTime(...)

          with returns the current time as the number of 100-nanosecond intervals that have passed since January 1, 1601.
          Hi Banfa,

          Actually we want to count current time in milliseconds and then has to show such a way that hr/min/sec/msec.

          Would u tell me the header files and functions in c required or else an example c program.

          Thank you.

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Originally posted by Harika
            Would u tell me the header files and functions in c required or else an example c program.
            Code:
            #include "windows.h"
            
            FILETIME now;
            
            GetSystemTimeAsFileTime(&now);
            or if you want it split in to hours etc
            Code:
            #include "windows.h"
            
            SYSTEMTIME now;
            
            GetSystemTime(&now);

            Comment

            • Harika
              New Member
              • Sep 2006
              • 19

              #7
              Originally posted by Banfa
              Code:
              #include "windows.h"
              
              FILETIME now;
              
              GetSystemTimeAsFileTime(&now);
              or if you want it split in to hours etc
              Code:
              #include "windows.h"
              
              SYSTEMTIME now;
              
              GetSystemTime(&now);


              hi Banfa,

              Thanks for ur best reply. But it doesnot works in linux. I need header files which is included in linux. Would u help me pls....

              Comment

              • arne
                Recognized Expert Contributor
                • Oct 2006
                • 315

                #8
                Originally posted by Harika
                hi Banfa,

                Thanks for ur best reply. But it doesnot works in linux. I need header files which is included in linux. Would u help me pls....
                Please refer to my previous post mentioning the gettimeofday() function. It will provide what you need.

                Comment

                • Incyc
                  New Member
                  • Apr 2012
                  • 1

                  #9
                  This will give you the time in the format HH:MM:SS:mmm

                  Code:
                  timeval tp;
                  gettimeofday(&tp, 0);
                  time_t curtime = tp.tv_sec;
                  tm *t = localtime(&curtime);
                  printf("%02d:%02d:%02d:%03d\n", t->tm_hour, t->tm_min, t->tm_sec, tp.tv_usec/1000);

                  Comment

                  Working...