Reputation: 177
Currently I am trying to add minutes to current time, but how do I do it? I read through some tutorials and all but still have no idea how to do it.
So my code goes..
time_t now = time(0);
tm* localtm = localtime(&now);
cout << "Current time : " << asctime(localtm) << endl;
My program will sort of "run" in minutes which is + 1 minute every loop..
So lets say there is 255 loops and it is 255 minutes.. I store it in Minute.
I tried adding it this way but the time remained the same as the current time..
localtm->tm_min + Minute;
mktime (localtm);
cout << "End of program time : " << asctime(localtm) << endl;
I am wondering how should I do it. Can anyone help?
Upvotes: 4
Views: 10310
Reputation: 71
C++ 11:
int main(int argc,char* argv[])
{
std::chrono::system_clock::time_point time_now =
std::chrono::system_clock::now();
time_now += std::chrono::hours(10);
time_t c_time_format = std::chrono::system_clock::to_time_t(time_now);
std::string str_time = std::ctime(& c_time_format);
std::cout<<str_time<<std::endl;
return 0;
}
To compile this code ,you shoud include headrs chrono ctime .
You can use "seconds(val),minutes(val),hours(val) etc"
With any question,you can visit the following : http://www.cplusplus.com/reference/chrono/system_clock/
Upvotes: 7
Reputation: 1454
int main()
{
time_t now = time(0);
size_t Minutes = 255;
time_t newTime = now + (60 * Minutes);
struct tm tNewTime;
memset(&tNewTime, '\0', sizeof(struct tm));
localtime_r(&newTime, &tNewTime);
cout << asctime(&tNewTime) << endl;
}
Upvotes: 7