PRB: localtime() May Return Invalid Pointer for Small Number

ID Number: Q74667

6.00 6.00a 6.00ax | 6.00 6.00a

MS-DOS | OS/2

buglist6.00 buglist6.00a buglist6.00ax

Summary:

SYMPTOMS

The run-time library function localtime() in Microsoft C versions

6.0, 6.0a, and 6.0ax and Microsoft QuickC versions 2.5 and 2.51 may

return an invalid value for a small number.

CAUSE

The localtime() function converts an object of type time_t to a

structure. The time_t value, which is passed to localtime(),

represents the number of seconds passed since January 1, 1970.

However, MS-DOS and OS/2 do not accommodate values prior to 1980.

If a value for time_t represents a value before 1980, localtime()

is documented to return a NULL pointer; however, for very small

values, localtime() may return an invalid structure pointer.

STATUS

Microsoft has confirmed this to be a problem in Microsoft C

versions 6.0, 6.0a, and 6.0ax and in Microsoft QuickC versions 2.5

and 2.51 (buglist2.50 and buglist2.51). We are researching this

problem and will post new information here as it becomes available.

More Information:

The sample code below illustrates this problem. A sample run of the

program generated the following results:

localtime(100) returned: Sat Feb 05 22:29:56 2106

localtime(100000) returned a NULL pointer.

localtime(679953756) returned: Fri Jul 19 13:02:36 1991

Although the second call to localtime() correctly returns a NULL

pointer, the first call returns a pointer to a time struct containing

the date February 5, 2106.

In C/C++ 7.0 the localtime() function starts at January 1, 1900.

Sample Code

-----------

/* Compile options needed: none

*/

#include <time.h>

#include <stdio.h>

#include <sys\types.h>

#include <sys\timeb.h>

void test(time_t ltime);

void main()

{

time_t now;

test(100L);

test(100000L);

test(time(&now));

}

void test(time_t ltime)

{

struct tm *today;

today = localtime(&ltime);

if (today==NULL)

printf("localtime(%lu) returned a NULL pointer.\n", ltime);

else

printf("localtime(%lu) returned: %s", ltime, asctime(today));

}

Additional reference words: 2.00 2.50 6.00 6.00a 6.00ax