diff --git a/ApplicationInsights/Channel/Contracts/Utils.php b/ApplicationInsights/Channel/Contracts/Utils.php index 465d027..85b4c0d 100644 --- a/ApplicationInsights/Channel/Contracts/Utils.php +++ b/ApplicationInsights/Channel/Contracts/Utils.php @@ -84,15 +84,13 @@ public static function convertMillisecondsToTimeSpan($milliseconds) * @return string */ public static function returnISOStringForTime($time = null) - { + { if ($time == NULL) { - return gmdate('c') . 'Z'; - } - else - { - return gmdate('c', $time) . 'Z'; + $time = microtime(true); } + $microseconds = substr(sprintf('%06d', round($time-floor($time), 6)*1000000), 0, 6); + gmdate("Y-m-d\TH:i:s.", $time).$microseconds.'+00:00Z'; //iso 8601 GMT with microseconds } /** diff --git a/Tests/Channel/Contracts/Utils_Test.php b/Tests/Channel/Contracts/Utils_Test.php index e05a678..8ef8c5b 100644 --- a/Tests/Channel/Contracts/Utils_Test.php +++ b/Tests/Channel/Contracts/Utils_Test.php @@ -30,4 +30,11 @@ public function testConvertMillisecondsToTimeSpan() $this->assertEquals(Utils::convertMillisecondsToTimeSpan(-1), "00:00:00.000", "invalid input"); } + public function testReturnISOStringForTime() + { + $date = (new DateTime("2004-02-12T15:19:21+00:00"))->getTimestamp(); + $this->assertEquals(Utils::returnISOStringForTime($date), "2004-02-12T15:19:21.000000+00:00Z"); + $this->assertEquals(Utils::returnISOStringForTime($date+0.1), "2004-02-12T15:19:21.100000+00:00Z", "milliseconds digit 1"); + $this->assertEquals(Utils::returnISOStringForTime($date+0.999999), "2004-02-12T15:19:21.999999+00:00Z", "rounding error"); + } }