InputOutputTiming::getTimestampArray()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
/*
4
 * The MIT License
5
 *
6
 * Copyright 2018 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
    use InputOutputMemory;
32
33
    public $intTimeCounter = 0;
34
35
    /**
36
     * Converts a Date from ISO-8601 format into UTC Time zone SQL format
37
     *
38
     * @param string $inStrictIso8601DtTm
39
     * @return string
40
     */
41
    public function convertDateTimeToUtcTimeZone($inStrictIso8601DtTm)
42
    {
43
        $tmpDateTimeIn = $this->convertTimeFromFormatSafely($inStrictIso8601DtTm);
44
        $tmpDateTimeIn->setTimezone(new \DateTimeZone('UTC'));
45
        return $tmpDateTimeIn->format('Y-m-d H:i:s');
46
    }
47
48
    public function convertTimeFromFormatSafely($inStrictIso8601DtTm)
49
    {
50
        $tmpDateTimeIn = \DateTime::createFromFormat(\DATE_ATOM, $inStrictIso8601DtTm);
51
        if ($tmpDateTimeIn === false) {
52
            throw new \RuntimeException(''
53
                . sprintf('Unable to create DateTime object from %s string!', $inStrictIso8601DtTm));
54
        }
55
        return $tmpDateTimeIn;
56
    }
57
58
    public function convertTimeToMilliseconds($time)
59
    {
60
        $dateTime = new \DateTime($time);
61
        $seconds  = array_sum([
62
            (((int) $dateTime->format('H')) * 3600),
63
            (((int) $dateTime->format('i')) * 60),
64
            ((int) $dateTime->format('s')),
65
        ]);
66
        return floatval($seconds . '.' . $dateTime->format('u'));
67
    }
68
69
    public function getTimestampArray($crtTime)
70
    {
71
        return [
72
            'float'  => $this->getTimestampFloat($crtTime),
73
            'string' => $this->getTimestampString($crtTime)
74
        ];
75
    }
76
77
    public function getTimestampFloat($crtTime)
78
    {
79
        $sReturn = null;
80
        if (PHP_VERSION_ID < 70307) {
81
            $sReturn = hrtime(true)[1] / pow(10, 6);
82
        } else {
83
            $sReturn = ($crtTime['sec'] + $crtTime['usec'] / pow(10, 6));
84
        }
85
        return $sReturn;
86
    }
87
88
    private function getTimestampString($dateTime)
89
    {
90
        return '<span style="color:black!important;font-weight:bold;">['
91
            . $dateTime->format('Y-m-d H:i:s.u') . ']</span>';
92
    }
93
94
    public function getTimestampedInfo()
95
    {
96
        $dateTime = new \DateTime('now', new \DateTimeZone('UTC'));
97
        $sMessage = $this->getTimestampString($dateTime) . '~' . $this->getMemoryUsageString();
98
        $this->intTimeCounter++;
99
        if (PHP_SAPI === 'cli') {
100
            return strip_tags($sMessage);
101
        }
102
        return $sMessage;
103
    }
104
}
105