From owner-freebsd-hackers@FreeBSD.ORG Thu Mar 31 13:55:04 2005 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 045CA16A4CE for ; Thu, 31 Mar 2005 13:55:04 +0000 (GMT) Received: from marlena.vvi.at (marlena.vvi.at [208.252.225.59]) by mx1.FreeBSD.org (Postfix) with ESMTP id 8DFD843D46 for ; Thu, 31 Mar 2005 13:55:03 +0000 (GMT) (envelope-from www@marlena.vvi.at) Received: from marlena.vvi.at (localhost.marlena.vvi.at [127.0.0.1]) by marlena.vvi.at (8.12.10/8.12.9) with ESMTP id j2VDtCh3023957; Thu, 31 Mar 2005 05:55:14 -0800 (PST) (envelope-from www@marlena.vvi.at) Received: (from www@localhost) by marlena.vvi.at (8.12.10/8.12.10/Submit) id j2VDt6fo023956; Thu, 31 Mar 2005 05:55:06 -0800 (PST) (envelope-from www) Date: Thu, 31 Mar 2005 05:55:06 -0800 (PST) Message-Id: <200503311355.j2VDt6fo023956@marlena.vvi.at> To: ganbold@micom.mng.net From: "ALeine" cc: freebsd-hackers@freebsd.org Subject: Re: subtracting days from localtime problem X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 31 Mar 2005 13:55:04 -0000 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