From 2cb8ebbb6a3cda777288b9dd030d0177f2c216e5 Mon Sep 17 00:00:00 2001 From: Andrew Collard Date: Wed, 30 Oct 2019 13:06:20 -0400 Subject: [PATCH 1/2] Added support for microseconds in ISO time. --- ApplicationInsights/Channel/Contracts/Utils.php | 10 ++++------ Tests/Channel/Contracts/Utils_Test.php | 7 +++++++ 2 files changed, 11 insertions(+), 6 deletions(-) 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..99e4feb 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 returnISOStringForTime() + { + $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"); + } } From 911e3c9b1393fc5aa78c34b49db65522dbc75aba Mon Sep 17 00:00:00 2001 From: Andrew Collard Date: Wed, 30 Oct 2019 13:08:16 -0400 Subject: [PATCH 2/2] Fixed tests name. --- Tests/Channel/Contracts/Utils_Test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/Channel/Contracts/Utils_Test.php b/Tests/Channel/Contracts/Utils_Test.php index 99e4feb..8ef8c5b 100644 --- a/Tests/Channel/Contracts/Utils_Test.php +++ b/Tests/Channel/Contracts/Utils_Test.php @@ -30,7 +30,7 @@ public function testConvertMillisecondsToTimeSpan() $this->assertEquals(Utils::convertMillisecondsToTimeSpan(-1), "00:00:00.000", "invalid input"); } - public function returnISOStringForTime() + 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");