簡體   English   中英

C time.h環繞

[英]C time.h wrap around

我想將最大可能時間分配給time_t變量,然后將其轉換為字符串並打印出結果。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <limits.h>

int main()
{
    time_t maxTime;
    maxTime= LONG_LONG_MAX;
    char *strOfMaxTime;
    *strOfMaxTime = ctime(maxTime);

    printf("%s",strOfMaxTime);

    return 0;
}

OP的代碼錯誤地使用了char *ctime(const time_t *timer)

time_t maxTime;
char *strOfMaxTime;
// *strOfMaxTime = ctime(maxTime);
strOfMaxTime = ctime(&maxTime);

只需分配maxTime= LONG_LONG_MAX; 確定系統可以處理的最長時間的正確方法不是必需的。

以下是反復試驗的方法-可能會受到各種實施限制。 time_t超出范圍時, localtime()返回NULL

#include <stdio.h>
#include <time.h>

time_t max_time() {
  time_t t0, t1;
  time_t delta = 1;
  time(&t0);    // now
  while (t0 != -1) {
    t1 = t0 + delta;
    if (localtime(&t1) == NULL) {  // If conversion fail, quit doubling.
      break;
    }
    delta *= 2; // 2x for  the next increment.
    t0 = t1;
  }
  while (delta) {
    t1 = t0 + delta;
    if (localtime(&t1) != NULL) { // if succeeds, update t0
      t0 = t1;
    }
    delta /= 2; // try smaller and smaller deltas.
  }
  printf("%s %lld\n", ctime(&t0), (long long) t0);
  return t0;
}

int main(void) {
  max_time();
  return 0;
}

輸出(請注意17:59:59取決於時區,並且2,147,483,647年是最大的32位有符號整數。YMMV。)

Tue Dec 31 17:59:59 2147483647
 67767976233532799

來自C Standards#7.27.1p4

在clock_t和time_t中可表示的時間范圍和精度是實現定義的

首先,您需要在程序中解決該問題。 下面的語句在編譯時必須給出錯誤:

*strOfMaxTime = ctime(maxTime);

更改為:

strOfMaxTime = ctime(&maxTime);

您可以使用perror()獲取給定輸入LONG_LONG_MAX的錯誤消息,如下所示:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <limits.h>
#include <errno.h>

int main()
{
    time_t maxTime;
    maxTime= LONG_LONG_MAX;
    char *strOfMaxTime;
    strOfMaxTime = ctime(&maxTime);
    if (errno != 0)
       perror ("Error");
    else
       printf("%d,%s",errno, strOfMaxTime);

    return 0;
}

在我的設置中,我得到以下輸出:

Error: Value too large to be stored in data type

實際上, LONG_LONG_MAX是無效的輸入。

正如標准所提到的, time_t的范圍是實現定義的,因此,如果我給出UINT_MAX我將得到輸出:

0,Sun Feb  7 11:58:15 2106

這是錯誤的:

*strOfMaxTime = ctime(maxTime);

這試圖將ctime (指向char的指針)的返回值分配給*strOfMaxTime char。

而是調用:

strOfMaxTime = ctime(&maxTime);

然后檢查strOfMaxTime的返回值,因為如果ctime未能轉換maxTime可能為NULL

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <limits.h>

int main()
{
    time_t maxTime;
    maxTime = INT_MAX;
    char *strOfMaxTime = ctime(&maxTime);

    printf("%s",strOfMaxTime);

    return 0;
}

最長年份為2038,這稱為2038年問題: https : //en.wikipedia.org/wiki/Year_2038_problem

在其他發布中指出了許多錯誤(將ctime()輸出分配給*strOfMaxTimeLONG_LONG_MAX等)。 在我的64位Ubuntu 16.04 Linux系統上, time_t定義為long intlong int定義為8個字節,而long long int定義為8個字節。 但是,將LLONG_MAX分配給maxTime仍然會導致ctime()失敗。 因此,我修改了您的代碼以獲取ctime()可接受的有效值上限的范圍。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <errno.h>
#include <limits.h>

int main()
{
    time_t maxTime;

    maxTime= LONG_MAX;

    char *strOfMaxTime;

    strOfMaxTime = ctime(&maxTime);

    while( strOfMaxTime == NULL )
    {
        perror("ctime error");
        printf("%ld div by 2\n", maxTime);
        maxTime /= 2;
        strOfMaxTime = ctime(&maxTime);
    }
    printf("%s\n",strOfMaxTime);

    return 0;
}

運行它會產生以下輸出:

ctime error: Invalid argument
9223372036854775807 div by 2
ctime error: Invalid argument
4611686018427387903 div by 2
ctime error: Invalid argument
2305843009213693951 div by 2
ctime error: Invalid argument
1152921504606846975 div by 2
ctime error: Invalid argument
576460752303423487 div by 2
ctime error: Invalid argument
288230376151711743 div by 2
ctime error: Invalid argument
144115188075855871 div by 2
ctime error: Invalid argument
72057594037927935 div by 2
Sat Jun 12 22:26:07 1141709097

暫無
暫無

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

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