Rev 86 | Rev 89 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 86 | Rev 87 | ||
---|---|---|---|
Line 1... | Line 1... | ||
1 | <?php |
1 | <?php |
2 | 2 | ||
3 | /* |
3 | /* |
4 | * UUID utils for PHP |
4 | * UUID utils for PHP |
5 | * Copyright 2011 - 2024 Daniel Marschall, ViaThinkSoft |
5 | * Copyright 2011 - 2024 Daniel Marschall, ViaThinkSoft |
6 | * Version 2024-03-09 |
6 | * Version 2024-04-03 |
7 | * |
7 | * |
8 | * Licensed under the Apache License, Version 2.0 (the "License"); |
8 | * Licensed under the Apache License, Version 2.0 (the "License"); |
9 | * you may not use this file except in compliance with the License. |
9 | * you may not use this file except in compliance with the License. |
10 | * You may obtain a copy of the License at |
10 | * You may obtain a copy of the License at |
11 | * |
11 | * |
Line 863... | Line 863... | ||
863 | str_pad("$month",2,'0',STR_PAD_LEFT).'-'. |
863 | str_pad("$month",2,'0',STR_PAD_LEFT).'-'. |
864 | str_pad("$day",2,'0',STR_PAD_LEFT).' '. |
864 | str_pad("$day",2,'0',STR_PAD_LEFT).' '. |
865 | str_pad("$hours",2,'0',STR_PAD_LEFT).':'. |
865 | str_pad("$hours",2,'0',STR_PAD_LEFT).':'. |
866 | str_pad("$minutes",2,'0',STR_PAD_LEFT).':'. |
866 | str_pad("$minutes",2,'0',STR_PAD_LEFT).':'. |
867 | str_pad("$seconds",2,'0',STR_PAD_LEFT)."'". |
867 | str_pad("$seconds",2,'0',STR_PAD_LEFT)."'". |
868 | str_pad("$milliseconds",2,'0',STR_PAD_LEFT); |
868 | str_pad("$milliseconds",3/*ms*/,'0',STR_PAD_LEFT); |
869 | if (strpos($utc_time,'X') === false) { |
869 | if (strpos($utc_time,'X') === false) { |
870 | $deviation = "(deviation -2ms..2ms)"; |
870 | $deviation = "(deviation -2ms..2ms)"; |
871 | echo "\n<u>Interpretation of <a href=\"https://gist.github.com/danielmarschall/7fafd270a3bc107d38e8449ce7420c25\">HickelSOFT \"SQL Server Sortable Custom UUID\", Version 2</a></u>\n\n"; |
871 | echo "\n<u>Interpretation of <a href=\"https://gist.github.com/danielmarschall/7fafd270a3bc107d38e8449ce7420c25\">HickelSOFT \"SQL Server Sortable Custom UUID\", Version 2</a></u>\n\n"; |
872 | echo sprintf("%-32s %s\n", "Random 16 bits:", "[0x$rnd16bits] 0b".str_pad("".base_convert($rnd16bits, 16, 2), 16, '0', STR_PAD_LEFT)); |
872 | echo sprintf("%-32s %s\n", "Random 16 bits:", "[0x$rnd16bits] 0b".str_pad("".base_convert($rnd16bits, 16, 2), 16, '0', STR_PAD_LEFT)); |
873 | echo sprintf("%-32s %s\n", "Milliseconds:", "[0x".substr($uuid,4,2)."] $milliseconds $deviation"); |
873 | echo sprintf("%-32s %s\n", "Milliseconds:", "[0x".substr($uuid,4,2)."] $milliseconds $deviation"); |
Line 908... | Line 908... | ||
908 | str_pad("$month",2,'0',STR_PAD_LEFT).'-'. |
908 | str_pad("$month",2,'0',STR_PAD_LEFT).'-'. |
909 | str_pad("$day",2,'0',STR_PAD_LEFT).' '. |
909 | str_pad("$day",2,'0',STR_PAD_LEFT).' '. |
910 | str_pad("$hours",2,'0',STR_PAD_LEFT).':'. |
910 | str_pad("$hours",2,'0',STR_PAD_LEFT).':'. |
911 | str_pad("$minutes",2,'0',STR_PAD_LEFT).':'. |
911 | str_pad("$minutes",2,'0',STR_PAD_LEFT).':'. |
912 | str_pad("$seconds",2,'0',STR_PAD_LEFT)."'". |
912 | str_pad("$seconds",2,'0',STR_PAD_LEFT)."'". |
913 | str_pad("$milliseconds",2,'0',STR_PAD_LEFT); |
913 | str_pad("$milliseconds",3/*ms*/,'0',STR_PAD_LEFT); |
914 | if (strpos($local_time,'X') === false) { |
914 | if (strpos($local_time,'X') === false) { |
915 | $deviation = "(deviation -4ms..0ms)"; |
915 | $deviation = "(deviation -4ms..0ms)"; |
916 | echo "\n<u>Interpretation of <a href=\"https://gist.github.com/danielmarschall/7fafd270a3bc107d38e8449ce7420c25\">HickelSOFT \"SQL Server Sortable Custom UUID\", Version 1</a></u>\n\n"; |
916 | echo "\n<u>Interpretation of <a href=\"https://gist.github.com/danielmarschall/7fafd270a3bc107d38e8449ce7420c25\">HickelSOFT \"SQL Server Sortable Custom UUID\", Version 1</a></u>\n\n"; |
917 | echo sprintf("%-32s %s\n", "Random 16 bits:", "[0x$rnd16bits] 0b".str_pad(base_convert($rnd16bits, 16, 2), 16, '0', STR_PAD_LEFT)); |
917 | echo sprintf("%-32s %s\n", "Random 16 bits:", "[0x$rnd16bits] 0b".str_pad(base_convert($rnd16bits, 16, 2), 16, '0', STR_PAD_LEFT)); |
918 | echo sprintf("%-32s %s\n", "Milliseconds:", "[0x".substr($uuid,4,2)."] $milliseconds $deviation"); |
918 | echo sprintf("%-32s %s\n", "Milliseconds:", "[0x".substr($uuid,4,2)."] $milliseconds $deviation"); |
Line 1564... | Line 1564... | ||
1564 | */ |
1564 | */ |
1565 | function gen_uuid_v8_sqlserver_sortable(int $hickelUuidVersion = 2, DateTime $dt = null): string { |
1565 | function gen_uuid_v8_sqlserver_sortable(int $hickelUuidVersion = 2, DateTime $dt = null): string { |
1566 | // The sorting in SQL Server is like this: |
1566 | // The sorting in SQL Server is like this: |
1567 | 1567 | ||
1568 | if ($dt == null) $dt = new DateTime(); |
1568 | if ($dt == null) $dt = new DateTime(); |
- | 1569 | if ($hickelUuidVersion >= 2) $dt->setTimeZone(new DateTimeZone("UTC")); |
|
1569 | 1570 | ||
1570 | // First Sort block 5, nibbles from left to right (i.e. 000000000001 < 000000000010 < ... < 010000000000 < 100000000000) |
1571 | // First Sort block 5, nibbles from left to right (i.e. 000000000001 < 000000000010 < ... < 010000000000 < 100000000000) |
1571 | if ($hickelUuidVersion == 1) { |
1572 | if ($hickelUuidVersion == 1) { |
1572 | $block5 = "000000000000"; |
1573 | $block5 = "000000000000"; |
1573 | } else if ($hickelUuidVersion == 2) { |
1574 | } else if ($hickelUuidVersion == 2) { |
Line 1596... | Line 1597... | ||
1596 | $block3 = sprintf('%01x%03x', $uuidVersion, $dayOfYear); |
1597 | $block3 = sprintf('%01x%03x', $uuidVersion, $dayOfYear); |
1597 | } |
1598 | } |
1598 | 1599 | ||
1599 | // Then: Sort block 2, bytes from right to left |
1600 | // Then: Sort block 2, bytes from right to left |
1600 | if ($hickelUuidVersion == 1) { |
1601 | if ($hickelUuidVersion == 1) { |
1601 | $block2 = $dt->format('ih'); |
1602 | $block2 = $dt->format('iH'); |
1602 | } else { |
1603 | } else { |
1603 | $minuteOfDay = (intval($dt->format('i')) + intval($dt->format('h')) * 60) + 1; // 1..1440 |
1604 | $minuteOfDay = (intval($dt->format('i')) + intval($dt->format('H')) * 60) + 1; // 1..1440 |
1604 | $block2 = sprintf('%04x', $minuteOfDay); |
1605 | $block2 = sprintf('%04x', $minuteOfDay); |
1605 | } |
1606 | } |
1606 | 1607 | ||
1607 | // Then: Sort block 1, bytes from right to left |
1608 | // Then: Sort block 1, bytes from right to left |
1608 | if ($hickelUuidVersion == 1) { |
1609 | if ($hickelUuidVersion == 1) { |