簡體   English   中英

如何比較C中的兩個時間戳?

[英]How do I compare two timestamps in C?

我正在編寫一個套接字程序來維護兩個輸入套接字的FIFO隊列。 在決定要服務的隊列時,程序從每個隊列中提取最新的時間戳。

我需要比較兩個可靠的方法timeval結構。 我嘗試使用timercmp() ,但我的gcc版本不支持它,並且文檔聲明該函數不符合POSIX。

我該怎么辦?

timercmp()只是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))

如果你需要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)

谷歌搜索timeval給出了第一個結果 從該頁面:

通常需要減去struct timeval或struct timespec類型的兩個值。 這是最好的方法。 它甚至可以在某些特殊的操作系統上運行,其中tv_sec成員具有無符號類型。

 /* Subtract the `struct timeval' values X and Y,
    storing the result in RESULT.
    Return 1 if the difference is negative, otherwise 0.  */

 int
 timeval_subtract (result, x, y)
      struct timeval *result, *x, *y;
 {
   /* Perform the carry for the later subtraction by updating y. */
   if (x->tv_usec < y->tv_usec) {
     int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
     y->tv_usec -= 1000000 * nsec;
     y->tv_sec += nsec;
   }
   if (x->tv_usec - y->tv_usec > 1000000) {
     int nsec = (x->tv_usec - y->tv_usec) / 1000000;
     y->tv_usec += 1000000 * nsec;
     y->tv_sec -= nsec;
   }

   /* Compute the time remaining to wait.
      tv_usec is certainly positive. */
   result->tv_sec = x->tv_sec - y->tv_sec;
   result->tv_usec = x->tv_usec - y->tv_usec;

   /* Return 1 if result is negative. */
   return x->tv_sec < y->tv_sec;
 }

這略有不同,但我認為清楚地說明了所涉及的邏輯。 我正在使用C語言編寫一些MSP430代碼,並且時間戳結構與timeval非常相似,但是使用nsecs而不是usecs。

這段代碼保持一切正面,所以無符號整數可以正常工作,並避免溢出(我認為)。 除了結果之外,它也不會修改傳入的時間戳/時間。

typedef struct timestamp {
    int32_t secs;
    int32_t nsecs;
} timestamp_t;

int timestamp_sub(timestamp_t * x, timestamp_t * y, timestamp_t * result){
    // returns 1 if difference is negative, 0 otherwise
    // result is the absolute value of the difference between x and y
    negative = 0;
    if( x->secs > y->secs ){
        if( x->nsecs > y->nsecs ){
            result->secs = x->secs - y->secs;
            result->nsecs = x->nsecs - y->nsecs;
        }else{
            result->secs = x->secs - y->secs - 1;
            result->nsecs = (1000*1000*1000) - y->nsecs + x->nsecs;
        }
    }else{
        if( x->secs == y->secs ){
            result->secs = 0;
            if( x->nsecs > y->nsecs ){
                result->nsecs = x->nsecs - y->nsecs;
            }else{
                negative = 1;
                result->nsecs = y->nsecs - x->nsecs;
            }
        }else{
            negative = 1;
            if( x->nsecs > y->nsecs ){
                result->secs = y->secs - x->secs - 1;
                result->nsecs = (1000*1000*1000) - x->nsecs + y->nsecs;
            }else{
                result->secs = y->secs - x->secs;
                result->nsecs = y->nsecs - x->nsecs;
            }
        }
    }
    return negative;
}

為了查看時間,我只是掀起了這個。 它返回一個timeval作為字符串,您可以打印或發送到文本文件:

char *tv2str(struct timeval *intv) {
  static char ans[200];
  snprintf(ans,200,"%u.%u",(unsigned int)intv->tv_sec, \
    (unsigned int) intv->tv_usec);
  return ans;
}

使用如下:

printf("nowtv: %s\n",tv2str(&nowtv));

nowtv:1568407554.646623

Timercmp()似乎沒有正常工作所以我想通過實際查看一些值來檢查它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM