2

ISO C / POSIX declare the time() function like this:

time_t time(time_t *tloc);

The documentation says:

The time() function shall return the value of time in seconds since the Epoch.

The tloc argument points to an area where the return value is also stored. If tloc is a null pointer, no value is stored.

I don't see why one would design the API from the start in such a way that there was both a return value and an "out" parameter. Is there a good rationale for designing it like that or did this evolve historically?

Was there at some point in the past a time() function that just had the out parameter (void time(time_t *)), but it was changed to a time_t return value, with the first parameter being kept for backwards compatibility?

Or alternatively, were there systems where the return value was restricted in size that didn't fit a time_t, so a pointer was needed?

1
  • 5
    When that function was coded... C had no type long but time was reported on 32bits. => The need for some memory address where the function could store an array of 2 (16bits) ints.
    – MC68020
    Commented Apr 26, 2021 at 6:34

1 Answer 1

2

So as MC68020 said in the comments, C didn't have a long type back then, the oldest occurrence I could find is in lib/time.s of Second Edition Unix, the file is dated 1972-06-07 on that website linked above, with this source (PDP-11 assembly):

/ C library -- time

/ tvec = time(tvec);
/
/ tvec[0], tvec[1] contain the time

    .globl  _time

.data
_time:
    1f
.text
1:
    mov 2(sp),r0
    sys time
    mov ac,(r0)
    mov mq,2(r0)
    rts pc

Interestingly, it seems like it always had the return value (tvec = time(tvec);), assuming tvec is some kind of 2-component array of 16-bit values, if that assignment has any relevance.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .