My Solution - SPOILER!


#1

Will you be ok with my solution ?

[code]//
// main.c
// time
//
// Created by C3PO on 21/02/13.
// Copyright © 2013 EdisonTV. All rights reserved.
//

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

int main(int argc, const char * argv[])
{
// Seconds since 1970
long secondsSince1970 = time(NULL);
printf("%ld seconds since 1970\n\n",secondsSince1970);

// Now
struct tm now;
localtime_r(&secondsSince1970, &now);
printf("Today is %.2d-%.2d-%.4d\n", now.tm_mon + 1, now.tm_mday, now.tm_year + 1900);


// Later
long secondsToAdd = 4 * pow(10,9);
long secondsLater = secondsSince1970 + secondsToAdd;
struct tm later;
localtime_r(&secondsLater, &later);
printf("In %ld seconds -> %.2d-%.2d-%.4d\n", secondsToAdd, later.tm_mday, later.tm_mon + 1, later.tm_year + 1900);

return 0;

}

[/code]

I hope ?

s
e
b

From France

(Thanks Arraon for your books)


#2

Looks good. In case you didn’t do it on purpose you’re adding 4 billion seconds rather than 4 million.


#3

:astonished: Oups

thank you

:slight_smile:


#4

Also if you want you could skip using the math library (for pow) and use this shortcut:

long secondsToAdd = 4e6;

#5

The best elegant solution (for today)

[code]//
// main.c
// time
//
// Created by C3PO on 15/03/13.
// Copyright © 2013 Sebastien REMY. All rights reserved.
//

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

int main(int argc, const char * argv[])
{
// Seconds since 1970
long secondsSince1970 = time(NULL);
printf("%ld seconds since 1970\n\n",secondsSince1970);

// Now
struct tm now;
localtime_r(&secondsSince1970, &now);
printf("Today -> %.2d-%.2d-%.4d ", now.tm_mon + 1, now.tm_mday, now.tm_year + 1900);


// Later
long secondsToAdd = 4e6;
long secondsLater = secondsSince1970 + secondsToAdd;
struct tm later;
localtime_r(&secondsLater, &later);
printf("in %ld seconds -> %.2d-%.2d-%.4d\n", secondsToAdd, later.tm_mday, later.tm_mon + 1, later.tm_year + 1900);

return 0;

}
[/code]

Console copy

[code]1363344785 seconds since 1970

Today -> 03-15-2013 in 4000000 seconds -> 30-04-2013[/code]

Thanks for your help


#6

[quote=“SebRemy”]The best elegant solution (for today)

[code]//
// main.c
// time
//
// Created by C3PO on 15/03/13.
// Copyright © 2013 Sebastien REMY. All rights reserved.
//

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

int main(int argc, const char * argv[])
{
// Seconds since 1970
long secondsSince1970 = time(NULL);
printf("%ld seconds since 1970\n\n",secondsSince1970);

// Now
struct tm now;
localtime_r(&secondsSince1970, &now);
printf("Today -> %.2d-%.2d-%.4d ", now.tm_mon + 1, now.tm_mday, now.tm_year + 1900);


// Later
long secondsToAdd = 4e6;
long secondsLater = secondsSince1970 + secondsToAdd;
struct tm later;
localtime_r(&secondsLater, &later);
printf("in %ld seconds -> %.2d-%.2d-%.4d\n", secondsToAdd, later.tm_mday, later.tm_mon + 1, later.tm_year + 1900);

return 0;

}
[/code]

Console copy

[code]1363344785 seconds since 1970

Today -> 03-15-2013 in 4000000 seconds -> 30-04-2013[/code]

Thanks for your help[/quote]

You could stream-line this code even more… with the following changes:

[code]// Later

long secondsLater = secondsSince1970 + 4e6; //I saw no reason in having two longs - when the same thing could be done in one? 
struct tm later;
localtime_r(&secondsLater, &later);
printf("in %ld seconds -> %.2d-%.2d-%.4d\n", secondsLater, later.tm_mday, later.tm_mon + 1, later.tm_year + 1900);
[/code]

#7

I find that I use the shortcut operators (+=, -=, *=, /=) a lot in other languages.

localtime_r(&secondSince1970, &now);
printf("the time is %d:%d:%d\n", now.tm_hour, now.tm_min, now.tm_sec);
    
secondSince1970 += 4e6;
    
localtime_r(&secondSince1970, &now);
    
printf("in 4 million seconds, the date will be %d/%d/%d", now.tm_mon+1, now.tm_mday, now.tm_year+1900);
return 0;