1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpIso\Util; |
6
|
|
|
|
7
|
|
|
use Carbon\Carbon; |
8
|
|
|
|
9
|
|
|
class IsoDate |
10
|
|
|
{ |
11
|
|
|
/* |
12
|
|
|
* UTC offset = Offset from Greenwich Mean Time in number of 15 min intervals from -48 (West) to +52 (East) recorded according to 7.1.2 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Create from a "7 bytes" date |
17
|
|
|
* |
18
|
|
|
* @param array<int, int> $buffer |
19
|
|
|
*/ |
20
|
17 |
|
public static function init7(array &$buffer, int &$offset): ?Carbon |
21
|
|
|
{ |
22
|
17 |
|
$year = 1900 + (int) $buffer[$offset + 0]; |
23
|
17 |
|
$month = (int) $buffer[$offset + 1]; |
24
|
17 |
|
$day = (int) $buffer[$offset + 2]; |
25
|
17 |
|
$hour = (int) $buffer[$offset + 3]; |
26
|
17 |
|
$min = (int) $buffer[$offset + 4]; |
27
|
17 |
|
$sec = (int) $buffer[$offset + 5]; |
28
|
17 |
|
$utcOffset = $buffer[$offset + 6]; |
29
|
17 |
|
$utcOffsetHours = (int) round($utcOffset / 4); |
30
|
|
|
|
31
|
17 |
|
$offset += 7; |
32
|
|
|
|
33
|
17 |
|
if ($year === 1900 || $month === 0 || $day === 0) { |
34
|
1 |
|
return null; |
35
|
|
|
} |
36
|
|
|
|
37
|
16 |
|
return Carbon::create($year, $month, $day, $hour, $min, $sec, $utcOffsetHours); |
38
|
|
|
} |
39
|
|
|
/** |
40
|
|
|
* Create from a "17 bytes" date |
41
|
|
|
* |
42
|
|
|
* @param array<int, int> $buffer |
43
|
|
|
*/ |
44
|
19 |
|
public static function init17(array &$buffer, int &$offset): ?Carbon |
45
|
|
|
{ |
46
|
19 |
|
$date = Buffer::getString($buffer, 16, $offset); |
47
|
|
|
|
48
|
19 |
|
$year = (int) substr($date, 0, 4); |
49
|
19 |
|
$month = (int) substr($date, 4, 2); |
50
|
19 |
|
$day = (int) substr($date, 6, 2); |
51
|
19 |
|
$hour = (int) substr($date, 8, 2); |
52
|
19 |
|
$min = (int) substr($date, 10, 2); |
53
|
19 |
|
$sec = (int) substr($date, 12, 2); |
54
|
19 |
|
$ms = (int) substr($date, 14, 2); |
55
|
19 |
|
$utcOffset = (int) substr($date, 16, 2); |
56
|
19 |
|
$utcOffsetHours = (int) round($utcOffset / 4); |
57
|
|
|
|
58
|
19 |
|
$offset += 1; |
59
|
|
|
|
60
|
19 |
|
if ($year === 0 || $month === 0 || $day === 0) { |
61
|
18 |
|
return null; |
62
|
|
|
} |
63
|
|
|
|
64
|
16 |
|
$date = Carbon::create($year, $month, $day, $hour, $min, $sec, $utcOffsetHours); |
65
|
|
|
|
66
|
16 |
|
if ($date !== null) { |
67
|
16 |
|
$date->addMilliseconds($ms); |
68
|
|
|
} |
69
|
|
|
|
70
|
16 |
|
return $date; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|