12,403,391 members (72,370 online)
Rate this:
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 21-Mar-13 22:40pm
Volynsky Alex 22-Mar-13 5:02am

http://en.cppreference.com/w/cpp/chrono/c/time_t
and here:
http://en.wikibooks.org/wiki/C_Programming/C_Reference/time.h

Rate this:

## Solution 1

For example you can try to settle it, by following way:
```#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;
}```
v2
Rate this:

## Solution 2

1. Convert the string to hours, minutes and seconds
2. combine these three numbers to calculate the total number of seconds
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.

Top Experts
Last 24hrsThis month
 OriginalGriff 353 Karthik Bangalore 230 Richard MacCutchan 177 Suvendu Shekhar Giri 175 RossMW 85
 OriginalGriff 7,346 Karthik Bangalore 3,127 ppolymorphe 3,080 Richard MacCutchan 2,308 F-ES Sitecore 2,222

Web02 | 2.8.160721.1 | Last Updated 27 Mar 2013