Search code examples
c++ctimeposix

Why does `std::time` have an unnecessary parameter?


From cppref:

std::time_t time(std::time_t* arg);

Returns the current calendar time encoded as a std::time_t object, and also stores it in the object pointed to by arg, unless arg is a null pointer.

I never see anyone call std::time with a non-null pointer argument. I just wonder:

1. Why does std::time have an unnecessary parameter?

2. Is there any motivation/rationale behind the design?


Solution

  • Historically time_t was an abstract type and there was probably an expectation that it might need to be a structure or extension type that might not be able to be returned reliably, or where compilers might disagree in ABI for returning it, such that returning it by storing it to a caller-provided address "made sense". Note the existence of the difftime interface and C's vagueness about how time_t values are to be interpreted. Only POSIX (much later) required the unit to be seconds (since the epoch, and defined the epoch). I'm not sure if there's any concrete evidence for this as the motivation (maybe in the C89 Rationale document?) but this is the area I'd look in.

    For C++, it's simply that the std::time interface is the C time function, wrapped in std::.