Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 31 Mar 2005 05:55:06 -0800 (PST)
From:      "ALeine" <aleine@austrosearch.net>
To:        ganbold@micom.mng.net
Cc:        freebsd-hackers@freebsd.org
Subject:   Re: subtracting days from localtime problem 
Message-ID:  <200503311355.j2VDt6fo023956@marlena.vvi.at>

next in thread | raw e-mail | index | archive | help
ganbold@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 



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200503311355.j2VDt6fo023956>