Challenge Answer?


#1

Here is my attempt of the challenge it seems to work but any advice is much appreciated.

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

void whatDateWillItBe (int seconds)
{
struct tm now;

long secondsSince1970 = time(NULL);

long secondsSince1970AndSome = secondsSince1970 + seconds;

localtime_r(&secondsSince1970AndSome, &now);

printf("The date in %i seconds from now will be %d/%d/%d",seconds,now.tm_mday,now.tm_mon + 1,now.tm_year+1900);

}

int main (int argc, const char * argv[])
{

int mySeconds = 4000000;

whatDateWillItBe(mySeconds);


return 0;

[/code]

Cheers, great book so far by the way!


#2

I just checked my answer using the objects chapter. :sunglasses:


#3

I did similar but left it all in main. Also we differ on date format, I used mm-dd-yyyy.

I am liking this book too.


#4

Shouldn’t you add 1970 instead of 1900?


#5

Never mind. I see the error of my ways.

Thanks.


#6

Pretty similar but I also did all of mine in main:

[code] // challenge

struct tm sFuture;
long moreSecondsSince1970 = time(NULL);
long future = moreSecondsSince1970 + 4000000;

localtime_r(&future, &sFuture);
printf("4,000,000 seconds in the future will be: %d-%d-%d\n", sFuture.tm_mon + 1, sFuture.tm_mday, sFuture.tm_year + 1900);[/code]

#7

Here’s what I came up with. I just messed around printing different data from the tm struct until I had a handle on how it worked, then the solution was pretty easy.

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

int main (int argc, const char * argv[])
{
//Find out what time it is now
//How long since the Epoch?
long secondsSinceEpoch = time(NULL);

// Add 4 million seconds to the current time
secondsSinceEpoch += 4000000;

//Make a struct with the our time 4 million seconds from now
struct tm now;
localtime_r(&secondsSinceEpoch, &now);

// Print the date in 4 million seconds
printf("In four million seconds the date will be %d-%d-%d\n", now.tm_mon, now.tm_mday, 1900 + now.tm_year);

return 0;

}[/code]


#8

[quote=“macshome”]Here’s what I came up with. I just messed around printing different data from the tm struct until I had a handle on how it worked, then the solution was pretty easy.

... // Print the date in 4 million seconds printf("In four million seconds the date will be %d-%d-%d\n", now.tm_mon, now.tm_mday, 1900 + now.tm_year); ...[/quote]
Only… you needed to add +1 to the tm_mon, I think?

... // Print the date in 4 million seconds printf("In four million seconds the date will be %d-%d-%d\n", 1 + now.tm_mon, now.tm_mday, 1900 + now.tm_year); ...
Also, is there a reason for typing 1900 + now.tm_year, instead of now.tm_year + 1900? Is that to avoid incrementing now.tm_year with 1900 accidentally?


#9

[quote=“whakkee”][quote=“macshome”]Here’s what I came up with. I just messed around printing different data from the tm struct until I had a handle on how it worked, then the solution was pretty easy.

... // Print the date in 4 million seconds printf("In four million seconds the date will be %d-%d-%d\n", now.tm_mon, now.tm_mday, 1900 + now.tm_year); ...[/quote]
Only… you needed to add +1 to the tm_mon, I think?
[/quote]

Ahh… good catch!

Since it’s just addition the order of the values doesn’t matter here. I put 1900 first as my thought was that I was starting there and then adding the value from the struct.


#10

so I managed to complete the challange but I’m a little confused with the the time function (). I understand what the function does but I don’t get why you have to pass NULL. If the only way of using this function is to pass NULL - why wasn’t NULL built inside the function ? I understand the logic of passing by value but surely this function only returns a value. I’m obviousley missing the point somewhere and would really appreciate some guidance. Thanks.
Love the book - but I hate NULL !!


#11

[quote=“peterT”]so I managed to complete the challange but I’m a little confused with the the time function (). I understand what the function does but I don’t get why you have to pass NULL. If the only way of using this function is to pass NULL - why wasn’t NULL built inside the function ? I understand the logic of passing by value but surely this function only returns a value. I’m obviousley missing the point somewhere and would really appreciate some guidance. Thanks.
Love the book - but I hate NULL !![/quote]

As I understand it time() can take a pointer as an argument, if you give it one then it puts the value of the current time in it. By calling time(NULL) it instead returns the time and we can assign it to our variable.


#12

@macshome - ok, so time() has to have a pointer passed for the function to work. We need the return value but don’t have a pointer to pass so we pass NULL. The bit I don’t get is when you quoted ’ if you give it one then it puts the value of the current time in it’ - If I give it a pointer, what address would it be pointing to and what would I want it to return ? would really appreciate a little example code. Many thanks.


#13

I found this page on StackOverflow http://stackoverflow.com/questions/2311014/srandom-time-null to be enlightening on the subject. It seems that time() always returns the current time in seconds, but you can also give it a pointer if you like for some pass by reference.

If I were you I wouldn’t dwell on this too much. The book is about to get into Foundation and Objective-C and then things like this become much simpler to code!


#14

@macshome - Thanks for reply and stack overflow post was helpful - after hours of fiddling I’ve finally come up with the following:

long currentYear; time(&currentYear); printf("The current year is %ld", currentYear / 31556926 + 1970 ); // current seconds divided by 31556926 (seconds in a year) + 1970

Ive also come up with some simple code that gives me an understanding of NULL.

Thanks again


#15

I don’t yet know enough to know if this question is of style or substance…

In the solution posted above @gazlong wrote:

And in main() wrote:

My question is: Why not use the same name for the variable passed from main() to whatDateWillItBe() ? To be clear, use:

It seems like this would be a maintenance win, but does it break a rule we don’t know about yet?

I’m not picking on @gazlong here – I couldn’t have written my answer without that help – I’m just curious about the choice.

Thanks!


#16

The naming is more a question of style. It makes sense to use descriptive names that have a clear meaning, but you can call them whatever you want really.


#17

So, if it’s helpful for me to keep the names the same, that’s cool, but others may not need/want that consistency? I can work with that.


#18

time() is designed to either populate a struct passed via pointer, or return a newly-allocated struct if a NULL pointer is passed.

I suspect that time() was designed to take a struct pointer for memory and time efficiency: remember, C was defined when computers were vastly underpowered relative to even the weakest modern smartphone. If you had to call time() frequently, it was (presumably) more efficient to create one such memory block and use it over and again.

C doesn’t have any mechanism for passing a different number of arguments, so if a function takes an argument, you’ve got to provide one. In this case, if the compiler permitted you to call time() without an argument, the variable that the argument ordinarily resides in would have garbage data, which time() would happily attempt to treat as a pointer and thus fill a random block of memory with time-related data.

(Edit: missed the fact there was an entire page of discussion after the time() question. So, ignore my now-mostly-irrelevant post if you wish.)


#19

[quote=“n8tr0n”]Never mind. I see the error of my ways.

Thanks.[/quote]

Yeah that one got me too. I like seeing all the different solutions! I’m one of those guys who always misinterprets the question and that got me off to a rocky start. For example, I read “time in 4 million seconds” and automatically finish off the sentence with “since 1970 in Greenwich” instead of “from now (current date and time)”. Good forum.


#20

I tried to better organize the code, leaving more readable.

#include <stdio.h>
#include <time.h>

void printDateIn (long DateInMSeconds){
    
    // Use (-) sign to have past dates.
    long miliSeconds =  time(NULL) + DateInMSeconds;
    struct tm desiredDate;
    localtime_r(&miliSeconds, &desiredDate);
    
    int day = desiredDate.tm_mday;
    int month = desiredDate.tm_mon+1;
    int year = desiredDate.tm_year+1900;
    
    printf("The date in \"%ld\" seconds is %d / %d / %d. \n", DateInMSeconds, month, day, year );

}


int main (int argc, const char * argv[])
{
    printDateIn(4000000);
    return 0;
}

/// Printed result:
/// The date in "4000000" seconds is 2 / 22 / 2012.