# Struct Challenge Solution

Hi people!

Here is the solution that I create. If someone think that it can be improved in any way or there may be another solution please let me know.

Thanks

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

int main(int argc, const char * argv[]) {
printf ("===== Structs Challenge =====\n\n");

``````struct tm now;

/* Today's Date */

/* I add +1 to now.tm_now (months from January up to now [0-11]) in order to adjust to the normal format */
/* I add +1900 to now.tm_year (years from 1900 up to now) in order to adjust to the normal format */

long secondsSince1970 = time (NULL);
localtime_r (&secondsSince1970, &now);
printf ("Today's date --> %d-%d-%d\n\n", now.tm_mon + 1, now.tm_mday, now.tm_year + 1900);

/* Date in 4 million seconds */

/* I add +4 million seconds to the amount of seconds since 1970 up to now in order to know how many seconds have passed since 1970 to a   4 million seconds future since present time, so that I can calculate date in 4 million seconds using localtime_r */

secondsSince1970 += 4000000;
localtime_r (&secondsSince1970, &now);
printf ("Date in 4 million seconds --> %d-%d-%d\n", now.tm_mon + 1, now.tm_mday, now.tm_year + 1900);
``````

}

+++++++++++++++ OUTPUT ++++++++++++++++

===== Structs Challenge =====

Todayâ€™s date --> 7-22-2015

Date in 4 million seconds --> 9-7-2015

I came up with the following for the challenge.

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

``````struct tm now;
localtime_r(&secondsSince1970, &now);

printf("The date in 4 million seconds from now is %d/%d/%d.\n", now.tm_mon + 1, now.tm_mday, now.tm_year + 1900);

return 0;
``````

}[/code]

Sweet, simple code.

Hadnâ€™t thought of adding to the individual cells of the struct. I recognize you didnâ€™t do that here, but can it be done?

Another question, maybe more relevant: why do â€śweâ€ť have to figure out how many seconds since 1970 to figure out what day it is NOW?

[quote]Another question, maybe more relevant: why do â€śweâ€ť have to figure out how many seconds since 1970 to figure out what day it is NOW?
[/quote]
The manual page for the localtime function states the following requirement:

``````The functions ... and localtime() all take as an argument a time value
representing the time in seconds since the Epoch (00:00:00 UTC, January 1, 1970;``````

Therefore, this requirement must be satisfied when calling the function.