Challenge: The date 4,000,000 Secs in the Future

Here is my solution for the challenge, the only problem is the year output, where I would expect ‘2014’, the console shows ‘114’, can someone explain why this is?

CONSOLE OUTPUT:

1390477089 = seconds since 1970
1394477089 = Since 1970 + 4 Million Seconds
The future date will be: Day: 10 // Month: 3 // Year: 114
Program ended with exit code: 0

Any additional comments would be useful, thanks!

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

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

long noOfSecsSince1970 = time(NULL);
printf("%ld = seconds since 1970\n", noOfSecsSince1970);

long fourMilSecsInTheFuture = time(NULL) + 4000000;
printf("%ld = Since 1970 + 4 Million Seconds\n", fourMilSecsInTheFuture);

struct tm now;
localtime_r(&fourMilSecsInTheFuture, &now);
int trueMonth = now.tm_mon + 1;
printf("The future date will be: Day: %d // Month: %d // Year: %d\n", now.tm_mday, trueMonth, now.tm_year);
    
return 0;

}[/code]

The year value is returned as the number of years since the year 1900. In this case, there have been 114 years since the year 1900.

I added 1900 to the year portion of the struct like I added 1 to the month.

Here is my code:

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


int main(int argc, const char * argv[])
{
	long currentPlus4Mil = time(NULL) + 4000000;
	
	struct tm futureTime;
	
	localtime_r(&currentPlus4Mil, &futureTime);
	
	printf("The date 4,000,000 seconds from now will be %d-%d-%d.\n", futureTime.tm_mon + 1, futureTime.tm_mday, futureTime.tm_year + 1900);
			
	return 0;
}

Output:
The date 4,000,000 seconds from now will be 3-16-2014.
Program ended with exit code: 0

This is my solution for the challenge (we need add 1900 to the year value because the year returned in the time struct is the years after 1900, 114 in my case):

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

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

    int futureSeconds = 4000000;
    long futureTime = time(NULL) + futureSeconds;
    struct tm futureStruct;

    localtime_r(&futureTime, &futureStruct);
    printf("The date after %d seconds is %d-%d-%d\n", futureSeconds, futureStruct.tm_mon + 1, futureStruct.tm_mday, futureStruct.tm_year + 1900);
    return 0;
}

Output
The date after 4000000 seconds is 4-10-2014
Program ended with exit code: 0

could someone explain why we’re using “time(NULL)” please?

I’m not sure i get that bit…why NULL? surely that doesn’t pass a value…

[quote=“kev1986”]could someone explain why we’re using “time(NULL)” please?

I’m not sure i get that bit…why NULL? surely that doesn’t pass a value…[/quote]

I believe (based on http://en.cppreference.com/w/c/chrono/time) that the c time() function requires 1 argument.
That argument can either be a pointer in which case the value is stored there;
or the argument can be NULL in which case the function returned the value.

I played around with it and by adding this:

long myTime; time(&myTime); long timeNull = time(NULL); printf("myTime = %ld\ntimenull = %ld\n", mytime, timeNull);

to the end of the challenge program, confirmed both approaches give the same value.

I was trying to to remove 100 from the year then use a string concatenation to connect 20 if needed using: [NSString stringWithFormat:@"%d%d",value1, value2]

when all I had to do was add 1900. talk about using the wrong side of your brain. :laughing:

On another note how do you merge numbers together in an INT? (in other languages you just use the & operand to unite numbers in an integer). Is there a way to do that in C or would you have to result in string concatenation?

Thanks,
JB

[quote=“CodeMaxter”]This is my solution for the challenge (we need add 1900 to the year value because the year returned in the time struct is the years after 1900, 114 in my case):

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

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

    int futureSeconds = 4000000;
    long futureTime = time(NULL) + futureSeconds;
    struct tm futureStruct;

    localtime_r(&futureTime, &futureStruct);
    printf("The date after %d seconds is %d-%d-%d\n", futureSeconds, futureStruct.tm_mon + 1, futureStruct.tm_mday, futureStruct.tm_year + 1900);
    return 0;
}

Output
The date after 4000000 seconds is 4-10-2014
Program ended with exit code: 0[/quote]

I originally went down the track of trying to use the now struct as outlined in the book. Then I realised that it would be easier to create a new futureTime struct and pass it the variable for the number of seconds until 4,000,000 seconds into the future.

Here is my code:

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

