1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* The MIT License |
5
|
|
|
* |
6
|
|
|
* Copyright 2018 - 2024 Daniel Popiniuc |
7
|
|
|
* |
8
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
9
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
10
|
|
|
* in the Software without restriction, including without limitation the rights |
11
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
12
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
13
|
|
|
* furnished to do so, subject to the following conditions: |
14
|
|
|
* |
15
|
|
|
* The above copyright notice and this permission notice shall be included in |
16
|
|
|
* all copies or substantial portions of the Software. |
17
|
|
|
* |
18
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
19
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
20
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
21
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
22
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
23
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
24
|
|
|
* THE SOFTWARE. |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
namespace danielgp\io_operations; |
28
|
|
|
|
29
|
|
|
trait InputOutputTiming |
30
|
|
|
{ |
31
|
|
|
|
32
|
|
|
use InputOutputMemory; |
33
|
|
|
|
34
|
|
|
public $intTimeCounter = 0; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Converts a Date from ISO-8601 format into UTC Time zone SQL format |
38
|
|
|
* |
39
|
|
|
* @param string $inStrictIso8601DtTm |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
|
|
public function convertDateTimeToUtcTimeZone($inStrictIso8601DtTm) |
43
|
|
|
{ |
44
|
|
|
$tmpDateTimeIn = $this->convertTimeFromFormatSafely($inStrictIso8601DtTm); |
45
|
|
|
$tmpDateTimeIn->setTimezone(new \DateTimeZone('UTC')); |
46
|
|
|
return $tmpDateTimeIn->format('Y-m-d H:i:s'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function convertTimeFromFormatSafely($inStrictIso8601DtTm) |
50
|
|
|
{ |
51
|
|
|
$tmpDateTimeIn = \DateTime::createFromFormat(\DATE_ATOM, $inStrictIso8601DtTm); |
52
|
|
|
if ($tmpDateTimeIn === false) { |
53
|
|
|
throw new \RuntimeException('' |
54
|
|
|
. sprintf('Unable to create DateTime object from %s string!', $inStrictIso8601DtTm)); |
55
|
|
|
} |
56
|
|
|
return $tmpDateTimeIn; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function convertTimeToMilliseconds($time) |
60
|
|
|
{ |
61
|
|
|
$dateTime = new \DateTime($time); |
62
|
|
|
$seconds = array_sum([ |
63
|
|
|
(((int) $dateTime->format('H')) * 3600), |
64
|
|
|
(((int) $dateTime->format('i')) * 60), |
65
|
|
|
((int) $dateTime->format('s')), |
66
|
|
|
]); |
67
|
|
|
return floatval($seconds . '.' . $dateTime->format('u')); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function convertTimeZoneFlexible(array $arrayInputs): string |
71
|
|
|
{ |
72
|
|
|
$srcTimeZone = new \DateTimeZone($arrayInputs['SourceTimeZone']); |
73
|
|
|
$objTimestamp = \DateTime::createFromFormat($arrayInputs['SourceFormat'], $arrayInputs['Value'], $srcTimeZone); |
74
|
|
|
$objTimestamp->setTimezone(new \DateTimeZone($arrayInputs['TargetTimeZone'])); |
75
|
|
|
return $objTimestamp->format($arrayInputs['TargetFormat']); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function getTimestampArray($crtTime) |
79
|
|
|
{ |
80
|
|
|
return [ |
81
|
|
|
'float' => $this->getTimestampFloat($crtTime), |
82
|
|
|
'string' => $this->getTimestampString($crtTime) |
83
|
|
|
]; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function getTimestampFloat($crtTime) |
87
|
|
|
{ |
88
|
|
|
$sReturn = null; |
89
|
|
|
if (PHP_VERSION_ID < 70307) { |
90
|
|
|
$sReturn = hrtime(true)[1] / pow(10, 6); |
91
|
|
|
} else { |
92
|
|
|
$sReturn = ($crtTime['sec'] + $crtTime['usec'] / pow(10, 6)); |
93
|
|
|
} |
94
|
|
|
return $sReturn; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
private function getTimestampString($dateTime) |
98
|
|
|
{ |
99
|
|
|
return '<span style="color:black!important;font-weight:bold;">[' |
100
|
|
|
. $dateTime->format('Y-m-d H:i:s.u') . ']</span>'; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function getTimestampedInfo() |
104
|
|
|
{ |
105
|
|
|
$dateTime = new \DateTime('now', new \DateTimeZone('UTC')); |
106
|
|
|
$sMessage = $this->getTimestampString($dateTime) . '~' . $this->getMemoryUsageString(); |
107
|
|
|
$this->intTimeCounter++; |
108
|
|
|
if (PHP_SAPI === 'cli') { |
109
|
|
|
return strip_tags($sMessage); |
110
|
|
|
} |
111
|
|
|
return $sMessage; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|