Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e2bfcbe

Browse files
committed
[Uid] Replace getTime() with getDateTime()
1 parent e2e2640 commit e2bfcbe

File tree

7 files changed

+51
-47
lines changed

7 files changed

+51
-47
lines changed

src/Symfony/Component/Uid/BinaryUtil.php

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -114,22 +114,28 @@ public static function add(string $a, string $b): string
114114
return $a;
115115
}
116116

117-
public static function timeToFloat(string $time): float
117+
/**
118+
* @param string $time Count of 100-nanosecond intervals since the UUID epoch 1582-10-15 00:00:00 in hexadecimal
119+
*/
120+
public static function timeToDateTime(string $time): \DateTimeImmutable
118121
{
119122
if (\PHP_INT_SIZE >= 8) {
120-
return (hexdec($time) - self::TIME_OFFSET_INT) / 10000000;
121-
}
122-
123-
$time = str_pad(hex2bin($time), 8, "\0", \STR_PAD_LEFT);
124-
$time = self::add($time, self::TIME_OFFSET_COM2);
125-
126-
if ($time >= self::TIME_OFFSET_COM2) {
127-
$time = -1 * (self::toBase($time ^ "\xff\xff\xff\xff\xff\xff\xff\xff", self::BASE10) + 1);
123+
$time = (string) (hexdec($time) - self::TIME_OFFSET_INT);
128124
} else {
129-
$time[0] = $time[0] & "\x7F";
130-
$time = self::toBase($time, self::BASE10);
125+
$time = str_pad(hex2bin($time), 8, "\0", \STR_PAD_LEFT);
126+
$time = self::add($time, self::TIME_OFFSET_COM2);
127+
128+
if ($time >= self::TIME_OFFSET_COM2) {
129+
$time = (string) (-1 * (self::toBase($time ^ "\xff\xff\xff\xff\xff\xff\xff\xff", self::BASE10) + 1));
130+
} else {
131+
$time[0] = $time[0] & "\x7F";
132+
$time = self::toBase($time, self::BASE10);
133+
}
131134
}
132135

133-
return $time / 10000000;
136+
$time = '-' !== $time[0] ? str_pad($time, 8, '0', STR_PAD_LEFT) : '-'.str_pad(substr($time, 1), 8, '0', STR_PAD_LEFT);
137+
$time = substr($time, 0, -1);
138+
139+
return \DateTimeImmutable::createFromFormat('U.u', substr($time, 0, -6).'.'.substr($time, -6), new \DateTimeZone('UTC'));
134140
}
135141
}

src/Symfony/Component/Uid/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Add `AbstractUid::fromBinary()`, `AbstractUid::fromBase58()`, `AbstractUid::fromBase32()` and `AbstractUid::fromRfc4122()`
8+
* [BC BREAK] Replace `UuidV1::getTime()`, `UuidV6::getTime()` and `Ulid::getTime()` with `UuidV1::getDateTime()`, `UuidV6::getDateTime()` and `Ulid::getDateTime()`.
89

910
5.2.0
1011
-----

src/Symfony/Component/Uid/Tests/UlidTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,18 @@ public function testBase58()
7676
/**
7777
* @group time-sensitive
7878
*/
79-
public function testGetTime()
79+
public function testGetDateTime()
8080
{
8181
$time = microtime(false);
8282
$ulid = new Ulid();
8383
$time = substr($time, 11).substr($time, 1, 4);
8484

85-
$this->assertSame((float) $time, $ulid->getTime());
85+
$this->assertEquals(\DateTimeImmutable::createFromFormat('U.u', $time), $ulid->getDateTime());
86+
87+
$this->assertEquals(new \DateTimeImmutable('@0'), (new Ulid('000000000079KA1307SR9X4MV3'))->getDateTime());
88+
$this->assertEquals(\DateTimeImmutable::createFromFormat('U.u', '0.001'), (new Ulid('000000000179KA1307SR9X4MV3'))->getDateTime());
89+
$this->assertEquals(\DateTimeImmutable::createFromFormat('U.u', '281474976710.654'), (new Ulid('7ZZZZZZZZY79KA1307SR9X4MV3'))->getDateTime());
90+
$this->assertEquals(\DateTimeImmutable::createFromFormat('U.u', '281474976710.655'), (new Ulid('7ZZZZZZZZZ79KA1307SR9X4MV3'))->getDateTime());
8691
}
8792

8893
public function testIsValid()

src/Symfony/Component/Uid/Tests/UuidTest.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function testV1()
6060

6161
$uuid = new UuidV1(self::A_UUID_V1);
6262

63-
$this->assertSame(1583245966.746458, $uuid->getTime());
63+
$this->assertEquals(\DateTimeImmutable::createFromFormat('U.u', '1583245966.746458'), $uuid->getDateTime());
6464
$this->assertSame('3499710062d0', $uuid->getNode());
6565
}
6666

@@ -95,7 +95,7 @@ public function testV6()
9595

9696
$uuid = new UuidV6(substr_replace(self::A_UUID_V1, '6', 14, 1));
9797

98-
$this->assertSame(85916308548.27832, $uuid->getTime());
98+
$this->assertEquals(\DateTimeImmutable::createFromFormat('U.u', '85916308548.278321'), $uuid->getDateTime());
9999
$this->assertSame('3499710062d0', $uuid->getNode());
100100
}
101101

@@ -308,12 +308,14 @@ public function testFromStringOnExtendedClassReturnsStatic()
308308
$this->assertInstanceOf(CustomUuid::class, CustomUuid::fromString(self::A_UUID_V4));
309309
}
310310