int main(int argc, const char * argv[])
{
long secondsToFuture = time(NULL) + 4000000;

struct tm futureTime;
localtime_r(&secondsToFuture, &futureTime);
printf("The date in 4,000,000 seconds will be %d-%d-%d\n", futureTime.tm_mon + 1, futureTime.tm_mday, futureTime.tm_year + 1900);

return 0;

}[/code]

Here’s my solution. I had some trouble but I figured it out after about 10 minutes. Understanding these functions can be a bit confusing at times but after a bit of concentration and logic, we’ve got it!

// The purpose of this chalenge is to figure out what the date will be in 4 million seconds

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

int main(int argc, const char * argv[])
{
    
    // The function time() returns the number of seconds since the first moment of 1970 Greenwich, England
    long secondsSince1970 = time(NULL);
    
    
    printf("It has been %ld seconds since 1970!\n", secondsSince1970);
    
    // Struct tm is defined in the standard C library time.h
    struct tm now;
    
    // localtime_() can read the duration of time, i.e. the value stored in a variable such as secondsSince1970, and pack a 'struct tm' with the appropriate values.
    // It actually takes the address of the number of seconds since 1970 and the address of struct tm.
    localtime_r(&secondsSince1970, &now);
    
    
    printf("The time is %d:%d:%d and the date is %d-%d-%d.\n", now.tm_hour, now.tm_min, now.tm_sec, 1 + now.tm_mon, now.tm_mday, now.tm_year - 100);
    
    // Disregard the next three comments
    // There are 86,400 seconds in a day
    // There are 2, 627,999.424 seconds in a month
    // There are 31,535,993.008 seconds in a year
    
    // Declare and initialize a variable with the value of 4 million
    long fourMillionSeconds = 4000000;
    
    // Create another struct of the type 'tm'that will hold the value of our future date
    struct tm theFuture;
    
    // Declare and initialize a variable that will hold the value of current time plus 4 million seconds
    long advanceInTime = secondsSince1970 + fourMillionSeconds;
    
    // Pass our calculated time from 'advanceInTime' to the address of 'theFuture'
    localtime_r(&advanceInTime, &theFuture);
    
    // Notice we had to add a 1 to 'theFuture.tm_mon' because January begins at 0...we also had to subtract 100 from 'theFuture.tm_year' because otherwise it would read '114' instead of '14'
    printf("However in another 4 million seconds, the date will be %d-%d-%d!\n", 1 + theFuture.tm_mon, theFuture.tm_mday, theFuture.tm_year - 100);
    
    return 0;
}

Just realized that I was declaring a variable fourMillionSeconds wasn’t necessary since I could have simply wrote :

    long advanceInTime = secondsSince1970 + 4000000;

Learning process!

This is how I would write it:

const long secondsSince1970 = time (NULL);
const long fourMillionSeconds = 4000000;
const long advanceInTime = secondsSince1970 + fourMillionSeconds;

Key Point: When declaring and initializing a variable, if you want its value to be always constant, declare the variable as read-only (with the const qualifier.)

Some examples to illustrate why.

Unintentional coding error sneaking in:

void Start (enum Option option)
{
   if (option == OPTION_A) { 
        ...
   }
   else if (option = OPTION_B) { // Oops error! (= should be ==)
        ...
   }
   else {
        ...
   }
}

With a read-only parameter, the compiler will not let it sneak in:

void Start (const enum Option option)
{
   if (option == OPTION_A) { 
        ...
   }
   else if (option = OPTION_B) { // Compiler will catch this error
        ...
   }
   else {
        ...
   }
}

This is an example of defensive coding.

Another example of defensive coding:

void Start (enum Option option)
{
   if (OPTION_A == option) { 
        ...
   }
   else if (OPTION_B = option) { // Compiler will catch this error
        ...
   }
   else {
        ...
   }
}

[Accelerate your learning and become a competent programmer faster than you can imagine: pretty-function.org]

Here’s my solution.

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

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

long secondsSince1970 = time(NULL);

// Get 4 million seconds ahead of now
long futuresec = secondsSince1970 + 4000000;

struct tm future;
localtime_r(&futuresec, &future);
int month = future.tm_mon + 1;
int year = future.tm_year + 1900;

printf("The date is %d-%d-%d\n", future.tm_mday, month, year);

return 0;

}[/code]

Hello everyone.

I’m surprised I did this!! I went on the net and found websites that had programs that dealt with structs tm and read them till I was starting to confuse myself. So I stopped and went back to the problem at hand and used the programs that I copied from the net as a reference.

