Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C Linux time
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 23:40pm
Comments
Volynsky Alex at 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
Rate this: bad
good
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;
}
  Permalink  
v2
Rate this: bad
good
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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,382


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 27 Mar 2013
Copyright © CodeProject, 1999-2014
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