Localtime() out by 1 hour

Mankin, Raphael Raphael.Mankin@O2.com
Fri Dec 20 02:53:00 GMT 2002


localtime() returns a value that is out by 1 hour for very small input
values, e.g. 0 give 1/1/1970 1:0:0 instead of 1/1/1970 0:0:0.

I have tested this on Solaris 2.6 with gcc-2.95.2 (unknown build) and
gcc-3.2, and on Linux with gcc-2.95.3 (unknown Slackware build).

Here is the build script for Solaris/gcc-3.2
	cd ../gcc-obj/
	../gcc-3.2//configure --prefix=$HOME/src/gcc-install \
        --enable-threads=posix \
        --enable-languages=c++,java
	MAKE=gmake MAKEFLAGS='-j5' time gmake

Here is the test program (complete)

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


main()
{       time_t i;
        struct  tm      *stm;

        for (i=0; i< 120; i++) {
            stm =localtime(&i);
            printf("%ld\t%02d/%02d/%04d %02d:%02d:%02d\n",
                 i,
                 stm->tm_mday, stm->tm_mon + 1, stm->tm_year + 1900,
                 stm->tm_hour, stm->tm_min, stm->tm_sec);

        }
}


Linux compilation:  gcc -v -o testx test.c
Solaris compilation: ~/src/gcc-install/bin/gcc -static -o test test.c -v

Here is part of the output:

0       01/01/1970 01:00:00
1       01/01/1970 01:00:01
2       01/01/1970 01:00:02
3       01/01/1970 01:00:03
4       01/01/1970 01:00:04
5       01/01/1970 01:00:05
6       01/01/1970 01:00:06
7       01/01/1970 01:00:07
8       01/01/1970 01:00:08
9       01/01/1970 01:00:09
10      01/01/1970 01:00:10
11      01/01/1970 01:00:11
12      01/01/1970 01:00:12
13      01/01/1970 01:00:13
14      01/01/1970 01:00:14
15      01/01/1970 01:00:15
16      01/01/1970 01:00:16
17      01/01/1970 01:00:17
18      01/01/1970 01:00:18
19      01/01/1970 01:00:19
20      01/01/1970 01:00:20
21      01/01/1970 01:00:21
--
 
This email and any attachments may be confidential and the subject of legal
professional privilege. Any disclosure, use, storage or copying of this
email without the consent of the sender is strictly prohibited. Please
notify the sender immediately if you are not the intended recipient and then
delete the email from your inbox and do not disclose the contents to another
person, use, copy or store the information in any medium.



More information about the Gcc-bugs mailing list