Unexpected results using os.time() and os.difftime()

I just came across this thread when I realized that a calculation I was trying to make might be having precision problems. I’m trying to evaluate a time series, one that can cover a pretty broad range. It involves julian days over the course of more than a century, with intermediate results in the sub-second range. 32 bit floats just can’t do it. If being able to switch to 64 floats (as a switch, I suppose) isn’t feasible, has anyone written a higher precision arithmetic package?

Thanks, @Ignatz. That’s just what I needed.

Probably missing something simple, but…

If I do…

``````d = difftime(os.time(), os.time())
Print(os.date("%M:%S", d)
``````

I get “00:00” which is just what I would expect. However, if I do

``````d = difftime(os.time(), os.time())
Print(os.date("%H:%M:%S", d)
``````

I get “18:00:00” which is definitely not what I expected. If I use %I instead of %H to do hours on a 12 hour clock, I get 06:00:00. So where is the 18 hour difference in hours coming from?

Sorry for reviving an old thread, but it seemed like the appropriate place.

When you use the d in os.date() in you’re example above, your asking os.date to convert the value of d to some date or time. d is the number of seconds from some arbitrary point in time long ago. Use the print statement below to see that point in time.

``````    print(os.date("%c", 0))
``````

I don’t see how that could work so that minutes and seconds are always 0, as I would expect,but hours is 18.

@Mark - I get 8 hours rather than 16, and my time is GMT+8 hours, so maybe it is treating the time parameter as being GMT, and reformatting it in local time.

If you prefix %H with ! , ie “!%H…” (which the docs tell me formats in Universal Coordinated Time, aka GMT) then you get zero as expected.

@Mark You set d = difftime(os.time(),os.time()), which set d to 0. Passing 0 to os.date() means that os.date() will give you the result of Wed Dec 31 18:00:00 1969 . As you can see, the hours and minutes are 0. Apparently you don’t understand what os.date is doing when you pass it a value. The value you give to d is the number of seconds since Wed Dec 31 18:00:00 1969. So depending on that value, os.date() will give you the date and time that many seconds since then.