Challenge Answer?

Moderator: MikeyWard

Challenge Answer?

Postby gazlong » Sun Oct 23, 2011 4:47 am

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

Code: Select all
#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;


Cheers, great book so far by the way!
gazlong
 
Posts: 9
Joined: Wed Sep 14, 2011 4:54 pm

Re: Challenge Answer?

Postby gazlong » Sun Oct 23, 2011 5:42 am

I just checked my answer using the objects chapter. 8-)
gazlong
 
Posts: 9
Joined: Wed Sep 14, 2011 4:54 pm

Re: Challenge Answer?

Postby BrianH » Tue Oct 25, 2011 11:35 pm

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.
BrianH
 
Posts: 11
Joined: Sun Oct 23, 2011 9:23 am

Re: Challenge Answer?

Postby n8tr0n » Sun Oct 30, 2011 5:37 pm

Shouldn't you add 1970 instead of 1900?
n8tr0n
 
Posts: 2
Joined: Sun Oct 30, 2011 5:35 pm

Re: Challenge Answer?

Postby n8tr0n » Sun Oct 30, 2011 6:27 pm

Never mind. I see the error of my ways.

Thanks.
n8tr0n
 
Posts: 2
Joined: Sun Oct 30, 2011 5:35 pm

Re: Challenge Answer?

Postby jchristie07 » Mon Oct 31, 2011 9:29 pm

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

Code: Select all
    // 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);
jchristie07
 
Posts: 7
Joined: Sun Oct 30, 2011 7:43 pm

Re: Challenge Answer?

Postby macshome » Mon Nov 07, 2011 8:49 am

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: Select all
#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;
}
macshome
 
Posts: 122
Joined: Sun Nov 06, 2011 1:08 pm

Re: Challenge Answer?

Postby whakkee » Fri Nov 11, 2011 9:08 am

macshome wrote: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: Select all
...
    // 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);
...

Only... you needed to add +1 to the tm_mon, I think?

Code: Select all
...
    // 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?
whakkee
 
Posts: 34
Joined: Mon Aug 08, 2011 5:18 pm
Location: Appsterdam

Re: Challenge Answer?

Postby macshome » Fri Nov 11, 2011 11:25 am

whakkee wrote:
macshome wrote: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: Select all
...
    // 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);
...

Only... you needed to add +1 to the tm_mon, I think?


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.
macshome
 
Posts: 122
Joined: Sun Nov 06, 2011 1:08 pm

Re: Challenge Answer?

Postby peterT » Tue Nov 15, 2011 2:58 pm

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 !!
peterT
 
Posts: 3
Joined: Tue Nov 15, 2011 1:28 pm

Next

Return to 10. Structs

Who is online

Users browsing this forum: No registered users and 1 guest