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

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

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

7条回答
  •  爱一瞬间的悲伤
    2020-11-30 19:58

    This version need not math library and checked the return value of clock_gettime().

    #include 
    #include 
    #include 
    
    /**
     * @return milliseconds
     */
    uint64_t get_now_time() {
      struct timespec spec;
      if (clock_gettime(1, &spec) == -1) { /* 1 is CLOCK_MONOTONIC */
        abort();
      }
    
      return spec.tv_sec * 1000 + spec.tv_nsec / 1e6;
    }
    

提交回复
热议问题