On Thu, 14 Sep 2000, Andy Dougherty wrote:

> Do you mean that the following program might not print '5' (well, about 5,
> given sleep's uncertaintites ...)
> 
>     #include <stdio.h>
>     #include <sys/types.h>
>     #include <time.h>      /* May need sys/time.h instead */
>     int main(void)
>     {
>        time_t start, stop;
>        start = time((time_t *) 0);
>        sleep(5);
>        stop = time((time_t *) 0);
>        return printf("The difference is %ld seconds.\n", stop - start);
>     }
[snip]
> I agree those are portability bugs in those modules and apps.  Just like
> assumptions that '/' is the directory separator, and that integers are 32
> bits long, and many of the other problems cited in perlport.pod.

You have another assumption up there: that time_t == signed long (since
you're printing it with %ld) with a resolution of seconds (since you say
"%ld seconds"). ISO/IEC 9899:1999 (draft C standard, the only C standard-y
thing I have around) says that time_t is an "arithmetic type" (OK, so
you're allowed to subtract one from another) "capable of representing
time", and that "the range and precision representable in clock_t and
time_t are implementation-defined". So subtracting them may not give you
seconds since any epoch (that's what the difftime() is for; it explicitly
gives you seconds of difference, as a double), and even if you knew it
did, there's no guarantee that %ld is the correct format to print it out.
Might as well be %llu or %d as %ld.

Maybe POSIX makes more guarantees.

Cheers,
Philip
-- 
Philip Newton <[EMAIL PROTECTED]>

Reply via email to