Total Second out of timestamps

This is a discussion on Total Second out of timestamps within the C++ Programming forums, part of the General Programming Boards category; Hi UAll I want to token out the int from the string "10:20:30", so in these case i have a ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    68

    Question Total Second out of timestamps

    Hi UAll

    I want to token out the int from the string "10:20:30", so in these case i have a delimiter as ':'.

    Why do i want to do these, because i have to find out the total second in time stamp.

    Please Help me
    I tried many way but it does not work, may be i am not thinking right.

    Here is you go this is what i am trying. I don't know what i am doing wrong

    Please look at the code and help me
    Thanks
    ---------------------------------------------------------------------------------

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int strchrall (const char *z,int b)
    {
      int rValue = 0;
      int Signal = 1;
      b = (char) b;
    
      do{
        if(*z == b)
        {
          rValue = rValue + 1;
        }
        if (*z++ == '\0')
        {
            Signal = 0;
        }
      } while(Signal);
      return rValue;
    }
    
    
    int main ()
    {
      int all = 0;
      int Mintues = 0;
      int Second = 0;
      int Hours = 0;
    
      // these will tell me how many times
     // the delimeter occures in a string
      all =  strchrall ("0:00:00",':');
    
     // the string can be from the following
     // only second 60
     // minutes and seconds 01:60
     // hours, minutes and seconds 10:20:30
     // Having problem figuring out these logic
     // All i want out of this is Total Second from time format....
     for(int nIndex = 0, nIndex <= all;nIndex++)
    {
        if(all == 0)
       {
           Second = atoi(strtok(0:00:00,":"));
       }
       else if (all == 1)
       {
           Mintues = atoi(strtok(0:00:00,":"));
           char *temp;
           while(temp != NULL )
           {
                          
           }
       }
    }
    
    
      return 0;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    Like so
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int timestamp ( char *t ) {
        int res;
        res = atoi(t);
        while ( (t=strchr(t,':')) != NULL ) {
            t++;
            res = res * 60 + atoi(t);
        }
        return res;
    }
    
    int main (void) {
        printf( "T1=%d\n", timestamp("22") );
        printf( "T2=%d\n", timestamp("1:23") );
        printf( "T3=%d\n", timestamp("2:34:56") );
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    68

    Thanks You

    This is nice thank you, and was stuck because of it...

    once again thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with day of the week program
    By Punkakitty in forum C++ Programming
    Replies: 10
    Last Post: 01-14-2009, 05:55 PM
  2. Replies: 8
    Last Post: 11-03-2008, 08:48 PM
  3. long problem
    By loko in forum C Programming
    Replies: 28
    Last Post: 07-22-2005, 09:38 AM
  4. Loops
    By Dt7 in forum C++ Programming
    Replies: 3
    Last Post: 10-23-2004, 06:11 PM
  5. Replies: 4
    Last Post: 04-22-2003, 12:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21