Tech Tip: Utility Method to get Current Time in Microseconds
PRODUCT: 4D | VERSION: 14.x | PLATFORM: Mac & Win
Published On: June 2, 2016
Below is a utility method to get the current time in seconds with a decimal representation of microseconds or milliseconds in Text Format. This is done by using the microtime() PHP command, which can return two formats for the time passed since midnight January 1, 1970 UTC. Passing a True will return the time in seconds with a precision of 4 floating point spaces, while passing a False will return a string with two numbers the first being the microseconds of a second and the second being the number of seconds.
Code:
// Util_MicroTime // // Desription: // Gets the Statistical Mean of an Array // // Parameters: // $1: Boolean to Select Format/Precision // Results: // $0: Seconds passed since midnight January 1, 1970 UTC in Text Format // ---------------------------------------------------- C_BOOLEAN($1;$format_b) C_TEXT($0;$time_t) If(Count Parameters=1) $format_b:=$1 $phpOK:=PHP Execute("";"microtime";$time_t;$format_b) If($phpOK) $0:=$time_t Else $0:="0" End if Else $0:="0" End if |
Sample:
C_TEXT($res1;$res2) $res1:=Util_MicroTime(True) $res2:=Util_MicroTime(False) |
Results:
$res1:="1464791880.3019"
When a True is passed the results are a little more precise than milliseconds by 1 more floating point value.
$res2:="0.30192300 1464791880"
When a False is passed the results are more precise, but due to the precision of REAL types the numbers are split into two parts with the first part of the string
being the microseconds and the second part being the seconds.
Becareful when converting to a Real using the Num command, the results are only precise up to thirteen digits.
http://php.net/manual/en/function.microtime.php