1

Hi I hava a little problem because the function time() keeps returning -1 is there any posible way to fix this?

I tried:

time_t start, end; start = time(NULL); //do something end = time(NULL); 

But when I debug both have the value -1. and I tried:

time_t start, end; time(&start); // do something time(&end); 

But in this case both values stay 0.

8
  • 1
    and how do you check the return value? Commented Nov 12, 2015 at 13:07
  • I use them in if (difftime(mod_end, mod_start) < TCK_WAIT_TIME_SEC) but I check the values in the debuger Commented Nov 12, 2015 at 13:09
  • 2
    What does it print if you do this: start = time(NULL); if (start==(time_t)-1) perror("time failed"); else printf("start=%ld\n",start); Commented Nov 12, 2015 at 13:16
  • 2
    have you tried a minimal working example of this? and just print out the content of your start-value? Commented Nov 12, 2015 at 13:16
  • 2
    What C compiler? The function works as expected in gcc 4.9 (with both ways of calling it). Commented Nov 12, 2015 at 13:18

1 Answer 1

1

It's perfectly valid, according to ISO C, for time() to return -1 if it cannot establish the time. From ISO C11 7.27.2.4 The time function /3 (though this behaviour has been around for a long time):

The time function returns the implementation’s best approximation to the current calendar time. The value (time_t)(-1) is returned if the calendar time is not available.

Given that you're working with Simplicity Studio, a package frequently used for embedded systems, it's possible your target environment may not be able to establish the calendar time (if, for example, it does not have a real-time clock to query).

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.