How do I compare two timestamps in C?

前端 未结 4 839
感情败类
感情败类 2020-12-29 10:03

I\'m writing a socket program that maintains FIFO queues for two input sockets. When deciding which queue to service, the program pulls the most recent time-stamp from each

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-29 10:19

    timercmp() is just a macro in libc (sys/time.h):

    # define timercmp(a, b, CMP)                                                  \
      (((a)->tv_sec == (b)->tv_sec) ?                                             \
       ((a)->tv_usec CMP (b)->tv_usec) :                                          \
       ((a)->tv_sec CMP (b)->tv_sec))
    

    If you need timersub():

    # define timersub(a, b, result)                                               \
      do {                                                                        \
        (result)->tv_sec = (a)->tv_sec - (b)->tv_sec;                             \
        (result)->tv_usec = (a)->tv_usec - (b)->tv_usec;                          \
        if ((result)->tv_usec < 0) {                                              \
          --(result)->tv_sec;                                                     \
          (result)->tv_usec += 1000000;                                           \
        }                                                                         \
      } while (0)
    

提交回复
热议问题