Trying to calculate difference between two times, won't subtract - c++

What my program does is that it first prints the current time, then the user presses enter. Afterwards it prints out the time again and calculates how long the user waited to press enter.
I can't get my time to subtract. I got the code for printing the local time from another question in stackoverflow.
#include <iostream>
#include <ctime>
using namespace std;
void main()
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf ( "Current local time and date: %s", asctime (timeinfo) );
cout << "Press enter" << endl;
cin.ignore();
time_t rawtime2;
struct tm * timeinfo2;
time ( &rawtime2 );
timeinfo2 = localtime ( &rawtime2 );
printf ( "Later local time and date: %s", asctime (timeinfo2) );
printf ( "You took %s", ( asctime (timeinfo2) - asctime (timeinfo) ) ); //math won't work here
printf ( " seconds to press enter. ");
cout << endl;
}

cout << "Elapsed time: " << rawtime2 - rawtime << " seconds" << endl;

printf ( "You took %s", ( asctime (timeinfo2) - asctime (timeinfo) ) ); //math won't work here
No. The difference between two char*s doesn't make any sense in this context. You really just need to take the difference of rawtime and rawtime2, which are already in whole numbers of seconds.
Also, you should not mix printf() and std::cout in c++ code as it is not idiomatic and makes code harder to read. As such, perhaps something like this would be better...
#include <ctime>
#include <iostream>
int main()
{
time_t rawtime;
struct tm* timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
std::cout << "Current local time and date: " << asctime(timeinfo);
std::cout << "Press enter\n";
std::cin.ignore();
time_t rawtime2;
struct tm* timeinfo2;
time(&rawtime2);
timeinfo2 = localtime(&rawtime2);
std::cout << "Later local time and date: " << asctime(timeinfo2);
time_t const timediff = rawtime2 - rawtime;
std::cout << "You took " << timediff << " seconds to press enter.\n";
}
Note:
main() should return int, not void.

This is a somewhat cleaner solution, the main difference is using int main() and return 0; at the end, main should never return void, it's a bad programming practice.
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
cout << "Current local time and date: " << asctime (timeinfo) ;
cout << "Press enter" << endl;
cin.ignore();
time_t rawtime2;
struct tm * timeinfo2;
time ( &rawtime2 );
timeinfo2 = localtime ( &rawtime2 );
cout << "Later local time and date: " << asctime (timeinfo2) << endl;
int prinAll = rawtime2 - rawtime;
cout << "You took " << prinAll << " seconds to press enter" << endl; //math won't work here
cout << endl;
return 0;
}

Related

Time of clock is not updating in c++

