How to print milliseconds in C?

后端 未结 3 455
难免孤独
难免孤独 2021-01-07 13:37

I want to print time in the format hh:mm:ss:ms(milliseconds). I could print in the form of hh:mm:ss. What can be the way to print remaining milliseconds?

相关标签:
3条回答
  • 2021-01-07 14:01
    #include<sys/timeb.h>
    #include<time.h>
    
    int main(void) {
        struct timeb tp;
        ftime(&tp);
        char timeString[80];
        strftime(timeString, sizeof(timeString), "%H:%M:%S", localtime(&tp.time));
        printf("%s:%d", timeString, tp.millitm);
        return 0;
    }
    
    0 讨论(0)
  • 2021-01-07 14:05

    If you're working with the MSDN library, you could try

    GetSystemTimeAsFileTime( pointerToSetToTime );
    

    Which sets a pointer argument to the current system time in 100-nanosecond intervals.

    0 讨论(0)
  • 2021-01-07 14:13

    This is what I use in linux ...

    struct timeval tv;
    gettimeofday(&tv,0);
    time_t long_time;
    struct tm *newtime;
    time(&long_time);
    newtime = localtime(&long_time);
    char result[100] = {0};
    sprintf(result, "%02d:%02d:%02d.%03ld", newtime->tm_hour,newtime->tm_min,newtime->tm_sec, (long)tv.tv_usec / 1000);
    return result;
    

    I have no experience working in windows .. try to find similar calls ..

    0 讨论(0)
提交回复
热议问题