Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i use tm structure to add minutes to current time?

Tags:

c++

time

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?

like image 507
John Avatar asked Nov 06 '25 21:11

John


1 Answers

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;
}
like image 84
Harikrishnan R Avatar answered Nov 08 '25 13:11

Harikrishnan R