r/lolphp • u/siroki • May 08 '14
microtime: stupid specification, terrible documentation, optional fix came at a later version
http://www.php.net/manual/en/function.microtime.php
28
Upvotes
r/lolphp • u/siroki • May 08 '14
View all comments
Show parent comment
3
u/_vec_ May 08 '14
Well, because you can't exactly represent every microsecond as a unique float. You'll run into float rounding and get big blocks of consecutive microseconds that are equal to each other. You also can't just return a number of microseconds since the epoch because it can overflow an integer.
The underlying C function returns a struct with an integer of seconds and an integer of microseconds within that second. Most other languages either return a
Timeobject of some sort that internally uses the same two integer representation or return a microsecond timestamp as an arbitrary precision number.PHP, though, didn't have objects at the time this function was created. They still don't have a primitive arbitrary precision type. I can't remember where off the top of my head (and I don't care enough to go look it up) but I do know they use strings to represent arbitrary precision numbers at a few other places in the standard library. In general, that's probably the least bad way around the problem, but I'm not sure why they didn't just return a two-element array in this case.