311-
public function testGetTime()
311+
public function testGetDateTime()
312312
{
313-
$this->assertSame(103072857660.6847, ((new UuidV1('ffffffff-ffff-1fff-a456-426655440000'))->getTime()));
314-
$this->assertSame(0.0000001, ((new UuidV1('13814001-1dd2-11b2-a456-426655440000'))->getTime()));
315-
$this->assertSame(0.0, (new UuidV1('13814000-1dd2-11b2-a456-426655440000'))->getTime());
316-
$this->assertSame(-0.0000001, (new UuidV1('13813fff-1dd2-11b2-a456-426655440000'))->getTime());
317-
$this->assertSame(-12219292800.0, ((new UuidV1('00000000-0000-1000-a456-426655440000'))->getTime()));
313+
$this->assertEquals(\DateTimeImmutable::createFromFormat('U.u', '103072857660.684697'), ((new UuidV1('ffffffff-ffff-1fff-a456-426655440000'))->getDateTime()));
314+
$this->assertEquals(\DateTimeImmutable::createFromFormat('U.u', '0.000001'), ((new UuidV1('1381400a-1dd2-11b2-a456-426655440000'))->getDateTime()));
315+
$this->assertEquals(new \DateTimeImmutable('@0'), (new UuidV1('13814001-1dd2-11b2-a456-426655440000'))->getDateTime());
316+
$this->assertEquals(new \DateTimeImmutable('@0'), (new UuidV1('13814000-1dd2-11b2-a456-426655440000'))->getDateTime());
317+
$this->assertEquals(new \DateTimeImmutable('@0'), (new UuidV1('13813fff-1dd2-11b2-a456-426655440000'))->getDateTime());
318+
$this->assertEquals(\DateTimeImmutable::createFromFormat('U.u', '-0.000001'), ((new UuidV1('13813ff6-1dd2-11b2-a456-426655440000'))->getDateTime()));
319+
$this->assertEquals(new \DateTimeImmutable('@-12219292800'), ((new UuidV1('00000000-0000-1000-a456-426655440000'))->getDateTime()));
318320
}
319321
}

src/Symfony/Component/Uid/Ulid.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,24 +104,24 @@ public function toBase32(): string
104104
return $this->uid;
105105
}
106106

107-
/**
108-
* @return float Seconds since the Unix epoch 1970-01-01 00:00:00
109-
*/
110-
public function getTime(): float
107+
public function getDateTime(): \DateTimeImmutable
111108
{
112109
$time = strtr(substr($this->uid, 0, 10), 'ABCDEFGHJKMNPQRSTVWXYZ', 'abcdefghijklmnopqrstuv');
113110

114111
if (\PHP_INT_SIZE >= 8) {
115-
return hexdec(base_convert($time, 32, 16)) / 1000;
112+
$time = hexdec(base_convert($time, 32, 16));
113+
} else {
114+
$time = sprintf('%02s%05s%05s',
115+
base_convert(substr($time, 0, 2), 32, 16),
116+
base_convert(substr($time, 2, 4), 32, 16),
117+
base_convert(substr($time, 6, 4), 32, 16)
118+
);
119+
$time = BinaryUtil::toBase(hex2bin($time), BinaryUtil::BASE10);
116120
}
117121

118-
$time = sprintf('%02s%05s%05s',
119-
base_convert(substr($time, 0, 2), 32, 16),
120-
base_convert(substr($time, 2, 4), 32, 16),
121-
base_convert(substr($time, 6, 4), 32, 16)
122-
);
122+
$time = str_pad($time, 6, '0', STR_PAD_LEFT);
123123

124-
return BinaryUtil::toBase(hex2bin($time), BinaryUtil::BASE10) / 1000;
124+
return \DateTimeImmutable::createFromFormat('U.u', substr($time, 0, -3).'.'.substr($time, -3), new \DateTimeZone('UTC'));
125125
}
126126

127127
private static function generate(): string

src/Symfony/Component/Uid/UuidV1.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,9 @@ public function __construct(string $uuid = null)
3131
}
3232
}
3333

34-
/**
35-
* @return float Seconds since the Unix epoch 1970-01-01 00:00:00
36-
*/
37-
public function getTime(): float
34+
public function getDateTime(): \DateTimeImmutable
3835
{
39-
$time = '0'.substr($this->uid, 15, 3).substr($this->uid, 9, 4).substr($this->uid, 0, 8);
40-
41-
return BinaryUtil::timeToFloat($time);
36+
return BinaryUtil::timeToDateTime('0'.substr($this->uid, 15, 3).substr($this->uid, 9, 4).substr($this->uid, 0, 8));
4237
}
4338

4439
public function getNode(): string

src/Symfony/Component/Uid/UuidV6.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,9 @@ public function __construct(string $uuid = null)
5050
}
5151
}
5252

53-
/**
54-
* @return float Seconds since the Unix epoch 1970-01-01 00:00:00
55-
*/
56-
public function getTime(): float
53+
public function getDateTime(): \DateTimeImmutable
5754
{
58-
$time = '0'.substr($this->uid, 0, 8).substr($this->uid, 9, 4).substr($this->uid, 15, 3);
59-
60-
return BinaryUtil::timeToFloat($time);
55+
return BinaryUtil::timeToDateTime('0'.substr($this->uid, 0, 8).substr($this->uid, 9, 4).substr($this->uid, 15, 3));
6156
}
6257

6358
public function getNode(): string

0 commit comments

Comments
 (0)