954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

time calculation help

hi guys,

I am making a program in which I have to enter start date and time and stop date and time in format like 01:05:07 15:26:57

I know that i need to use function for that but i don't know how can i calculate the difference between time. i have no clue of that. could you guys give me some tips on that?

rajsharma_85
Newbie Poster
14 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

This may not be the best way to do it but, you could build a function to convert the times into seconds and use subtraction. Your function would have to tokenize the time format using the : as a break.

Actually, you don't even need to convert the times into seconds. Just tokenize the time into hours and minutes then subtract. Your output will already be in the correct format.
Mark

dmmckelv
Light Poster
33 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

but i am already taking input from the user in hours minutes and seconds in hh:mm:ss format. but i don't know how to calculate the difference between two different inputs. i don't know that particular equation

do you mean to say (hh:mm:ss)-(hh:mm:ss) ?

rajsharma_85
Newbie Poster
14 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

Sure, use a tokenizer to separate HH, MM, and SS from HH:MM:SS. Then subtract HH from your other HH, MM from MM, etc. As long as it is in 24hr format it should work. You will have to write some code to decrement HH and MM if you need to borrow. Once you have done the math, put the answer back into a string in the format of HH:MM:SS or whatever other format you need.

Mark

I don't know what the deal is with the smileys but I mean ": S" minus the space." Stupid smileys.

dmmckelv
Light Poster
33 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

okay. so you are trying so say to use (hh1-hh2) and (mm1-mm2) and (ss1-ss2) format?

rajsharma_85
Newbie Poster
14 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

yep! well HH(Stop)-HH(Start), etc..

dmmckelv
Light Poster
33 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

thanks for your help dmmclev.

can i ask you one more question? what should i do if i want to refresh that time every second?

for example, i am my program is recording a channel between start and stop time and i want to show the amount of time that has been recorded and make it refreshing every second.

rajsharma_85
Newbie Poster
14 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

Each second add 1 to a counter and output that counter.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 


I sense a disturbance in the ethernet, a sense that the same question has been asked elsewhere

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

sorry man, i didn't know the rules. i posted it elsewhere because i didn't know which forum is faster in replying and i was really in hurry to get the help

rajsharma_85
Newbie Poster
14 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

could you tell me how to make system time refreshing every second

#include <iostream>
#include <stdio.h>
#include <io.h>
#include <conio.h>

using namespace std;

int main(void)
{
   FILE *stream;
   std::ftime ft;

   if ((stream = fopen("TEST.$$$","wt")) == NULL)
   {
      fprintf(stderr, "Cannot open output file.\n");
      return 1;
   }
   getftime(fileno(stream), &ft);
   cout<<"File time: "<<ft.ft_hour<<":"<<ft.ft_min<<":"<<ft.ft_tsec * 2;
   cout<<endl<<"File date: "<<ft.ft_month<<":"<<ft.ft_day<<":"<<ft.ft_year+1980;
   fclose(stream);
   getch();
   return 0;
}
rajsharma_85
Newbie Poster
14 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

Add header time.h and make a loop that gets the current time using time() . You can then test if the time has changed and output it.

By the way, if you are going to use 90% C (and old non-standard C at that) you should just make a C program by converting the cout to printf() and removeiostream

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

> i posted it elsewhere because i didn't know which forum is faster in replying
> and i was really in hurry to get the help
Yes, and that is exactly the message it sends to people who help. It tells us that you've labelled the post as URGENT .

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You