From owner-freebsd-hackers@FreeBSD.ORG Thu Mar 31 11:55:27 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 2F05D16A4CE for ; Thu, 31 Mar 2005 11:55:27 +0000 (GMT) Received: from publicd.ub.mng.net (publicd.ub.mng.net [202.179.0.88]) by mx1.FreeBSD.org (Postfix) with ESMTP id 17F6C43D1D for ; Thu, 31 Mar 2005 11:55:26 +0000 (GMT) (envelope-from ganbold@micom.mng.net) Received: from [202.179.0.164] (helo=ganbold.micom.mng.net) by publicd.ub.mng.net with esmtpa (Exim 4.43 (FreeBSD)) id 1DGyR3-0002CK-6Q for freebsd-hackers@FreeBSD.org; Thu, 31 Mar 2005 21:06:06 +0900 Message-Id: <6.2.0.14.2.20050331203318.031f2050@202.179.0.80> X-Mailer: QUALCOMM Windows Eudora Version 6.2.0.14 Date: Thu, 31 Mar 2005 20:53:12 +0900 To: freebsd-hackers@FreeBSD.org From: Ganbold Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii"; format=flowed Subject: 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 11:55:27 -0000 Hi hackers, 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? thanks in advance, Ganbold Here is system info: # uname -an FreeBSD backend.ub.mng.net 4.11-PRERELEASE FreeBSD 4.11-PRERELEASE #4: Tue Dec 14 18:18:34 ULAT 2004 tsgan@backend.ub.mng.net:/usr/obj/usr/src/sys/DB i386 # env | grep TZ TZ=Asia/Ulaanbaatar # date Thu Mar 31 20:45:14 ULAST 2005 Here is program output: # ./test_date Thu Mar 31 20:36:47 2005 Current Date: 2005-03-31 0 day(s) before current Date: 2005-03-31 1 day(s) before current Date: 2005-03-30 2 day(s) before current Date: 2005-03-29 3 day(s) before current Date: 2005-03-28 4 day(s) before current Date: 2005-03-27 5 day(s) before current Date: 2005-03-25 6 day(s) before current Date: 2005-03-24 7 day(s) before current Date: 2005-03-23 8 day(s) before current Date: 2005-03-22 9 day(s) before current Date: 2005-03-21 10 day(s) before current Date: 2005-03-20 11 day(s) before current Date: 2005-03-19 12 day(s) before current Date: 2005-03-18 13 day(s) before current Date: 2005-03-17 14 day(s) before current Date: 2005-03-16 15 day(s) before current Date: 2005-03-15 16 day(s) before current Date: 2005-03-14 17 day(s) before current Date: 2005-03-13 18 day(s) before current Date: 2005-03-12 19 day(s) before current Date: 2005-03-11 20 day(s) before current Date: 2005-03-10 21 day(s) before current Date: 2005-03-09 22 day(s) before current Date: 2005-03-08 23 day(s) before current Date: 2005-03-07 24 day(s) before current Date: 2005-03-06 25 day(s) before current Date: 2005-03-05 26 day(s) before current Date: 2005-03-04 27 day(s) before current Date: 2005-03-03 28 day(s) before current Date: 2005-03-02 29 day(s) before current Date: 2005-03-01 30 day(s) before current Date: 2005-02-28 31 day(s) before current Date: 2005-02-27 Total run time = 0 sec # Here is test program. test_date.c ------------------------------------------------------------------------------------------ #include #include #include #include #include #include char *getDate(int day); char *my_alloc(char *strin); int main(int argc, char *argv[]){ time_t now; int start_day = 1; struct timeval t1; struct timeval t2; char *m_date, *cur_date; int i; long d; gettimeofday(&t1,NULL); now = time(NULL); fprintf(stderr, "%s\n",ctime(&now)); start_day = 32; cur_date = getDate(0); for(i=0;itm_mday -= day; t->tm_hour = t->tm_min = t->tm_sec = 0; 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; } /*---------------------------------------------------------------------------------------------------------------------*/ char *my_alloc(char *strin) { int len; char *p; len = strlen (strin) + 1; p = (char *)malloc(len); if (p != NULL) { strcpy (p, strin); } return (p); }