subtracting days from localtime problem
ALeine
aleine at austrosearch.net
Thu Mar 31 05:55:04 PST 2005
ganbold at micom.mng.net wrote:
> I have problem subtracting days from current date using test
> program. We have daylight saving occured on 2AM of March 26, 2005.
> As you can see below, there is missing March 26th line from
> program output.
>
> And all lines after 27th March are wrong.
> Instead of 25th March it should be 26th March, 24th March should
> be 25th March and so on.
>
> Can somebody tell me why is this happening? How can I correct
> this problem?
You should set the tm_isdst to -1 before making the call to
mktime(3) in order to make mktime(3) try to figure out whether
Daylight Savings Time is in effect or not. See man 3 mktime for
details.
So, your getDate() function should look something like this:
char *
getDate(int day)
{
struct tm *t;
time_t now;
char date[12];
char *localdate;
time_t p;
now = time(NULL);
t = localtime(&now);
t->tm_mday -= day;
t->tm_hour = t->tm_min = t->tm_sec = 0;
/* make mktime(3) figure out whether DST is in effect */
t->tm_isdst = -1;
p = mktime(t);
if (p == (time_t)-1)
printf ("mktime failed\n");
snprintf(date,11,"%d-%.2d-%.2d", t->tm_year + 1900,
t->tm_mon + 1,
t->tm_mday);
if ((localdate=my_alloc(date)) == NULL) {
fprintf(stderr, "Allocation error!\n");
exit(2);
}
printf("Date: %s\n",localdate);
return localdate;
}
ALeine
___________________________________________________________________
WebMail FREE http://mail.austrosearch.net
More information about the freebsd-hackers
mailing list