Like Tree3Likes
  • 2 Post By antred
  • 1 Post By Salem

how can I get the system time in the unit of mili-second

This is a discussion on how can I get the system time in the unit of mili-second within the C++ Programming forums, part of the General Programming Boards category; Hello, I want to get the system time for a delay. But I do not want to use Windows API, ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    17

    how can I get the system time in the unit of mili-second

    Hello,

    I want to get the system time for a delay. But I do not want to use Windows API, for I want my program to run in Linux system as well.
    For example, there is a description of what I want to implement.

    Code:
      // codes
          start_time = func_2_get_current_time_in_mili-second;
          do{
              cur_time = the same func as above;
          while( (cur_time-start_time)<80 )
    I want it to make a delay in 80 mili-seconds or another time.
    Any suggestion is appreciated.

  2. #2
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    247
    If you have a compiler that supports C++11, have a look at std::chrono::system_clock::now and std::chrono::duration.


    P.S. Scroll down to the 2nd answer. http://stackoverflow.com/questions/3...ifference-in-c
    Salem and manasij7479 like this.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,544
    You can't get millisecond time in a portable manner (but see previous post).

    So you have to do something like this
    Code:
    #ifdef WIN32
    unsigned long getMilliTime( void ) {
      unsigned long result = (unsigned long)GetTickCount();
      return result;
    }
    #else
    unsigned long getMilliTime( void ) {
      unsigned long result = 0;
      struct timeval t;
      if ( gettimeofday(&t,NULL) ) {
        result = t.tv_sec * 1000 + t.tv_usec / 1000;
      }
      return result;
    }
    #endif
    For a one-off function, you can do this inline within a source file.

    But if you've got a lot of them, then it's worth creating something like
    Code:
    #ifndef PORTING_H
    #define PORTING_H
    unsigned long getMilliTime( void );
    #endif
    All your win32 goes into port_win32.cpp
    Code:
    unsigned long getMilliTime( void ) {
      unsigned long result = (unsigned long)GetTickCount();
      return result;
    }
    All your POSIX goes into port_posix.cpp
    Code:
    unsigned long getMilliTime( void ) {
      unsigned long result = 0;
      struct timeval t;
      if ( gettimeofday(&t,NULL) ) {
        result = t.tv_sec * 1000 + t.tv_usec / 1000;
      }
      return result;
    }
    The only config change is the build rule which picks up either port_win32.cpp or port_posix.cpp
    The 99% of the code which doesn't care about the platform just calls your wrapper function, and remains unaware of the platform.
    antred likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    17
    Thanks very much to you both.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Want to get system time...how?
    By Blacky Ducky in forum C++ Programming
    Replies: 9
    Last Post: 05-12-2011, 04:00 PM
  2. time of system
    By arian in forum C++ Programming
    Replies: 3
    Last Post: 12-10-2003, 11:51 AM
  3. System Time
    By Leeman_s in forum C++ Programming
    Replies: 2
    Last Post: 05-23-2003, 06:45 PM
  4. System Time
    By MethodMan in forum C Programming
    Replies: 4
    Last Post: 03-16-2003, 01:07 PM
  5. system time
    By jpre in forum C++ Programming
    Replies: 2
    Last Post: 09-29-2001, 09:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21