Formatted challenge solution


#1

My solution isn’t really unique from the others in approach, but I did try one thing differently that pleased me. In previous chapters, we have changed the %f token placeholder to %.2f in order to have it use two decimal places. I decided to try %.2d to force two integer places and it worked. I discovered in one of my test runs that the seconds were blank and not displaying “00”, then a few seconds later showed “5” instead of “05”.

#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);
    long plus4MillionSeconds = secondsSince1970 + 4000000;
    printf("It will be %ld seconds in 4 million seconds.\n", plus4MillionSeconds);
    
    struct tm future;
    localtime_r(&plus4MillionSeconds, &future);
    printf("The time in four million seconds will be %.2d:%.2d:%.2d on %d-%d-%d.\n", future.tm_hour, future.tm_min, future.tm_sec, future.tm_mday, future.tm_mon + 1, future.tm_year + 1900);
    return 0;
}

Output window returns:
It has been 1400857761 seconds since 1970.
It will be 1404857761 seconds in 4 million seconds.
The time in four million seconds will be 18:16:01 on 8-7-2014.
Program ended with exit code: 0