How to get local time c++ - 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;
}

Related

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 time difference in milliseconds in c++

I'm trying to get the time difference between two time stamps. Thread is sleeping between the two time stamps. but when i get the difference, it doesn't give me the time which the thread was sleeping. My code is below.
#include <iostream>
#include <locale>
#include <sys/time.h>
#include <cstdlib>
#include <unistd.h>
using namespace std;
int timeInMilli();
int main()
{
timeval t;
timeval t2;
gettimeofday(&t, NULL);
gettimeofday(&t2, NULL);
std::string buf(20, '\0');
std::strftime(&buf[0], buf.size(), "%H:%M:%S:", localtime(&t.tv_sec));
std::string hr = buf.substr(0, 2);
std::string min = buf.substr(3, 2);
std::string sec = buf.substr(6, 2);
/*std::cout << hr << '\n';
std::cout << min << '\n';
std::cout << std::atoi(sec.c_str()) << '\n';*/
int a = timeInMilli();
usleep(10);
int b = timeInMilli();
cout << b-a << endl;
}
int timeInMilli()
{
timeval t;
gettimeofday(&t, NULL);
string buf(20, '\0');
strftime(&buf[0], buf.size(), "%H:%M:%S:", localtime(&t.tv_sec));
string str_hr = buf.substr(0, 2);
string str_min = buf.substr(3, 2);
string str_sec = buf.substr(6, 2);
int hr = atoi(str_hr.c_str());
int min = atoi(str_min.c_str());
int sec = atoi(str_sec.c_str());
int milli = t.tv_usec/1000;
/*cout << hr << endl;
cout << min << endl;
cout << sec << endl;
cout << milli << endl;*/
int timeInMilli = (((hr * 60) + min) * 60 + sec) * 1000 + milli;
return timeInMilli;
}
usleep(10);
means that you pause for 10 µsec and not 10 msec since usleep works with microseconds. try with
usleep(10000);

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