C++读系统时间,且读出毫秒的方法[VC和GCC]


  1. #include <iostream>
  2. #ifdef WIN32
  3. #include <windows.h>
  4. #else
  5. #include <sys/time.h>
  6. #endif

  7. using std::cout;
  8. using std::endl;

  9. long GetMillisecond()
  10. {
  11.     long    lMillisecond = 0L;
  12. #ifdef WIN32
  13.     SYSTEMTIME    st;     // System time structure
  14.     GetSystemTime(&st);     // Get system time
  15.     lMillisecond = st.wMilliseconds;     // Get millisecond
  16. #else
  17.     struct timeval tv;
  18.     gettimeofday(&tv, NULL);     // NULL is only legal value
  19.     lMillisecond = tv.tv_usec / 1000;     // Get Millisecond;
  20. #endif

  21.     return lMillisecond;
  22. }

  23. int main()
  24. {
  25.     int i = 0;     // Counter
  26.     cout<<"Get Millisecond Example by JJ."<<endl;
  27.     cout<<"----------------------------"<<endl;

  28.     for (i = 0; i < 20; ++i)
  29.     {
  30.         cout<<GetMillisecond()<<endl;
  31.         Sleep(100);
  32.     }
  33.     cout<<"---------------------------- Done!"<<endl;
  34.     return 0;
  35. }

阅读(662) | 评论(0) | 转发(2) |
0

上一篇:收藏摄影作品

下一篇:IDEA算法c语言实现

给主人留下些什么吧!~~
评论热议
原文地址:https://www.cnblogs.com/black/p/5171611.html