-
time input 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 don't know how to get that value from the user in hh:mm:ss format and calculate the difference between different inputs. i have no clue of that. could you guys give me some tips on that?
-
There are no direct functions to handle time in that way in the standard C & C++ languages. Although, I suppose difftime() could be used for calculating the differenc, if you wish.
Reading time would be easiest to do by reading a string and then splitting it, I would think, but you could also create a time-class and specialized << and >> operators if you wish [which is made much easier if you write a function that takes a string and makes it into your internal format and from internal to string - then you can just use those string to/from functions to perform the input/output functions].
The internal format of your time can be either h, m, s or seconds since midnight or some such - the latter is obviuously easier for math, but requires a bit more effort to format the output [not a whole lot tho].
--
Mats
-
please do not take my questions as irritation as i am beginner :)
you said that there are no functions to handle time this way but can i still take time from the user in hh:mm:ss format and then i can use difftime() function or the other solution that you have specified? if yes, can you give me one example of that so that it can be clear to me.
thanks
-
Well, difftime requires a full set of year, month, day, hour, minute and second. It is probably more complicated to get that set up than it is to calculate the time difference from hour, minute and second itself. I would suggest that you use a internal format of "seconds since midnight" and then calculate back hour, minute and second from that.
--
Mats
-
could you tell me how can i make system time refreshing every second?
this is the code that displays the system time
Code:
#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;
}