subtracting days from localtime problem

From: Ganbold (ganbold_at_micom.mng.net)
Date: 03/31/05

  • Next message: David Schultz: "Re: organization"
    Date: Thu, 31 Mar 2005 20:53:12 +0900
    To: freebsd-hackers@FreeBSD.org
    
    

    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 <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #include <sys/time.h>
    #include <time.h>

    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;i<start_day;i++){
             printf("%d day(s) before current ",i);
             m_date = getDate(i);
         }

         gettimeofday(&t2,NULL);
         printf("\nTotal run time = %ld sec\n", (t2.tv_sec - t1.tv_sec));

         exit(0);
    }

    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;

         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);
    }

    _______________________________________________
    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"


  • Next message: David Schultz: "Re: organization"

    Relevant Pages

    • SHA1_Update() produces wrong results for large buffers
      ... Attached is a test program, which mmaps a file and then ... int fd; struct stat st; SHA_CTX ctx; unsigned char *buf; char hexdigest; ...
      (freebsd-stable)
    • Re: Is the behaviour defined
      ... The spell of the return is wrong in the test program above.I did not have it in my test program which I compiled, but added it while composing this mail, of the fear of getting battered by the C language purists;). ... type char*. ... c has type char* while &c ... With 'strcpy;' the Devil steps in, destroying c and anything else the Devil chooses. ...
      (comp.lang.c)
    • Re: Newbie: learning to use malloc().
      ... of 80 char. ... The array I understand, the pointer I ... sizeof char * is 4 ... little test program above verifies this. ...
      (comp.lang.c)