On Mon, Oct 05, 2020 at 03:16:24PM +0000, Roderick wrote:
> 
> The result of time() has type time_t and we know what kind of number
> goes there: seconds since 0 hours, 0 minutes, 0 seconds, January 1,
> 1970, Coordinated Universal Time.
> 
> In my FreeBSD running on a 64 bit processor this type is: int (__32_t).
> It considers this size enough for above information.
> 
> In my OpenBSD running on a 32 bit processor this type is: long long
> (__64_t).
> 
> None of both has an unsigned type, although time moves forward
> (more or less fast!!!).

I don't know if these are the real reasons, but I can think of two ways
having a signed type helps:

(a) Sometimes, you might want to represent times before 1970. Not in the
    return value of time(), of course, but maybe in other places your
    program is reasoning about times.

(a) Working with unsigned types can be error-prone. For example,
    consider:

    if (deadline - time() < TIME_NEEDED) {
      printf("There's not enough time left.\n");
    }

    You probably want the condition to be triggered even when
    time() > deadline, but that's not what the above code would do if
    they were unsigned. There are probably more compelling examples
    where it's even more obvious unsigned ints will not do what you
    probably wanted, but that's what came to me on the spot. In any
    case, my previous employer's (C++) style guide strongly discouraged
    use of unsigned integer types for this reason.

-- 
James

Reply via email to