Completed
Push — master ( 23013c...52630b )
by Chris
04:20
created

nanotime_to_datetimeimmutable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 1
dl 0
loc 18
ccs 0
cts 10
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace DaveRandom\LibLifxLan;
4
5
// @codeCoverageIgnoreStart
6
use DaveRandom\LibLifxLan\Exceptions\InvalidValueException;
7
8
if (\strlen(\pack('e', 0.1)) === 4) {
9
    \define(__NAMESPACE__ . '\\FLOAT32_CODE', 'e');
10
} else if (\strlen(\pack('g', 0.1)) === 4) {
11
    \define(__NAMESPACE__ . '\\FLOAT32_CODE', 'g');
12
} else {
13
    /** @noinspection PhpUnhandledExceptionInspection */
14
    throw new \Error('Cannot pack()/unpack() floating point numbers to a 32-bit little-endian representation');
15
}
16
17
const UINT32_MIN = \PHP_INT_SIZE === 4 ? \PHP_INT_MIN : 0;
18
const UINT32_MAX = \PHP_INT_SIZE === 4 ? \PHP_INT_MIN : 0xffffffff;
19
// @codeCoverageIgnoreEnd
20
21
/**
22
 * @param \DateTimeInterface $dateTime
23
 * @return \DateTimeImmutable
24
 */
25
function datetimeinterface_to_datetimeimmutable(\DateTimeInterface $dateTime): \DateTimeImmutable
26
{
27 50
    if ($dateTime instanceof \DateTimeImmutable) {
28 25
        return $dateTime;
29
    }
30
31 25
    \assert($dateTime instanceof \DateTime, new \Error('DateTimeInterface is not DateTimeImmutable or DateTime???'));
32
33 25
    return \DateTimeImmutable::createFromMutable($dateTime);
34
}
35
36
/**
37
 * @param int $timestamp
38
 * @return \DateTimeImmutable
39
 * @throws InvalidValueException
40
 */
41
function nanotime_to_datetimeimmutable(int $timestamp): \DateTimeImmutable
42
{
43
    static $utcTimeZone;
44
45
    $usecs = (int)(($timestamp % 1000000000) / 1000);
46
    $secs = (int)($timestamp / 1000000000);
47
48
    $result = \DateTimeImmutable::createFromFormat(
49
        'u U',
50
        \sprintf("%06d %d", $usecs, $secs),
51
        $utcTimeZone ?? ($utcTimeZone = new \DateTimeZone('UTC'))
52
    );
53
54
    if ($result === false) {
55
        throw new InvalidValueException("Could not convert nanotime to DateTimeImmutable instance");
56
    }
57
58
    return $result;
59
}
60
61
/**
62
 * @param \DateTimeInterface $dateTime
63
 * @return int
64
 * @throws InvalidValueException
65
 */
66
function datetimeinterface_to_nanotime(\DateTimeInterface $dateTime): int
67
{
68
    $result = ($dateTime->format('U') * 1000000000) + ($dateTime->format('u') * 1000);
69
70
    if ($result < 0) {
71
        throw new InvalidValueException("Timestamp {$dateTime->format('Y-m-d H:i:s.u')} is negative");
72
    }
73
74
    return $result;
75
}
76
77
function int16_to_uint16(int $signed): int
78
{
79
    if ($signed >= 0) {
80
        return $signed & 0x7fff;
81
    }
82
83
    return 0x8000 | (($signed & 0x7fff) + 1);
84
}
85
86
function uint16_to_int16(int $unsigned): int
87
{
88
    if (!($unsigned & 0x8000)) {
89
        return $unsigned;
90
    }
91
92
    return -(($unsigned & 0x7fff) + 1);
93
}
94