lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi Roberto,

> On Dec 17, 2014, at 12:49 PM, Roberto Ierusalimschy <roberto@inf.puc-rio.br> wrote:
> 
>>>> began = os.clock ()
>>>> local elapsed = os.difftime (os.clock (), began)
>> 
>> os.difftime is meant to be used with numbers returned by os.time, not os.clock.
>> In POSIX systems, you can simply subtract the numbers, in both cases.
>> 
>> Perhaps the manual should say so.
> 
> It already does:
> 
>  os.clock ()
> 
>  Returns an approximation of the amount in seconds of CPU time
>  used by the program.
> 
> Note the units ("in seconds"). You can always subtract seconds from
> seconds. (That is valid in any system, not only POSIX.)

Sure, but since the docs for os.difftime() imply that it is a more portable
version of POSIX `t2 - t1`, I have been using it on `os.clock ()` results too. 

While the manual entry for `os.time ()` indicates that it should be used only
as an argument to `os.date ()` or `os.difftime ()`, and the manual entry for
`os.date ()` clearly states that if  the `time` parameter is used, it should
be a value from `os.date ()`... the entry for `os.difftime ()` seems to imply
that I can pass any seconds-like value.  And indeed, on POSIX, that has been
working admirably for me.

Now that it's been pointed out, and I've given it some thought, it seems
perfectly sensible that `os.difftime ()` only takes `os.date ()` return
values, but it would likely save other folks from mistakenly abusing the
function in the way I have for the last few years if the manual was instead
worded along the lines, analogously to `os.date ()`:

  os.difftime (t2, t1)

  Returns the number of seconds from time t1 to time t2 (see the os.time
  function for a description of these values). In POSIX, Windows, and some
  other systems, this value is exactly t2-t1.

Cheers,
-- 
Gary V. Vaughan (gary AT vaughan DOT pe)