I have to find the date 4,000,000 seconds from now. I can get the correct answer if I add 4,000,000 to secondsSince1970, but I was wondering why it doesn’t work if I add 4,000,000 to now.tm_sec?
int main(int argc, const char * argv[])
{
long secondsSince1970 = time(NULL) + 4000000;
struct tm now;
localtime_r(&secondsSince1970, &now);
printf("The date from 4,000,000 seconds from now is %i-%i-%i\n", now.tm_mon + 1, now.tm_wday, now.tm_year + 1900);
}
Output:
The date is 10-1-2012
int main(int argc, const char * argv[])
{
long secondsSince1970 = time(NULL);
struct tm now;
localtime_r(&secondsSince1970, &now);
now.tm_sec += 4000000;
printf("The date from 4,000,000 seconds from now is %i-%i-%i\n", now.tm_mon + 1, now.tm_wday, now.tm_year + 1900);
}
Output: The date is 8-4-2012
Your
struct tmis just a bunch of variables that get filled in bylocaltime_r. After the call tolocaltime_r, assigning to one of these variables will not magically make the other ones change their values.