Click here to Skip to main content
13,046,045 members (45,772 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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
Let's read about time_t here:
and here:
Rate this: bad
Please Sign up or sign in to vote.

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;
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170713.1 | Last Updated 27 Mar 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100