#include <ctime>
#include <iostream>
using namespace std;
int main() {
time_t t = time(0); // get time now
struct tm * now = localtime( & t );
cout << now->tm_mday << '-'//day
<< (now->tm_mon +1 ) << '-'//month
<< (now->tm_year +1900 )//year
<<endl
<<now->tm_hour//hour
<<'-'<<now->tm_min//min
<<'-'<< now->tm_sec//sec
<< endl;
return 0;
}
This piece of code gives me the system date and time,the only problem i have is that the time is not updating.
For example:= the time is not moving forward and is stuck at a fixed time like 1.33.10
You need to clear the console and display date again every a given amount of time.
Consider following example (compile with -std=c++11):
#include <iostream> // IO
#include <ctime> // time
#include <thread> // threads
#include <chrono> // chrono
int main(){
while(true){
system("clear"); // clear console
time_t t = time(0); // get time now
struct tm* now = localtime(&t);
std::cout << now->tm_mday << '-' // day
<< (now->tm_mon + 1) << '-' // month
<< (now->tm_year + 1900) << std::endl // year
<< now->tm_hour << '-' // hour
<< now->tm_min << '-' // min
<< now->tm_sec << std::endl // sec
;
// sleep for 1000 ms (=1s)
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
return 0;
}
In given code, we infinitelly loop the code that:
Clears the console
Recout the date
Waits 1000ms (1s)

Unixtime conversion to comparable time

I have strings of unixtime and I want to compare them by hours and minutes.
For example:
unixtime1 = "1327418725"
unixtime2 = "1327511595"
time_t convertUnixToTime(const char* unixtime){
time_t time = (time_t)atol(unixtime);
return time;
}
I use the function as such:
time_t time1 = convertUnixToTime("1327418725");
struct tm *timeinfo1;
timeinfo1 = localtime(&time1);
time_t time2 = convertUnixToTime("1327511595");
struct tm *timeinfo2;
timeinfo2 = localtime(&time2);
cout << "time: " << asctime(timeinfo1);// << endl;
cout << "time: " << asctime(timeinfo2);// << endl;
and I get the output:
time: Wed Jan 25 09:13:15 2012
time: Wed Jan 25 09:13:15 2012
I can't figure out why I'm getting the same time and also I want to be able to difftime() after to see how many HOURS and MINUTES they are apart.
Any suggestions/insight?
localtime() retuns a pointer to an object that is integral to the library. You are storing this pointer twice in timeinfo1 and timeinfo2.
You need to copy the struct tm
#include <ctime>
#include <cstdlib>
#include <iostream>
time_t convertUnixToTime(const char* unixtime){
time_t time = (time_t)atol(unixtime);
return time;
}
using namespace std;
int main()
{
time_t time1 = convertUnixToTime("1327418725");
struct tm timeinfo1;
timeinfo1 = *localtime(&time1);
time_t time2 = convertUnixToTime("1327511595");
struct tm timeinfo2;
timeinfo2 = *localtime(&time2);
cout << "time: " << asctime(&timeinfo1);// << endl;
cout << "time: " << asctime(&timeinfo2);// << endl;
return 0;
}

get future time value

#include <time.h>
#include <iostream>
using namespace std;
int main()
{
time_t current = time(0);
cout << ctime(&current) << endl;
return 0;
}
How can I get the future time, say 1 hour later, from the current time?
time(2) returns the number of seconds since 1970-01-01 00:00:00 +0000 (UTC). One hour later would be current + 3600.
time_t current = time(0);
time_t inOneHour = current + (60*60); // 60 minutes of 60 sec.
cout << "Now: " << ctime(&current) << "\n"
<< "In 1 hour: " << ctime(&inOneHour)
<< "\n";

parse localtime in c++

Is there an easy "beginner" way to take the current time using <ctime> to a Date object that has
int month
int day
int year
for it's member variables? Thanks.
time_t tt = time(NULL); // get current time as time_t
struct tm* t = localtime(&tt) // convert t_time to a struct tm
cout << "Month " << t->tm_mon
<< ", Day " << t->tm_mday
<< ", Year " << t->tm_year
<< endl
The tm struct ints are all 0-based (0 = Jan, 1 = Feb) and you can get various day measures, day in month (tm_mday), week (tm_wday) and year(tm_yday).
If there is localtime_r then you should use localtime_r rather than localtime since this is the reentrant version of localtime.
#include <ctime>
#include <iostream>
int main()
{
time_t tt = time(NULL); // get current time as time_t
tm tm_buf;
tm* t = localtime_r(&tt, &tm_buf); // convert t_time to a struct tm
std::cout << "Month " << t->tm_mon
<< ", Day " << t->tm_mday
<< ", Year " << t->tm_year
<< std::endl;
return 0;
}

C++ Timer suggestions?

I'm trying to create a timer for fun. I'm running into the trouble of the computer being too fast for counting every second; therefore it prints " cout << "the time" << theTime << endl; " like 10 times, but I want it to only print once. Any suggestions to what I should do or looking into? Thanks.
just thought of a potential solution: using the difftime to check if the new time is different from previous
void activateTimer(int Hours)
{
double seconds;
//current time
//figure out what each lines does http://www.cplusplus.com/reference/ctime/gmtime/
time_t rawtime;
struct tm * ptm;
time(&rawtime);
ptm = localtime(&rawtime);
cout << "The current time is: " << (ptm->tm_hour - 12) << ":" << (ptm->tm_min)<<endl; //time hour time minute
cout << "The stop time is:" << (ptm->tm_hour - 12 + Hours) << ":" << (ptm->tm_min)<<endl;
cout << "Commencing countdown timer." << endl;
while ((ptm->tm_hour - 12) != ((ptm->tm_hour) + (Hours))) //checking if the hours match
{
//declaration
time_t rawtime; //calling the library raw time
struct tm * ptm; //strcuture of 7 things tm_sec, tm_min, tm_hour, pointed by ptm
time(&rawtime); //time=rawtime(null)
ptm = localtime(&rawtime);
if (ptm->tm_sec == 59 || ptm->tm_sec == 60 || ptm->tm_sec == 61)
{
//intializiation
int theTime = ((ptm->tm_hour - 12 + Hours) * 60) + (ptm->tm_min);
cout << "the time" << theTime << endl;
}
}
}