forked from ramsey/uuid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDegradedUuid.php
154 lines (141 loc) · 6.16 KB
/
DegradedUuid.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
namespace Ramsey\Uuid;
use Ramsey\Uuid\Converter\NumberConverterInterface;
class DegradedUuid extends Uuid
{
public function __construct(array $fields, NumberConverterInterface $converter, CodecInterface $codec)
{
parent::__construct($fields, $converter, $codec);
}
/**
* Returns a PHP DateTime object representing the timestamp associated
* with this UUID.
*
* The timestamp value is only meaningful in a time-based UUID, which
* has version type 1. If this UUID is not a time-based UUID then
* this method throws UnsupportedOperationException.
*
* @return \DateTime A PHP DateTime representation of the date
* @throws Exception\UnsupportedOperationException If this UUID is not a version 1 UUID
* @throws Exception\UnsatisfiedDependencyException if called on a 32-bit system and
* Moontoast\Math\BigNumber is not present
*/
public function getDateTime()
{
if ($this->getVersion() != 1) {
throw new Exception\UnsupportedOperationException('Not a time-based UUID');
}
$time = $this->converter->fromHex($this->getTimestampHex());
$ts = new \Moontoast\Math\BigNumber($time, 20);
$ts->subtract('122192928000000000');
$ts->divide('10000000.0');
$ts->round();
$unixTime = $ts->getValue();
return new \DateTime("@{$unixTime}");
}
/**
* Returns an array of the fields of this UUID, with keys named according
* to the RFC 4122 names for the fields.
*
* * **time_low**: The low field of the timestamp, an unsigned 32-bit integer
* * **time_mid**: The middle field of the timestamp, an unsigned 16-bit integer
* * **time_hi_and_version**: The high field of the timestamp multiplexed with
* the version number, an unsigned 16-bit integer
* * **clock_seq_hi_and_reserved**: The high field of the clock sequence
* multiplexed with the variant, an unsigned 8-bit integer
* * **clock_seq_low**: The low field of the clock sequence, an unsigned
* 8-bit integer
* * **node**: The spatially unique node identifier, an unsigned 48-bit
* integer
*
* @return array The UUID fields represented as integer values
* @throws Exception\UnsatisfiedDependencyException if called on a 32-bit system
* @link http://tools.ietf.org/html/rfc4122#section-4.1.2
*/
public function getFields()
{
throw new Exception\UnsatisfiedDependencyException(
'Cannot call ' . __METHOD__ . ' on a 32-bit system, since some '
. 'values overflow the system max integer value'
. '; consider calling getFieldsHex instead'
);
}
/**
* Returns the node value associated with this UUID
*
* For UUID version 1, the node field consists of an IEEE 802 MAC
* address, usually the host address. For systems with multiple IEEE
* 802 addresses, any available one can be used. The lowest addressed
* octet (octet number 10) contains the global/local bit and the
* unicast/multicast bit, and is the first octet of the address
* transmitted on an 802.3 LAN.
*
* For systems with no IEEE address, a randomly or pseudo-randomly
* generated value may be used; see RFC 4122, Section 4.5. The
* multicast bit must be set in such addresses, in order that they
* will never conflict with addresses obtained from network cards.
*
* For UUID version 3 or 5, the node field is a 48-bit value constructed
* from a name as described in RFC 4122, Section 4.3.
*
* For UUID version 4, the node field is a randomly or pseudo-randomly
* generated 48-bit value as described in RFC 4122, Section 4.4.
*
* @return int Unsigned 48-bit integer value of node
* @link http://tools.ietf.org/html/rfc4122#section-4.1.6
* @throws Exception\UnsatisfiedDependencyException if called on a 32-bit system
*/
public function getNode()
{
throw new Exception\UnsatisfiedDependencyException(
'Cannot call ' . __METHOD__ . ' on a 32-bit system, since node '
. 'is an unsigned 48-bit integer and can overflow the system '
. 'max integer value'
. '; consider calling getNodeHex instead'
);
}
/**
* Returns the low field of the timestamp (the first 32 bits of the UUID).
*
* @return int Unsigned 32-bit integer value of time_low
* @throws Exception\UnsatisfiedDependencyException if called on a 32-bit system
*/
public function getTimeLow()
{
throw new Exception\UnsatisfiedDependencyException(
'Cannot call ' . __METHOD__ . ' on a 32-bit system, since time_low '
. 'is an unsigned 32-bit integer and can overflow the system '
. 'max integer value'
. '; consider calling getTimeLowHex instead'
);
}
/**
* The timestamp value associated with this UUID
*
* The 60 bit timestamp value is constructed from the time_low,
* time_mid, and time_hi fields of this UUID. The resulting
* timestamp is measured in 100-nanosecond units since midnight,
* October 15, 1582 UTC.
*
* The timestamp value is only meaningful in a time-based UUID, which
* has version type 1. If this UUID is not a time-based UUID then
* this method throws UnsupportedOperationException.
*
* @return int Unsigned 60-bit integer value of the timestamp
* @throws Exception\UnsupportedOperationException If this UUID is not a version 1 UUID
* @throws Exception\UnsatisfiedDependencyException if called on a 32-bit system
* @link http://tools.ietf.org/html/rfc4122#section-4.1.4
*/
public function getTimestamp()
{
if ($this->getVersion() != 1) {
throw new Exception\UnsupportedOperationException('Not a time-based UUID');
}
throw new Exception\UnsatisfiedDependencyException(
'Cannot call ' . __METHOD__ . ' on a 32-bit system, since timestamp '
. 'is an unsigned 60-bit integer and can overflow the system '
. 'max integer value'
. '; consider calling getTimestampHex instead'
);
}
}