简体   繁体   中英

C time.h wrap around

I would like to assign max possible time to a time_t variable then convert it to a string and print out the result.

#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's code is using char *ctime(const time_t *timer) incorrectly.

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

Yet simply assigning maxTime= LONG_LONG_MAX; is not necessary the correct way to determine the maximum time a system can handle.

Below is a trial and error method - likely with various implementation limitations. localtime() returns NULL when time_t is out of range.

#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;
}

Output (Note that 17:59:59 depends on timezone and the year 2,147,483,647 is the max 32-bit signed integer. YMMV.)

Tue Dec 31 17:59:59 2147483647
 67767976233532799

From C Standards#7.27.1p4

The range and precision of times representable in clock_t and time_t are implementation-defined .

First, you need to fix the issue in your program. The below statement must be giving error while compiling:

*strOfMaxTime = ctime(maxTime);

Change this to:

strOfMaxTime = ctime(&maxTime);

You can use perror() to get the error message for the given input - LONG_LONG_MAX , like this:

#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;
}

On my setup I am getting this output:

Error: Value too large to be stored in data type

Indeed, LONG_LONG_MAX is invalid input.

As the standard mentions that the range of time_t is implementation-defined, so if I give UINT_MAX I am getting the output:

0,Sun Feb  7 11:58:15 2106

This is wrong:

*strOfMaxTime = ctime(maxTime);

This tries to assign the return value of ctime (a pointer to a char) to *strOfMaxTime a char.

Instead call:

strOfMaxTime = ctime(&maxTime);

And then check the return value of strOfMaxTime as it may be NULL if ctime fails to convert maxTime

#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;
}

The maximum year is 2038, and this is known as Year 2038 problem: https://en.wikipedia.org/wiki/Year_2038_problem

Numerous errors have been pointed out in other postings (assigning output of ctime() to *strOfMaxTime , LONG_LONG_MAX , etc). On my 64bit Ubuntu 16.04 Linux system, time_t is defined as a long int and a long int is defined as 8 bytes as is a long long int . However assigning LLONG_MAX to maxTime still causes ctime() to fail. So I modified your code to get a range of what the upper limit of valid values ctime() will accept.

#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;
}

Running it yields the following output:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM