date and time arithmatic

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • upadhyad
    New Member
    • Jul 2008
    • 15

    #1

    date and time arithmatic

    Hi all!!
    i am relatively new to c... and I need to help urgently!
    i have date and time in 2 separate strings. i have parsed them. eg
    I had 26.2.2007 and I parsed it so that i have day month and year in different integer varaibles i.e. a=26; b=2;c=2007;
    Similarly I have the time also parsed and hour minute and seconds stored in different integer variables.
    Now I have to store them in the struct tm or any other time format so that i can use difftime function of the time.h

    a
    *** stack smashing detected ***: ./a.out terminated
    ======= Backtrace: =========
    /lib/tls/i686/cmov/libc.so.6(__for tify_fail+0x48)[0xb7f52138]
    /lib/tls/i686/cmov/libc.so.6(__for tify_fail+0x0)[0xb7f520f0]
    ./a.out[0x80485b9]
    [0x7d7]
    ======= Memory map: ========
    08048000-08049000 r-xp 00000000 08:01 1400873 /home/upadhyad/Desktop/C testing/date time/a.out
    08049000-0804a000 rw-p 00000000 08:01 1400873 /home/upadhyad/Desktop/C testing/date time/a.out
    0804a000-0806b000 rw-p 0804a000 00:00 0 [heap]
    b7e64000-b7e65000 rw-p b7e64000 00:00 0
    b7e65000-b7fae000 r-xp 00000000 08:01 17088 /lib/tls/i686/cmov/libc-2.7.so
    b7fae000-b7faf000 r--p 00149000 08:01 17088 /lib/tls/i686/cmov/libc-2.7.so
    b7faf000-b7fb1000 rw-p 0014a000 08:01 17088 /lib/tls/i686/cmov/libc-2.7.so
    b7fb1000-b7fb4000 rw-p b7fb1000 00:00 0
    b7fb5000-b7fbf000 r-xp 00000000 08:01 4661312 /lib/libgcc_s.so.1
    b7fbf000-b7fc0000 rw-p 0000a000 08:01 4661312 /lib/libgcc_s.so.1
    b7fc0000-b7fc3000 rw-p b7fc0000 00:00 0
    b7fc3000-b7fc4000 r-xp b7fc3000 00:00 0 [vdso]
    b7fc4000-b7fde000 r-xp 00000000 08:01 4661267 /lib/ld-2.7.so
    b7fde000-b7fe0000 rw-p 00019000 08:01 4661267 /lib/ld-2.7.so
    bfc69000-bfc7e000 rw-p bffeb000 00:00 0 [stack]
    Aborted
  • upadhyad
    New Member
    • Jul 2008
    • 15

    #2
    PLz someone help...it is urgent

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Originally posted by upadhayad
      struct tm *haha;
      etc............ ........

      haha->tm_sec=c;
      haha->tm_min=b;
      haha->tm_hour=a;
      haha->tm_mday=x;
      haha->tm_mon=y;
      haha->tm_year=z;
      All you have is a pointer to a struct tm but you a) have no struct tm itself and b) even if you did this pointer has not been initialized.

      Either:
      [code=cpp]
      struct tm haha;
      haha.tm_sec=c;
      etc...
      [/code]

      or
      [code=cpp]
      struct tm theTime;
      struct tm* haha = &theTime; //define and initialize pointer
      haha->tm_sec=c;
      etc...
      [/code]

      Comment

      • upadhyad
        New Member
        • Jul 2008
        • 15

        #4
        Thanks a lot for replying!!
        I rectified the program. now I have made it to get the difference of the different times :
        [code=c]
        struct tm theTime1, theTime2;
        struct tm *pointer1=&theT ime1; //first pointer
        struct tm *pointer2=&theT ime2; //second pointer

        char date1[]="26.2.2007" ; //first date and time
        char time1[]="13:34:45";

        char date2[]="26.2.2007" ; //second date and time
        char time2[]="13:34:49";

        char *ptr1, *ptr11;
        int x1,y1,z1,a1,b1, c1;

        char *ptr2, *ptr22;
        int x2,y2,z2,a2,b2, c2;

        ptr1=(char *)strtok(date1, "."); //first date parse
        x1=atoi(ptr1);
        ptr1=(char *)strtok(NULL," .");
        y1=atoi(ptr1);
        ptr1=(char *)strtok(NULL," .");
        z1=atoi(ptr1);

        ptr11=(char *)strtok(time1, ":"); //first time parse
        a1=atoi(ptr11);
        ptr11=(char *)strtok(NULL," :");
        b1=atoi(ptr11);
        ptr11=(char *)strtok(NULL," :");
        c1=atoi(ptr11);

        ptr2=(char *)strtok(date2, "."); //second date parse
        x2=atoi(ptr2);
        ptr2=(char *)strtok(NULL," .");
        y2=atoi(ptr2);
        ptr2=(char *)strtok(NULL," .");
        z2=atoi(ptr2);

        ptr22=(char *)strtok(time2, ":"); //second time parse
        a2=atoi(ptr22);
        ptr22=(char *)strtok(NULL," :");
        b2=atoi(ptr22);
        ptr22=(char *)strtok(NULL," :");
        c2=atoi(ptr22);

        pointer1->tm_sec=c1; //assigning values to the components of the calendar time 1
        pointer1->tm_min=b1;
        pointer1->tm_hour=a1;
        pointer1->tm_mday=x1;
        pointer1->tm_mon=y1;
        pointer1->tm_year=z1;

        pointer2->tm_sec=c2; //assigning values to the components of the calendar time 2
        pointer2->tm_min=b2;
        pointer2->tm_hour=a2;
        pointer2->tm_mday=x2;
        pointer2->tm_mon=y2;
        pointer2->tm_year=z2;

        time_t t1= mktime(pointer1 ); //converting time 1 to the calendar time

        time_t t2= mktime(pointer2 ); //converting time 2 to the calendar time
        [/code]
        ---------------------------the output-------------------------
        0
        -1
        -1
        --------------------------------

        please help!
        Last edited by sicarie; Jul 5 '08, 11:56 PM. Reason: Code tags are [code=c] and after your code [/code]. Please use them.

        Comment

        • gpraghuram
          Recognized Expert Top Contributor
          • Mar 2007
          • 1275

          #5
          First tell me how are you or where u are getting the date and time?
          Also explain what is ur requirement.

          Raghu

          Comment

          • upadhyad
            New Member
            • Jul 2008
            • 15

            #6
            hi gpraghuram!

            I have read these date and time values form a text file.
            the text file has stages listed in each newline and each stage has a starting date and time and one ending date and time. I have read the dates and times as string form the file. First I wanted to be sure of the date and time operations therefore I wrote the above program. there is no aditional information given. I have tosubtract the starting time and the ending time.

            Comment

            • gpraghuram
              Recognized Expert Top Contributor
              • Mar 2007
              • 1275

              #7
              I think then you should make use of this function strptime

              Raghuram.

              Comment

              • upadhyad
                New Member
                • Jul 2008
                • 15

                #8
                Hi thanks a lot
                that command was not listed in the book i m using
                I have a new problem now.
                I have defined a new structure:
                =============== ===

                struct IDnTime
                {
                char previousEdate[50];
                struct tm theTime;
                struct tm *ptr;
                time_t t;
                } IDnTimeArray[5];
                =============== ===LIKE THIS===
                but when I call

                =============== =======
                strcpy(IDnTimeA rray[2].previousEdate, "26.02.2007 ");
                strptime(IDnTim eArray[2].previousEdate, "%d.%m.%Y", IDnTimeArray[counter].ptr);
                =============== ==========

                I get a segmentation fault

                I dont understand why??

                Comment

                • upadhyad
                  New Member
                  • Jul 2008
                  • 15

                  #9
                  Hi all !!
                  my previous problem is also solved
                  I had not assigned the pointer in the structure to any value.....now i have rectified the mistake
                  thanks a lot everyone for helping me
                  this website rocks!!!!

                  Comment

                  Working...