My Ch. 10 Challenge Solution - Spoiler Alert


I’ll admit, I was stumped on this one.

Here’s my code:

[code]#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int main (int argc, const char * argv[])
long futureSeconds = 4000000;
long secondsSince1970 = time(NULL);
long futureTime = secondsSince1970 + futureSeconds;
printf(“It has been %ld seconds since 1970.\n”, secondsSince1970);

struct tm future;
localtime_r(&futureTime, &future);
printf("The date is %d-%d-%d.\n", future.tm_mon + 1, future.tm_mday, future.tm_year + 1900);

return 0;



It has been 1372037468 seconds since 1970. The date is 8-9-2013.


I don’t mean to be a total n00b, but can you PLEASE explain this code in depth ? I just really want to understand it, because frankly I don’t really get it. Sorrry :cry:


It’s been a while since you asked about this, but I figured I’d attempt to explain his code for your benefit, but also for my own:

To start off, you need to figure out what time it is now. The challenge asks for the date (and you could also do the time) 4 million seconds from right now. In order to get that time, you need to start with the time now and add 4 million seconds. ctmpkmlec4 decided to store the 4 million seconds in a variable called futureSeconds, so he creates a long integer and assigns it a value of 4,000,000. This way, you can find out what time it is any number of seconds in the future by just changing this value. He then creates a long integer variable called secondsSince1970 and uses the time() function to store the number of seconds since 1970 until now (almost exactly as noted in the book). He then adds the two variables together to get the number of seconds 4,000,000 seconds from now and prints a string to that effect.

In the next block, he creates a variable based on the time structure mentioned in the book called “future”. This structure, as shown in the book, can contain 11 different components of time, namely - seconds, minutes, hours, days, months, years, etc. This is just going to be a placeholder for the time so that we can pull the individual components out of whatever time we give it. That’s what the next function is for: localtime_r takes in an address where we’ve stored the time we want to figure out and then another address for the struct we created earlier (currently empty) where we can store the time components (minutes, hours, days, etc.) and spits each of those components out and saves them into our struct. When the localtime_r function is done, we should have a future structure that has all of the minutes, hours, days, etc. since 1970 plus an additional 4 million seconds.

The very last string, just prints out that statement to the log while adding 1 to the month (since it starts at 0 for January instead of 1), the day of the month, and the number of years since 1900 + 1900 (since leaving off the 1900 simply gives you 113 because that’s how many years it’s been since).

I hope this helps. I’ve tried to be really, really explicit (moreso for my own understanding) so please don’t take it as me talking down or anything. I just want to make sure I touched on everything. :slight_smile:

Let me know if you have any other questions or if something I said was more confusing.


dpkonofa, thanks very much for that. Great walk through of the code, I was struggling with that one too. Thanks :slight_smile:


No problem. I have to spend a lot of time doing this for myself because I feel like walking through code gives me confidence that I’m understanding it. I just wish it was this easy for me on some of the harder programs. :-/