Struct Time Challenge Question?


#1

I don’t know if I did it quite right, but I try to not look at the answers here in the forums.

I’m curious if this is how someone would have done it. Thanks

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

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

// insert code here...
long secondsSince1970 = time(NULL) + 4000000;
printf("It has been %ld seconds since 1970 + 4,000,000 more seconds.\n",secondsSince1970);

struct tm now;
localtime_r(&secondsSince1970, &now);
printf("The date is %d-%d-%d is 4,000,000 seconds in the future\n", now.tm_mon, now.tm_mday, now.tm_year + 1900);

return 0;

}[/code]

Thanks for any help.

Stan


#2

This should work. I get in the habit of using variables instead of adding 4 million directly so I would have set that to a variable.

Also, on your month, you need to add 1 to it as you added 1900 to year. Months start counting at 0 for Jan.


#3

Hi all! I’m a newbie. Here is my version of the solution of this task.

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

{
long fmsec = 4000000; //four million seconds
long secondsince1970 = time(NULL) ; //seconds since 1970
printf(“It has been %ldl seconds from 1970\n\n”, secondsince1970);

struct tm now;
localtime_r(&secondsince1970, &now);
printf("Current date is %d %d %d\n\nCurrent time is %d:%d:%d\n\n", now.tm_year+1900, now.tm_mon+1, now.tm_mday, now.tm_hour, now.tm_min,now.tm_sec); // YYYY:MM:DD
struct tm fms;
localtime_r(&fmsec, &fms);
printf("The 4 million seconds equals %d month(s) %d days %d years\n", fms.tm_mon, fms.tm_mday, fms.tm_year-70);

return 0;

}[/code]


#4

I think you need to add secondsince1970 to fmsec. Otherwise you don’t end up in the future.

[quote=“Freemansion”]Hi all! I’m a newbie. Here is my version of the solution of this task.

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

{
long fmsec = 4000000; //four million seconds
long secondsince1970 = time(NULL) ; //seconds since 1970
printf(“It has been %ldl seconds from 1970\n\n”, secondsince1970);

struct tm now;
localtime_r(&secondsince1970, &now);
printf("Current date is %d %d %d\n\nCurrent time is %d:%d:%d\n\n", now.tm_year+1900, now.tm_mon+1, now.tm_mday, now.tm_hour, now.tm_min,now.tm_sec); // YYYY:MM:DD
struct tm fms;
localtime_r(&fmsec, &fms);
printf("The 4 million seconds equals %d month(s) %d days %d years\n", fms.tm_mon, fms.tm_mday, fms.tm_year-70);

return 0;

}[/code][/quote]


#5

Hi All,
here’s my quick and dirty solution to the challenge…Any way to improve it?

//
//  main.c
//  TimeTestChallenge
//
//  Created by Anthony Armstrong on 10/27/12.
//  Copyright (c) 2012 Anthony Armstrong. All rights reserved.
//

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


int main(int argc, const char * argv[])
{
    long timesince1970 = time(NULL);
    long timeThen = timesince1970 + 4000000;
    struct tm now;
    struct tm then;
    
    localtime_r(&timesince1970, &now);
    localtime_r(&timeThen, &then);
    
    printf("seconds since 1970 til now: %ld\n", timesince1970);
    printf("The time now is %d:%d:%d\n", now.tm_hour, now.tm_min, now.tm_sec);
    printf("In 4,000,000 seconds, the date will be: %d-%d-%d", (then.tm_mon + 1), then.tm_mday, then.tm_year + 1900);
    
    return 0;
}

#6

//heres mine

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

int main(int argc, const char * argv[])
{
    long secondsSince1970 = time(NULL);
    long future = secondsSince1970 + 4000000;
    
    
    struct tm will;
    localtime_r(&future, &will);
    printf("In 4 million seconds the current date will be %d-%d-%d\n", will.tm_mon, will.tm_mday, will.tm_year +1900);
    
    
    return 0;
}

#7

my solution

[code]int main(int argc, const char * argv[])
{
long secondesDepuis1970 = time(NULL);
long tempsFuture = secondesDepuis1970 + 4000000;
//printf("%ld",secondesDepuis1970);

struct tm maintenant;
localtime_r(&secondesDepuis1970, &maintenant);
printf("Nous somme le %d %d %d\n\n",maintenant.tm_mday, maintenant.tm_mon +1, maintenant.tm_year + 1900);

struct tm future;
localtime_r(&tempsFuture, &future);
printf("dans 4000000 de secondes nous serons le %d %d %d \n\n", future.tm_mday, future.tm_mon +1, future.tm_year + 1900);

return 0;

}
[/code]