Click here to Skip to main content
15,868,292 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hI..
I got a very simple question..
i got my current time in "HH:MM:SS"format
how i 'm going to calculate the new time if i got a new value in second format.
for example, my current time now is 16:36:30. I got 80 second and need to add this value to my current time.
What is the simply way to calculate the new time
Posted
Comments
Volynsky Alex 22-Mar-13 5:02am    
Let's read about time_t here:
http://en.cppreference.com/w/cpp/chrono/c/time_t
and here:
http://en.wikibooks.org/wiki/C_Programming/C_Reference/time.h

For example you can try to settle it, by following way:
C++
#include <time.h>
#include <stdio.h>

int main()
{
    time_t your_time = ....

    struct tm your_time_tm = *localtime( &your_time);


    struct tm then_tm = your_time_tm;
    then_tm.tm_sec += 100;    

    mktime( &then_tm);      // let's normalize it

    printf( "%s\n", asctime( &your_time_tm));
    printf( "%s\n", asctime( &then_tm));

    return 0;
}
 
Share this answer
 
v2
1. Convert the string to hours, minutes and seconds
2. combine these three numbers to calculate the total number of seconds
3. Add 80
4. Split up the resulting value back to hours, minutes and seconds
5. convert that back to string.

There may be shortcuts or faster ways of doing this, but all programatical timers internally convert to a single number representing the number of seconds, milliseconds or an even smaller unit, that have passed since some predetermined starting point. Doing it this way will make it easier for you to deal with these other formats.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900