Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit e7fdaa6

Browse files
authored
Use DateTime::createFromFormat (#168)
1 parent ce28990 commit e7fdaa6

File tree

4 files changed

+63
-47
lines changed

4 files changed

+63
-47
lines changed

src/Trace/DateFormatTrait.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
* Copyright 2018 OpenCensus Authors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace OpenCensus\Trace;
19+
20+
/**
21+
* Trait which provides helper methods for converting DateTime input formats.
22+
*/
23+
trait DateFormatTrait
24+
{
25+
/**
26+
* Handles parsing a \DateTimeInterface object from a provided timestamp.
27+
*
28+
* @param \DateTimeInterface|int|float $when [optional] The end time of this span.
29+
* **Defaults to** now. If provided as an int or float, it is expected to be a Unix timestamp.
30+
* @return \DateTimeInterface
31+
* @throws \InvalidArgumentException
32+
*/
33+
private function formatDate($when = null)
34+
{
35+
if (!$when) {
36+
// now
37+
$when = $this->formatFloatTimeToDate(microtime(true));
38+
} elseif (is_numeric($when)) {
39+
$when = $this->formatFloatTimeToDate($when);
40+
} elseif (!$when instanceof \DateTimeInterface) {
41+
throw new \InvalidArgumentException('Invalid date format. Must be a \DateTimeInterface or numeric.');
42+
}
43+
$when->setTimezone(new \DateTimeZone('UTC'));
44+
return $when;
45+
}
46+
47+
/**
48+
* Converts a float timestamp into a \DateTimeInterface object.
49+
*
50+
* @param float $when The Unix timestamp to be converted.
51+
* @return \DateTimeInterface
52+
*/
53+
private function formatFloatTimeToDate($when)
54+
{
55+
return \DateTime::createFromFormat('U.u', number_format($when, 6, '.', ''));
56+
}
57+
}

src/Trace/Span.php

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
class Span
2828
{
2929
use AttributeTrait;
30+
use DateFormatTrait;
3031

3132
// See https://github.com/census-instrumentation/opencensus-specs/blob/master/trace/HTTP.md#attributes
3233
const ATTRIBUTE_HOST = 'http.host';
@@ -331,40 +332,6 @@ public function setStatus($code, $message)
331332
$this->status = new Status($code, $message);
332333
}
333334

334-
/**
335-
* Handles parsing a \DateTimeInterface object from a provided timestamp.
336-
*
337-
* @param \DateTimeInterface|int|float $when [optional] The end time of this span.
338-
* **Defaults to** now. If provided as an int or float, it is expected to be a Unix timestamp.
339-
* @return \DateTimeInterface
340-
*/
341-
private function formatDate($when = null)
342-
{
343-
if (!$when) {
344-
// now
345-
$when = $this->formatFloatTimeToDate(microtime(true));
346-
} elseif (is_numeric($when)) {
347-
$when = $this->formatFloatTimeToDate($when);
348-
} elseif (!$when instanceof \DateTimeInterface) {
349-
throw new \InvalidArgumentException('Invalid date format. Must be a \DateTimeInterface or numeric.');
350-
}
351-
$when->setTimezone(new \DateTimeZone('UTC'));
352-
return $when;
353-
}
354-
355-
/**
356-
* Converts a float timestamp into a \DateTimeInterface object.
357-
*
358-
* @param float $when The Unix timestamp to be converted.
359-
* @return \DateTimeInterface
360-
*/
361-
private function formatFloatTimeToDate($when)
362-
{
363-
$sec = floor($when);
364-
$micro = sprintf("%06d", ($when - $sec) * 1000000);
365-
return new \DateTime(date('Y-m-d H:i:s.'. $micro, $sec));
366-
}
367-
368335
/**
369336
* Generate a random ID for this span. Must be unique per trace,
370337
* but does not need to be globally unique.

src/Trace/TimeEvent.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
*/
2323
abstract class TimeEvent
2424
{
25+
use DateFormatTrait;
26+
2527
/**
2628
* @var \DateTimeInterface The time of this event
2729
*/
@@ -59,16 +61,6 @@ public function time()
5961
*/
6062
public function setTime($time = null)
6163
{
62-
if (!$time) {
63-
list($usec, $sec) = explode(' ', microtime());
64-
$micro = sprintf("%06d", $usec * 1000000);
65-
$time = new \DateTime(date('Y-m-d H:i:s.' . $micro));
66-
} elseif (is_numeric($time)) {
67-
// Expect that this is a timestamp
68-
$micro = sprintf("%06d", ($time - floor($time)) * 1000000);
69-
$time = new \DateTime(date('Y-m-d H:i:s.'. $micro, (int) $time));
70-
}
71-
$time->setTimezone(new \DateTimeZone('UTC'));
72-
$this->time = $time;
64+
$this->time = $this->formatDate($time);
7365
}
7466
}

tests/unit/Trace/SpanTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,9 @@ public function timestampFields()
181181
{
182182
return [
183183
['startTime', 1490737410, '2017-03-28T21:43:30.000000000Z'],
184-
['startTime', 1490737450.4843, '2017-03-28T21:44:10.484299000Z'],
184+
['startTime', 1490737450.484299, '2017-03-28T21:44:10.484299000Z'],
185185
['endTime', 1490737410, '2017-03-28T21:43:30.000000000Z'],
186-
['endTime', 1490737450.4843, '2017-03-28T21:44:10.484299000Z'],
186+
['endTime', 1490737450.484299, '2017-03-28T21:44:10.484299000Z'],
187187
];
188188
}
189189
}

0 commit comments

Comments
 (0)