How to print milliseconds in C?

后端 未结 3 454
难免孤独
难免孤独 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条回答
  •  -上瘾入骨i
    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 ..

提交回复
热议问题