Passed
Push — master ( 52630b...060a6c )
by Chris
02:44
created

uint16_to_int16()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

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