How to get the current time in milliseconds from C in Linux?

前端 未结 7 922
不知归路
不知归路 2020-11-30 19:09

How do I get the current time on Linux in milliseconds?

7条回答
  •  隐瞒了意图╮
    2020-11-30 20:01

    C11 timespec_get

    It returns up to nanoseconds, rounded to the resolution of the implementation.

    It is already implemented in Ubuntu 15.10. API looks the same as the POSIX clock_gettime.

    #include 
    struct timespec ts;
    timespec_get(&ts, TIME_UTC);
    struct timespec {
        time_t   tv_sec;        /* seconds */
        long     tv_nsec;       /* nanoseconds */
    };
    

    More details here: https://stackoverflow.com/a/36095407/895245

提交回复
热议问题