GenericTimeConverterTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. declare(strict_types=1);
  3. namespace Ramsey\Uuid\Test\Converter\Time;
  4. use Ramsey\Uuid\Converter\Time\GenericTimeConverter;
  5. use Ramsey\Uuid\Math\BrickMathCalculator;
  6. use Ramsey\Uuid\Test\TestCase;
  7. use Ramsey\Uuid\Type\Hexadecimal;
  8. class GenericTimeConverterTest extends TestCase
  9. {
  10. /**
  11. * @dataProvider provideCalculateTime
  12. */
  13. public function testCalculateTime(string $seconds, string $microseconds, string $expected): void
  14. {
  15. $calculator = new BrickMathCalculator();
  16. $converter = new GenericTimeConverter($calculator);
  17. $result = $converter->calculateTime($seconds, $microseconds);
  18. $this->assertSame($expected, $result->toString());
  19. }
  20. /**
  21. * @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification
  22. */
  23. public function provideCalculateTime(): array
  24. {
  25. return [
  26. [
  27. 'seconds' => '-12219146756',
  28. 'microseconds' => '0',
  29. 'expected' => '000001540901e600',
  30. ],
  31. [
  32. 'seconds' => '103072857659',
  33. 'microseconds' => '999999',
  34. 'expected' => '0fffffffff9785f6',
  35. ],
  36. [
  37. 'seconds' => '1578612359',
  38. 'microseconds' => '521023',
  39. 'expected' => '01ea333764c71df6',
  40. ],
  41. // This is the earliest possible date supported by v1 UUIDs:
  42. // 1582-10-15 00:00:00.000000
  43. [
  44. 'seconds' => '-12219292800',
  45. 'microseconds' => '0',
  46. 'expected' => '0000000000000000',
  47. ],
  48. // This is the last possible time supported by the GenericTimeConverter:
  49. // 60038-03-11 05:36:10.955161
  50. // When a UUID is created from this time, however, the highest 4 bits
  51. // are replaced with the version (1), so we lose fidelity and cannot
  52. // accurately decompose the date from the UUID.
  53. [
  54. 'seconds' => '1832455114570',
  55. 'microseconds' => '955161',
  56. 'expected' => 'fffffffffffffffa',
  57. ],
  58. // This is technically the last possible time supported by v1 UUIDs:
  59. // 5236-03-31 21:21:00.684697
  60. // All dates above this will lose fidelity, since the highest 4 bits
  61. // are replaced with the UUID version (1). As a result, we cannot
  62. // accurately decompose the date from UUIDs created from dates
  63. // greater than this one.
  64. [
  65. 'seconds' => '103072857660',
  66. 'microseconds' => '684697',
  67. 'expected' => '0ffffffffffffffa',
  68. ],
  69. ];
  70. }
  71. /**
  72. * @dataProvider provideConvertTime
  73. */
  74. public function testConvertTime(Hexadecimal $uuidTimestamp, string $unixTimestamp, string $microseconds): void
  75. {
  76. $calculator = new BrickMathCalculator();
  77. $converter = new GenericTimeConverter($calculator);
  78. $result = $converter->convertTime($uuidTimestamp);
  79. $this->assertSame($unixTimestamp, $result->getSeconds()->toString());
  80. $this->assertSame($microseconds, $result->getMicroseconds()->toString());
  81. }
  82. /**
  83. * @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification
  84. */
  85. public function provideConvertTime(): array
  86. {
  87. return [
  88. [
  89. 'uuidTimestamp' => new Hexadecimal('1e1c57dff6f8cb0'),
  90. 'unixTimestamp' => '1341368074',
  91. 'microseconds' => '491000',
  92. ],
  93. [
  94. 'uuidTimestamp' => new Hexadecimal('1ea333764c71df6'),
  95. 'unixTimestamp' => '1578612359',
  96. 'microseconds' => '521023',
  97. ],
  98. [
  99. 'uuidTimestamp' => new Hexadecimal('fffffffff9785f6'),
  100. 'unixTimestamp' => '103072857659',
  101. 'microseconds' => '999999',
  102. ],
  103. // This is the last possible time supported by v1 UUIDs. When
  104. // converted to a Unix timestamp, the microseconds are lost.
  105. // 60038-03-11 05:36:10.955161
  106. [
  107. 'uuidTimestamp' => new Hexadecimal('fffffffffffffffa'),
  108. 'unixTimestamp' => '1832455114570',
  109. 'microseconds' => '955161',
  110. ],
  111. // This is the earliest possible date supported by v1 UUIDs:
  112. // 1582-10-15 00:00:00.000000
  113. [
  114. 'uuidTimestamp' => new Hexadecimal('000000000000'),
  115. 'unixTimestamp' => '-12219292800',
  116. 'microseconds' => '0',
  117. ],
  118. // This is the Unix epoch:
  119. // 1970-01-01 00:00:00.000000
  120. [
  121. 'uuidTimestamp' => new Hexadecimal('1b21dd213814000'),
  122. 'unixTimestamp' => '0',
  123. 'microseconds' => '0',
  124. ],
  125. ];
  126. }
  127. }