C++ Timer suggestions? - c++

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

Related

How to get local time c++

I need to get the current time (hour,minutes and seconds) and add 30 minutes. In final i need to save the result to an integer variable.
How it's possible to do?
My code at the moment:
int main()
{
time_t currentTime;
struct tm *localTime;
time( &currentTime );
localTime = localtime( &currentTime );
int Hour = localTime->tm_hour;
int Min = localTime->tm_min;
int Sec = localTime->tm_sec;
std::cout << "Execute at: " << Hour << ":" << Min << ":" << Sec << std::endl;
return 0;
}
Thanks
you can do the following to get time 30 minutes ahead of local time:
time_t currentTime;
struct tm *localTime;
time( &currentTime );
currentTime += 1*30*60;
localTime= localtime(&utc);
The localTime structure now has 30 minutes ahead of current local time.You can get the values in integers as you were doing before.
int Hour = localTime->tm_hour;
int Min = localTime->tm_min;
int Sec = localTime->tm_sec;
In case you need string out of it,you can do
std::string strAheadTime = asctime(localTime);
You can achieve the same like this -
#include <iostream>
#include <ctime>
int main()
{
time_t currentTime;
struct tm *localTime;
time( &currentTime ); // Get the current time
localTime = localtime( &currentTime ); // Convert the current time to the local time
int Hour = localTime->tm_hour;
int Min = localTime->tm_min;
int Sec = localTime->tm_sec;
std::cout << "The current time is : " << Hour << ":" << Min << ":" << Sec << std::endl;
//increase local time by 30 minutes and adjust minutes and hour.
int IncMin = Min + 30;
if(IncMin >= 60){
int res = IncMin/60;
int newMin = IncMin % 60;
Hour += res;
if(Hour > 23){
Hour = 00;
}
std::cout << "The updated time is : " << Hour << ":" << newMin << ":" << Sec << std::endl;
}
return 0;
}

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

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

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

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