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

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

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

7条回答
  •  感情败类
    2020-11-30 20:04

    Derived from Dan Moulding's POSIX answer, this should work :

    #include 
    #include 
    
    long millis(){
        struct timespec _t;
        clock_gettime(CLOCK_REALTIME, &_t);
        return _t.tv_sec*1000 + lround(_t.tv_nsec/1.0e6);
    }
    

    Also as pointed out by David Guyon: compile with -lm

提交回复
热议问题