Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can time(NULL) ever return failure?

Tags:

c

posix

time

Can the time_t time(time_t *t) function ever return failure if the argument passed is always NULL?

If the call is time(NULL), do we still need to check for the return value?

The only documented error code is EFAULT, which relates to the pointer being invalid.

like image 299
Jay Avatar asked Dec 16 '15 03:12

Jay


People also ask

What does time null return?

time(NULL) returns the number of seconds elapsed since 00:00:00 hours, GMT (Greenwich Mean Time), January 1, 1970.

What to return if function fails?

If you make the function to do some operation , return 0 for success , -1 for failed, and set errno to appropriate value so that the caller could check it to know the detail of failure. Sometimes people also use different return values to mean different failures.

How do I fix null time conversion error?

null will cause the time conversion operation to fail, but still creates a time object. In the action immediately following the Convert time zone action, set the Configure run after to run after success and failure. Do this for every action that comes after a Convert time zone action. Doing this will fix all the issues you have been having.

Do you fail to reject the null hypothesis?

Speaking purely as an editor, I acknowledge that "failing to reject the null hypothesis" is cringe-worthy. "Failing to reject" seems like an overly complicated equivalent to accept. At minimum, it's clunky phrasing. But it turns out those rough-and-ready statisticians in the Nulls Angels have good reason to talk like that.

How do I run the convert time zone action after failure?

In the action immediately following the Convert time zone action, set the Configure run after to run after success and failure. Do this for every action that comes after a Convert time zone action.

Why does C++ make new throw a null pointer by default?

The C++ committee could have let new return a null pointer on an allocation failure, but every constructor that used new for internal memory would have to detect the failure and turn it into an exception anyway. So having new throw by default simplifies everyone's code.


3 Answers

Yes. time has a documented may fail case:

The time() function may fail if:

[EOVERFLOW] The number of seconds since the Epoch will not fit in an object of type time_t.

Source: http://pubs.opengroup.org/onlinepubs/9699919799/functions/time.html

Expect this to happen in practice in about 22 years, no sooner, and not on 64-bit systems or 32-bit ones that utilize a 64-bit time_t.

Also, the presence of any shall fail or may fail cases also allows for implementation-defined errors, though their existence would be a serious quality-of-implementation flaw.

EFAULT is a non-issue/non-existent because it only happens when your program has undefined behavior.

So despite all this, in the real world, time is not actually going to fail.

like image 132
R.. GitHub STOP HELPING ICE Avatar answered Sep 30 '22 05:09

R.. GitHub STOP HELPING ICE


Can time(NULL) ever return failure?

No. C standard says that

C11: 7.27.2.4:

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.

like image 40
haccks Avatar answered Sep 30 '22 03:09

haccks


I've checked on RHEL, SLES and UBTU; the man 2 page yields the same (relevant) thing:

  time() returns the time since the Epoch (00:00:00 UTC, January 1, 1970), measured in seconds.
   If t is non-NULL, the return value is also stored in the memory pointed to by t.

Anyway, going back to the original questions

  • Q0: Can the time_t time(time_t *t) function ever return failure if the argument passed is always NULL?

    • A/R0: Yes, if some very special events occurred (memory full, and so on, ...)
  • Q1: If the call is time(NULL), do we still need to check for the return value?

    • A/R1: The actual answer is "NO", you don't have to; the fact that the func could return something relevant, is a different story. After all, why calling a func ,if there's no need of doing so?
  • Q2: The only documented error code is EFAULT, which relates to the pointer being invalid.

    • You don't have anything to do with the invalid codes; as you said you're passing NULL, so there's no problem.
like image 36
CristiFati Avatar answered Sep 30 '22 05:09

CristiFati