0

I am trying to find the difference in seconds from time now and a future time.

#include <time.h>
#include <stdio.h>
#include <float.h>
void main() {

    time_t future = 0xFFFFFFFFFFF;
    time_t now_time = time(NULL);
    printf("The future time is %s\n", ctime(&future));
    long double  diff_in_sec = difftime(time(&future), time(&now_time));
    printf("The diff in sec from now to future is %ld\n", diff_in_sec);
}

Now as i see , difftime returns double even though i try to use long double it is not possible for me to return the proper time diff in seconds. How can i achieve this?

offcourse long double doesn't make any sense there. But i only want to know is there another way i can achieve such a big diff.

Note: I am using 64bit system

bluefoggy
  • 961
  • 1
  • 9
  • 23
  • This may help: http://stackoverflow.com/questions/471248/what-is-ultimately-a-time-t-typedef-to – dragosht Sep 08 '14 at 08:48
  • How does one _know_ `0xFFFFFFFFFFF` represents a future or valid time? Many systems will truncate this to `0xFFFFFFFF` and will interpret this as some time near year 1969. If code needs a future time, fill out a `struct tm` with a future date and then convert to `time_t`. – chux - Reinstate Monica Sep 08 '14 at 14:58

2 Answers2

1

time_t is not big enough to hold 0xFFFFFFFFFFF.

Try this:

printf("%0lli\n%0lli\n", future, 0xFFFFFFFFFFF);

It will return this:

-1
4294971391
Petr
  • 1,159
  • 10
  • 20
  • How come i can get the value with ctime by using 0xFFFFFFFFFFF. – bluefoggy Sep 08 '14 at 08:36
  • I get 1410165485, 17592186044415 – bluefoggy Sep 08 '14 at 08:38
  • Yes, It is because the numbe overflows, so the result is udnefined (you can get whatsoever). You cant use such a big number with ctime() – Petr Sep 08 '14 at 08:45
  • What is the limit to time_t ? – bluefoggy Sep 08 '14 at 08:46
  • I dont think standard says something about the limit, so It will be system dependent, but practically It is 2147483647u (about 19 Jan. 2038) – Petr Sep 08 '14 at 08:58
  • So does that mean i cannot practically achieve the desired result ever? – bluefoggy Sep 08 '14 at 09:00
  • No, you cant. At least no with the standard C functions. There might be non-standard library for this (but I do not know it). – Petr Sep 08 '14 at 09:13
  • @Petr: `sizeof(time_t)` can be `8` and `0xFFFFFFFFFFF < ((1 << 63) - 1)` e.g., it [works on my system](http://stackoverflow.com/a/25721546/4279). – jfs Sep 08 '14 at 10:19
  • Yes, It can. It means that it will work only on some systems. It should by wise at least use assert(sizeof(time_t) >= 8). – Petr Sep 08 '14 at 12:06
1

The issue is with time(&future) call that modifies future. difftime() accepts the original future value on my machine:

/** $ make CC="gcc -std=c99" kingsdeb && ./kingsdeb */
#include <time.h>
#include <stdio.h>
#include <stdlib.h>

int
main(void) {
  struct tm t = {
    .tm_year=559444 - 1900, .tm_mon=2, .tm_mday=8,
    .tm_hour=13, .tm_min=40, .tm_sec=15, .tm_isdst=-1
  };
  time_t future = mktime(&t);
  if (future == (time_t) -1) {
    fprintf(stderr, "error: mktime returns -1 for %s", asctime(&t));
    exit(EXIT_FAILURE);
  }
  time_t now_time = time(NULL);
  if (now_time == (time_t) -1) {
    perror("time");
    exit(EXIT_FAILURE);
  }
  time_t now_from_future = future;
  if (time(&now_from_future) == (time_t) -1) {
    perror("time(&now_from_future)");
    exit(EXIT_FAILURE);
  }
  double  diff_in_sec = difftime(future, now_time);
  if (diff_in_sec < 1 && future != now_time) {
    fprintf(stderr, "difftime() returned value %f is too small\nfor "
                    "the time difference between (\n%s",
            diff_in_sec, ctime(&future));
    fprintf(stderr, "and\n%s)\n", ctime(&now_time));
    exit(EXIT_FAILURE);
  }
  printf("The current time is %s", ctime(&now_time));
  printf("time(&future)       %s", ctime(&now_from_future));
  printf("The future time  is %s", ctime(&future));
  printf("The diff in sec from now to future is %f\n", diff_in_sec);
  return 0;
}

Output

The current time is Mon Sep  8 13:52:00 2014
time(&future)       Mon Sep  8 13:52:00 2014
The future time  is Fri Mar  8 13:40:15 559444
The diff in sec from now to future is 17590775874495.000000

The output shows that time(&ts) stores the current time into ts. Don't pass future into it.

jfs
  • 399,953
  • 195
  • 994
  • 1,670