From: | Rudi Chiarito |
Date: | 21 Aug 2000 at 14:25:50 |
Subject: | Re: AMIOPEN: Bug in strftime |
There were some bugs in the example...
> With the "%z" format, /lib/strftime segfaults if given a date
> that falls
> during the summer.
>
> Try the code below. It will crash if you type in a number
> between 88 and 298:
>
> - Marc Culler
>
> #include <stdio.h>
> #include <time.h>
>
> int main(void)
> {
> time_t t;
> struct tm *p;
> int days;
> char rfc822_zone[10];
>
> puts("Enter a number of days since the epoch: ");
> scanf("%d", &days);
> t = (time_t)(days*24*3600);
'time_t' is a long, 'days' is an int. It overflows in January 2038
(days > 24855)
To avoid this, cast to 'time_t' earlier, or use longs internally within
the expression. ie
t = (time_t)days*24*3600;
Alternatively,
t = (time_t)((long)days*24*3600);
[We don't want the OS to be blamed for the 2038 bug when the problem
is in the application ;) ]
> p = localtime(&t);
> printf("year: %d\n", 1900 + p->tm_year);
> printf("month: %d\n", p->tm_mon);
'tm_mon' ranges from 0..11
printf("month: %d\n", p->tm_mon+1);
> printf("day: %d\n", p->tm_mday);
>
> /* The call to strftime segfaults if the date falls in the summer! */
>
> strftime(rfc822_zone, 10, "%z", p);
> printf("rfc822 zone: %s\n", rfc822_zone);
Officially, something should be returned.
> }