I was noodling around really when it fell into my lap all wrapped up and ready to deliver. You will see from my comments that I didn’t really know what I was doing exactly. If you can help me here with my vague comments I would be grateful.

So away we go…[code]// 4millionSeconds

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

int main(int argc, const char * argv[])
{
time_t rawtime; /* I think raw time lists the seconds from Jan.1 1970 to now */
struct tm info; / I think I made a pointer to place time (now + 4 million seconds) into /
time(&rawtime); /
I think I made raw time usable */

info = localtime(&rawtime);
printf("%s - time now.\n\n", asctime(info)); /* I printed out the time now for comparison */

long nowPlus4million = (time(NULL) + 4000000); /* I declared nowPlus4 million as a long integer */
info = localtime(&nowPlus4million); /* I placed local time (now) plus 4 million seconds into info */


/* I printed out the time 4 million seconds from now int asctime format */
printf("%s - time 4 million seconds from now.\n", asctime(info));

return 0;

}
[/code]

The output was…[quote]Sat Oct 25 14:41:49 2014

  • time now.

Wed Dec 10 21:48:29 2014

  • time 4 million seconds from now.[/quote]
    Well, what do you think?

[color=#004040]JR[/color]

High there Redeyesi.

I read your question about the 144 problem:

[quote=“Redeyesi”]the only problem is the year output, where I would expect ‘2014’, the console shows ‘114’, can someone explain why this is?
[/quote]
and Saintpeezy is right:[quote=“Saintpeezy”]The year value is returned as the number of years since the year 1900. In this case, there have been 114 years since the year 1900.[/quote]
I copied your code and I did this to “now.tm_year” in your third and last printf statement. printf("The future date will be Day: %d // Month: %d // Year: %d\n", now.tm_mday, trueMonth, now.tm_year-100);

When I did this, I thought; "No way! Too EASY! Won’t work. - It WORKED!!

Your solution to the challenge was simple and direct other than that small glitch.

[color=#004040]JR[/color]

Can anyone explain why you have to add 1 to:

CAN SOMEONE PLEASE TELL ME IF WHAT I DID IS CORRECT AS FAST AS POSSIBLE?

THIS IS MY CODE :-

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

int main(int argc, const char * argv[])
{
long fourMillionSeconds = time(NULL);
printf(“It has been %ld seconds since 1970\n”, fourMillionSeconds);

long fourmillion = time(NULL) + 4000000;
printf("It will be %ld seconds in four million seconds from now on\n", fourmillion);

struct tm now;
localtime_r(&fourMillionSeconds, &now);
printf("The time is %d:%d\n", now.tm_hour, now.tm_min);

}

IF IT’S WRONG WHY IS IT WRONG?

THANKS IN ANDVANCE :smiley:

I have absolutely no idea how to approach this problem. Is this normal at this point of the book?

i feel like I should just give up trying to understand programming. I thought it was interesting, and I have a technical mind, but I kind of doubt that now.

@iphonelover Well, I just found this forum and am in the process of doing the challenges now myself. It looks like your code isn’t the answer to the challenge that was posed in the book. The challenge was to get the date in month, day, year format. Your code, however, will return the number of seconds since 1970, that number plus 4 million more, and then the time when you run the code.

Here’s my solution to the problem:

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

int main(int argc, const char * argv[]) {
    
    long secondsSince1970 = time(NULL);
    printf("It has been %ld seconds since 1970.\n", secondsSince1970);
    
    secondsSince1970 += 4000000;
    
    struct tm now;
    
    localtime_r(&secondsSince1970, &now);
    
    printf("The future date is: %d-%d-%d\n", now.tm_mon+1, now.tm_mday, 1900 + now.tm_year);
    
    return 0;
}

@sineadw according to page 78, the structure is defined such that tm_mon returns a value between 0 and 11 to represent the month (ie 0 is January). We typically call January the 1st month, so we need to add a 1 to make the months equal the numbers we’re familiar with.

Hello everybody!! I took a calculator and calculated how much it is 4 million seconds. And the result was 46 days.
46 days since January 1970!

That statement says: increase the value of the variable secondsSince1970 by 4,000,000, meaning 4,000,000 seconds (~46 days) from now.

SYNOPSIS
     #include <time.h>

     time_t
     time(time_t *tloc);

DESCRIPTION
     The time() function returns the value of time in seconds since 0 hours, 0 min-
     utes, 0 seconds, January 1, 1970, Coordinated Universal Time, without including
     leap seconds