1

I currently have a double containing a POSIX time stamp, and I am successfully using gmtime and asctime to display a calendar date via a time_t struct:

    time_t input = posix;
    printf("%s",asctime(gmtime(&input)));

This works well, except (obviously) when the POSIX time falls outside the time.h library's limits (i.e. 1901-2038), in which case it returns a date in 1901. Are there any easy alternatives to gmtime/asctime/time.h, or am I simply going to have to work with the raw figures?

Edit: I should add that having a result that falls outside those limits is fairly likely, as the posix double is the result of a calculation, rather than an instantiation of the current system time.

ojk
  • 23
  • 2
  • How do you expect an international standrad to be displayed as a calendar date? –  Dec 28 '12 at 15:07
  • By calendar date I mean a representation separated into year, month, day, etc. In the above case, when the content of double posix is 1235736569.980815, it correctly returns Fri Feb 27 12:09:29 2009. – ojk Dec 28 '12 at 15:11
  • I hardly think there is a viable alternative to `time.h` covering the 2038 problem. You sure you need to cover dates over 20 yrs from now? :) – davak Dec 28 '12 at 15:57
  • Yeah absolutely certain I need to cover dates beyond 2038 and before 1901... – ojk Dec 28 '12 at 16:05

1 Answers1

0

Read the Wikipedia entry on Unix Time:

The POSIX and Open Group Unix specifications include the C standard library, which includes the time types and functions defined in the <time.h> header file. The ISO C standard states that time_t must be an arithmetic type, but does not mandate any specific type or encoding for it.

If you need to handle a large range of times in a portable manner, you will probably have to use your own time library. I don't think you can even count on struct tm covering a range beyond 1901 to 2038.

You might also want to read this StackOverflow answer regarding time_t.

Community
  • 1
  • 1
tomlogic
  • 11,489
  • 3
  • 33
  • 59