[prev in list] [next in list] [prev in thread] [next in thread] 

List:       freebsd-hackers
Subject:    Re: subtracting days from localtime problem
From:       "ALeine" <aleine () austrosearch ! net>
Date:       2005-03-31 13:55:06
Message-ID: 200503311355.j2VDt6fo023956 () marlena ! vvi ! at
[Download RAW message or body]

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 
_______________________________________________
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "freebsd-hackers-unsubscribe@freebsd.org"
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic