Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1023e26

Browse filesBrowse files
fancywebnicolas-grekas
authored andcommitted
[Uid] Replace getTime() with getDateTime()
1 parent 8f9fe02 commit 1023e26
Copy full SHA for 1023e26

File tree

7 files changed

+42
-38
lines changed
Filter options

7 files changed

+42
-38
lines changed

‎src/Symfony/Component/Uid/BinaryUtil.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Uid/BinaryUtil.php
+7-3Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,10 @@ public static function add(string $a, string $b): string
119119
/**
120120
* @param string $time Count of 100-nanosecond intervals since the UUID epoch 1582-10-15 00:00:00 in hexadecimal
121121
*/
122-
public static function timeToFloat(string $time): float
122+
public static function timeToDateTime(string $time): \DateTimeImmutable
123123
{
124124
if (\PHP_INT_SIZE >= 8) {
125-
$time = hexdec($time) - self::TIME_OFFSET_INT;
125+
$time = (string) (hexdec($time) - self::TIME_OFFSET_INT);
126126
} else {
127127
$time = str_pad(hex2bin($time), 8, "\0", \STR_PAD_LEFT);
128128

@@ -136,6 +136,10 @@ public static function timeToFloat(string $time): float
136136
}
137137
}
138138

139-
return $time / 10000000;
139+
if (9 > \strlen($time)) {
140+
$time = '-' === $time[0] ? '-'.str_pad(substr($time, 1), 8, '0', \STR_PAD_LEFT) : str_pad($time, 8, '0', \STR_PAD_LEFT);
141+
}
142+
143+
return \DateTimeImmutable::createFromFormat('U.u?', substr_replace($time, '.', -7, 0));
140144
}
141145
}

‎src/Symfony/Component/Uid/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Uid/CHANGELOG.md
+1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Uid/Tests/UlidTest.php
+7-2Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Uid/Tests/UuidTest.php
+10-8Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Uid/Ulid.php
+13-11Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,24 +104,26 @@ 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 = (string) 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+
if (4 > \strlen($time)) {
123+
$time = str_pad($time, 4, '0', \STR_PAD_LEFT);
124+
}
123125

124-
return BinaryUtil::toBase(hex2bin($time), BinaryUtil::BASE10) / 1000;
126+
return \DateTimeImmutable::createFromFormat('U.u', substr_replace($time, '.', -3, 0));
125127
}
126128

127129
private static function generate(): string

‎src/Symfony/Component/Uid/UuidV1.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Uid/UuidV1.php
+2-7Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Uid/UuidV6.php
+2-7Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.