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 <time.h> 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?

Recommended Answers

All 12 Replies

Member Avatar for dmmckelv

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

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) ?

Member Avatar for dmmckelv

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.

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

Member Avatar for dmmckelv

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

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.

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

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

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;
}

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 remove iostream

> 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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.