Test Failed
Push — main ( db7e54...3a2f90 )
by Daniel
02:52
created

InputOutputTiming::convertTimeZoneInternational()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 9
rs 10
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 convertTimeZoneInternational(array $arrayInputs): string
79
    {
80
        $srcTz        = new \DateTimeZone($arrayInputs['SourceTimeZone']);
81
        $objTimestamp = \DateTime::createFromFormat($arrayInputs['SourceFormat'], $arrayInputs['Value'], $srcTz);
82
        $objFormat    = new \IntlDateFormatter($arrayInputs['TargetLocale'],
83
            \IntlDateFormatter::FULL, \IntlDateFormatter::FULL,
84
            $arrayInputs['TargetTimeZone'], \IntlDateFormatter::GREGORIAN,
85
            $arrayInputs['TargetFormat']);
86
        return $objFormat->format($objTimestamp);
87
    }
88
89
    public function getTimestampArray($crtTime)
90
    {
91
        return [
92
            'float'  => $this->getTimestampFloat($crtTime),
93
            'string' => $this->getTimestampString($crtTime)
94
        ];
95
    }
96
97
    public function getTimestampFloat($crtTime)
98
    {
99
        $sReturn = null;
100
        if (PHP_VERSION_ID < 70307) {
101
            $sReturn = hrtime(true)[1] / pow(10, 6);
102
        } else {
103
            $sReturn = ($crtTime['sec'] + $crtTime['usec'] / pow(10, 6));
104
        }
105
        return $sReturn;
106
    }
107
108
    private function getTimestampString($dateTime)
109
    {
110
        return '<span style="color:black!important;font-weight:bold;">['
111
            . $dateTime->format('Y-m-d H:i:s.u') . ']</span>';
112
    }
113
114
    public function getTimestampedInfo()
115
    {
116
        $dateTime = new \DateTime('now', new \DateTimeZone('UTC'));
117
        $sMessage = $this->getTimestampString($dateTime) . '~' . $this->getMemoryUsageString();
118
        $this->intTimeCounter++;
119
        if (PHP_SAPI === 'cli') {
120
            return strip_tags($sMessage);
121
        }
122
        return $sMessage;
123
    }
124
}
125