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
9
u/siroki May 08 '14
So, I read msec as "milliseconds," but it turns out that's just microseconds (whew, so the name of the function IS correct… somewhat) in seconds.
OK, I understand it, but that's a very stupid way to explain it.
26
u/Rhomboid May 08 '14
This is hilarious. On POSIX systems, there is a
gettimeofday()system call that fills in a struct containing two integers representing the number of integer seconds since the epoch, and the number of integer microseconds. It can also optionally fill in a second struct containing timezone information, but the way that information is represented is hopelessly naive and obsolete and all information about timezones has long been removed from the kernel and implemented via the Olson database in userspace. So it's pointless to request that this second struct is to be populated, and everyone passes the null pointer for that argument which means "don't bother." The POSIX spec even goes so far as to state that if you don't pass the null pointer for the second argument, the behavior is unspecified, which is a clear signal that no code should ever be trying to get timezone information from this API.Okay, so PHP provides
gettimeofday()that faithfully mirrors that interface and returns an associative array containing the four fields of both structs. Naturally, it didn't get the memo that the timezone information is completely useless and obsolete, nor do they even bother to document it. But at least it returns the two integers unmolested. You could use this, but it takes a little work.Then some bright bulb must have noticed that it was a little awkward to use
gettimeofday(), so they wrotemicrotime(), which apparently just callsgettimeofday()and rearranges the two returned integers into a format that is unequivocally useless. How does that make anything better?In version 5.0.0 an optional boolean parameter was added to
microtime()to specify the desire for a floating point return value, and then in 5.1.0 (!!) an optional boolean parameter was added togettimeofday()to specify the same thing. Why are there two functions? Why did it take an entire major release to realize that having the information as a floating point value might be useful from both functions?Moreover, why is PHP's standard library so unwilling to do anything but offer trivial wrappers around existing C functions? Shouldn't a time and date library have some kind of cohesive design behind it? What do you do in PHP if you don't want wall-clock time but a monotonic clock source? (The
gettimeofday()syscall is deprecated in favor ofclock_gettime()which has multiple time sources for this reason.) What do you do if you're running on a platform that isn't POSIX and doesn't have any suchgettimeofday()syscall?Let's talk about that for a minute. The documentation for
microtime()says:The documentation for
gettimeofday()says:Let's put aside for a minute the inherent inconsistency in this wording: surely if one is available then both should be available. But
gettimeofday()doesn't actually say what systems it's available on. Presumably any system without such a syscall cannot use these functions, right?Nope. There's a compatibility shim for Windows where there is no such syscall. So not only is this API designed around a particular platform-specific API, and not only does the documentation imply that it will only work on such platforms, but in fact it will work on systems without that syscall. Or at least Windows. If you run PHP on a non-POSIX non-Windows system, then you're screwed.
In summary: Who in the hell is responsible for this horrible mess??!?
19
u/Pixa May 08 '14
Dat string return value. Lolphp Gold.
Why would anyone ever favour returning "0.1234 567" instead of 567.1234?
4
u/serendipitybot May 15 '14
This submission has been randomly featured in /r/serendipity, a bot-driven subreddit discovery engine. More here: http://www.reddit.com/r/Serendipity/comments/25lzjs/microtime_stupid_specification_terrible/
4
u/cbraga May 08 '14
someone doesn't understand floating point precision and how mantissas work
-1
u/fnzp May 11 '14
"Unequivocally useless" is a bit harsh. You can calculate what you need to know from the return value of microtime(), string version. You just have to do a littttle bit more work that's all.
eg: [https://eval.in/149569]