get future time value - c++

#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";

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

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

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