Hi!
I am posting my solution for this chapter’s challenge question hoping that anybody can give their comments on it because I want to improve better.
Therefore, thanks in advance!
My code is:
#include <stdio.h>
#include <time.h>
int main(int argc, const char * argv[]) {
printf("**************************\n");
printf("Challenge Qusetion\n");
printf("**************************\n");
//Calculate today's date
long secondsSince1970 = time(NULL);
struct tm todayDate;
localtime_r(&secondsSince1970, &todayDate);
printf("Today date is %d:%d:%d\n", todayDate.tm_mday, todayDate.tm_mon+1, todayDate.tm_year+1900);
//Calculate date 4 millions seconds from now
long fourMillionSeconds = 4000000;
long totalSeconds = fourMillionSeconds + secondsSince1970;
//Question wants to know 4million seconds from now.
//So we add "number of seconds from 1970 until now" and 4millionseconds together.
struct tm dateAfter4MillionSeconds;
localtime_r(&totalSeconds, &dateAfter4MillionSeconds);
printf("The date after 4 million seconds from now is %d:%d:%d",
dateAfter4MillionSeconds.tm_mday,
dateAfter4MillionSeconds.tm_mon+1,
dateAfter4MillionSeconds.tm_year+1900);
return 0;
}
The output is:
[code]
Challenge Qusetion
Today date is 6:6:2015
The date after 4 million seconds from now is 23:7:2015[/code]