|
-
Nov 20th, 2002, 09:05 PM
#1
Thread Starter
Frenzied Member
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!
-
Nov 21st, 2002, 03:59 AM
#2
Addicted Member
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
-
Nov 21st, 2002, 05:42 AM
#3
Monday Morning Lunatic
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
-
Nov 21st, 2002, 05:58 AM
#4
transcendental analytic
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.
-
Nov 21st, 2002, 06:26 AM
#5
Monday Morning Lunatic
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
-
Nov 22nd, 2002, 08:50 AM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|