I am trying to write a function that takes in a parameter which is the offset days, and returns the date those many offset days from now. I can easily get the current date from below
#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_year + 1900) << '-'
<< (now->tm_mon + 1) << '-'
<< now->tm_mday
<< endl;
}
My question is if I change the now->tm_mday to now->tm_mday – offset, is it smart enough to do the month change or the year change as they may change.
No —
(now->tm_year + 1900),(now->tm_mon + 1)andnow->tm_mdayare separate expressions and adding a new arithmetic operation to one will not affect the others.Apply the offset to
tinstead, which is an integral value representing seconds since UNIX epoch. Then the change will carry through to thetmstructure and, ultimately, each of your output expressions: