Results 1 to 6 of 6

Thread: Converting a time in string format to minutes

  1. #1

    Thread Starter
    Frenzied Member ae_jester's Avatar
    Join Date
    Jun 2001
    Location
    Kitchener Ontario Canada Earth
    Posts
    1,545

    Converting a time in string format to minutes

    I'm using C++ Builder 6

    I'm trying to convert a string in the form "12:25", which represents a time of day, to minutes. So in this case "12:25" would become 745.

    I've never done anything like this, and I can't seem to get anything working. I'm sure its something extremely simple.

    I also need to convert a time in minutes to a time string, so 745 minutes would return "12:25", but I will attemp to do this part myself provided someone can assist me with the first part.

    Thanx for any help!

  2. #2
    Addicted Member HairyDave's Avatar
    Join Date
    Aug 2002
    Location
    Er...I can't remember.
    Posts
    196
    I don't know if there's an easier way, but couldn't you:

    1. Tokenise the string (using strtok or other) to be an hours string and a minutes string.
    2. Convert both to integers (atoi or other).
    3. ( hours * 60 ) + minutes = total

    Is that what you're looking for - or were you looking for something at a higher level?

    HD

  3. #3
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    If you know it's only going to be in the form "hh:mm" (including leading zeros, and 24-hour clock), you can do this:
    Code:
    inline char down(char c) { return c - '0'; }
    
    // ...
    
    string dt = "23:12";
    
    int minutes = down(dt[0]) * 10 * 60;
    minutes += down(dt[1]) * 60;
    minutes += down(dt[3]) * 10;
    minutes += down(dt[4]);
    ...or similar. I wouldn't *really* recommend that, since it has no error checking whatsoever. I'll leave that as an exercise for the reader
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    or maybe something like this

    int minutes = (dt[0]*10+dt[1])*60+dt[3]*10+dt[4]-0x7DD0;


    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Heh. You win
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Other way round is even easier:
    C:
    Code:
    void minstotime(int mins, char *buffer) {
      sprintf(buffer, "%02i:%02i", mins/60, mins%60);
    }
    C++:
    Code:
    void minstotime(int mins, string &str) {
      ostringstream oss;
      oss << setw(2) << setfill('0') << mins/60;
      oss << ':';
      oss << setw(2) << setfill('0') << mins%60;
      str = oss.str();
    }
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width