Rev 1469 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1469 | Rev 1470 | ||
---|---|---|---|
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-05 |
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 813... | Line 813... | ||
813 | } |
813 | } |
814 | } |
814 | } |
815 | 815 | ||
816 | // END: OIDplus 2.0 Custom UUID Interpretation |
816 | // END: OIDplus 2.0 Custom UUID Interpretation |
817 | 817 | ||
- | 818 | // START: hayageek UUIDv8 Interpretation |
|
- | 819 | // https://github.com/hayageek/uuid-v8 |
|
- | 820 | // Example: 07e80404-1736-8934-8374-e63eb25babd2 |
|
- | 821 | ||
- | 822 | $year = hexdec(substr($uuid,0,4)); |
|
- | 823 | $month = hexdec(substr($uuid,4,2)); |
|
- | 824 | $day = hexdec(substr($uuid,6,2)); |
|
- | 825 | $hours = hexdec(substr($uuid,8,2)); |
|
- | 826 | $minutes = hexdec(substr($uuid,10,2)); |
|
- | 827 | $ver = hexdec(substr($uuid,12,1)); |
|
- | 828 | $rnd1 = substr($uuid,13,1); |
|
- | 829 | $seconds = hexdec(substr($uuid,14,2)); |
|
- | 830 | ||
- | 831 | // Diagram (WRONG): <2 variant 0b10> <8 msec> <6 random> |
|
- | 832 | // Actual Code: <2 variant 0b10> <2 zero> <12 msec 0..0x3E7> |
|
- | 833 | $tmp = hexdec(substr($uuid,16,4)); |
|
- | 834 | ||
- | 835 | $var = ($tmp >> 14) & 0b11; |
|
- | 836 | $zero = ($tmp >> 12) & 0b11; |
|
- | 837 | $milliseconds = $tmp & 0xFFF; |
|
- | 838 | ||
- | 839 | $rnd2 = substr($uuid,20,12); |
|
- | 840 | ||
- | 841 | $utc_time = |
|
- | 842 | str_pad("$year",4,'0',STR_PAD_LEFT).'-'. |
|
- | 843 | str_pad("$month",2,'0',STR_PAD_LEFT).'-'. |
|
- | 844 | str_pad("$day",2,'0',STR_PAD_LEFT).' '. |
|
- | 845 | str_pad("$hours",2,'0',STR_PAD_LEFT).':'. |
|
- | 846 | str_pad("$minutes",2,'0',STR_PAD_LEFT).':'. |
|
- | 847 | str_pad("$seconds",2,'0',STR_PAD_LEFT)."'". |
|
- | 848 | str_pad("$milliseconds",3/*ms*/,'0',STR_PAD_LEFT); |
|
- | 849 | ||
- | 850 | $date_valid = checkdate($month, $day, $year); |
|
- | 851 | $time_valid = ($hours >= 0) && ($hours <= 23) && |
|
- | 852 | ($minutes >= 0) && ($minutes <= 59) && |
|
- | 853 | ($seconds >= 0) && ($seconds <= 59) && |
|
- | 854 | ($milliseconds >= 0) && ($milliseconds <= 999); /**@phpstan-ignore-line*/ |
|
- | 855 | ||
- | 856 | if (($ver == 8) && ($var == 2) && ($zero == 0) && $date_valid && $time_valid) { |
|
- | 857 | echo "\n<u>Interpretation of <a href=\"https://github.com/hayageek/uuid-v8\">hayageek UUIDv8</a></u>\n\n"; |
|
- | 858 | ||
- | 859 | echo sprintf("%-32s %s\n", "Date/Time:", "$utc_time UTC"); |
|
- | 860 | echo sprintf("%-32s %s\n", "Random Data:", "0x$rnd1$rnd2"); |
|
- | 861 | echo sprintf("%-32s %s\n", "Year:", sprintf("[0x%04x] %d", $year, $year)); |
|
- | 862 | echo sprintf("%-32s %s\n", "Month:", sprintf("[0x%02x] %d", $month, $month)); |
|
- | 863 | echo sprintf("%-32s %s\n", "Day:", sprintf("[0x%02x] %d", $day, $day)); |
|
- | 864 | echo sprintf("%-32s %s\n", "Hours:", sprintf("[0x%02x] %d", $hours, $hours)); |
|
- | 865 | echo sprintf("%-32s %s\n", "Minutes:", sprintf("[0x%02x] %d", $minutes, $minutes)); |
|
- | 866 | echo sprintf("%-32s %s\n", "Random 1:", sprintf("[0x%s]", $rnd1)); |
|
- | 867 | echo sprintf("%-32s %s\n", "Seconds:", sprintf("[0x%02x] %d", $seconds, $seconds)); |
|
- | 868 | echo sprintf("%-32s %s\n", "Milliseconds:", sprintf("[0x%03x] %d", $milliseconds, $milliseconds)); |
|
- | 869 | echo sprintf("%-32s %s\n", "Random 2:", sprintf("[0x%s]", $rnd2)); |
|
- | 870 | } |
|
- | 871 | ||
- | 872 | // END: hayageek UUIDv8 Interpretation |
|
- | 873 | ||
818 | break; |
874 | break; |
819 | default: |
875 | default: |
820 | echo sprintf("%-32s %s\n", "Version:", "[0x".dechex($version)."] Unknown"); |
876 | echo sprintf("%-32s %s\n", "Version:", "[0x".dechex($version)."] Unknown"); |
821 | break; |
877 | break; |
822 | } |
878 | } |
Line 863... | Line 919... | ||
863 | str_pad("$month",2,'0',STR_PAD_LEFT).'-'. |
919 | str_pad("$month",2,'0',STR_PAD_LEFT).'-'. |
864 | str_pad("$day",2,'0',STR_PAD_LEFT).' '. |
920 | str_pad("$day",2,'0',STR_PAD_LEFT).' '. |
865 | str_pad("$hours",2,'0',STR_PAD_LEFT).':'. |
921 | str_pad("$hours",2,'0',STR_PAD_LEFT).':'. |
866 | str_pad("$minutes",2,'0',STR_PAD_LEFT).':'. |
922 | str_pad("$minutes",2,'0',STR_PAD_LEFT).':'. |
867 | str_pad("$seconds",2,'0',STR_PAD_LEFT)."'". |
923 | str_pad("$seconds",2,'0',STR_PAD_LEFT)."'". |
868 | str_pad("$milliseconds",2,'0',STR_PAD_LEFT); |
924 | str_pad("$milliseconds",3/*ms*/,'0',STR_PAD_LEFT); |
869 | if (strpos($utc_time,'X') === false) { |
925 | if (checkdate($month, $day, $year) && (strpos($utc_time,'X') === false)) { |
870 | $deviation = "(deviation -2ms..2ms)"; |
926 | $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"; |
927 | 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)); |
928 | 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"); |
929 | echo sprintf("%-32s %s\n", "Milliseconds:", "[0x".substr($uuid,4,2)."] $milliseconds $deviation"); |
874 | echo sprintf("%-32s %s\n", "Seconds:", "[0x".substr($uuid,6,2)."] $seconds"); |
930 | echo sprintf("%-32s %s\n", "Seconds:", "[0x".substr($uuid,6,2)."] $seconds"); |
Line 908... | Line 964... | ||
908 | str_pad("$month",2,'0',STR_PAD_LEFT).'-'. |
964 | str_pad("$month",2,'0',STR_PAD_LEFT).'-'. |
909 | str_pad("$day",2,'0',STR_PAD_LEFT).' '. |
965 | str_pad("$day",2,'0',STR_PAD_LEFT).' '. |
910 | str_pad("$hours",2,'0',STR_PAD_LEFT).':'. |
966 | str_pad("$hours",2,'0',STR_PAD_LEFT).':'. |
911 | str_pad("$minutes",2,'0',STR_PAD_LEFT).':'. |
967 | str_pad("$minutes",2,'0',STR_PAD_LEFT).':'. |
912 | str_pad("$seconds",2,'0',STR_PAD_LEFT)."'". |
968 | str_pad("$seconds",2,'0',STR_PAD_LEFT)."'". |
913 | str_pad("$milliseconds",2,'0',STR_PAD_LEFT); |
969 | str_pad("$milliseconds",3/*ms*/,'0',STR_PAD_LEFT); |
914 | if (strpos($local_time,'X') === false) { |
970 | if (checkdate($month, $day, $year) && (strpos($local_time,'X') === false)) { |
915 | $deviation = "(deviation -4ms..0ms)"; |
971 | $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"; |
972 | 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)); |
973 | 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"); |
974 | echo sprintf("%-32s %s\n", "Milliseconds:", "[0x".substr($uuid,4,2)."] $milliseconds $deviation"); |
919 | echo sprintf("%-32s %s\n", "Seconds:", "[0x".substr($uuid,6,2)."] $seconds"); |
975 | echo sprintf("%-32s %s\n", "Seconds:", "[0x".substr($uuid,6,2)."] $seconds"); |
Line 1564... | Line 1620... | ||
1564 | */ |
1620 | */ |
1565 | function gen_uuid_v8_sqlserver_sortable(int $hickelUuidVersion = 2, DateTime $dt = null): string { |
1621 | function gen_uuid_v8_sqlserver_sortable(int $hickelUuidVersion = 2, DateTime $dt = null): string { |
1566 | // The sorting in SQL Server is like this: |
1622 | // The sorting in SQL Server is like this: |
1567 | 1623 | ||
1568 | if ($dt == null) $dt = new DateTime(); |
1624 | if ($dt == null) $dt = new DateTime(); |
- | 1625 | if ($hickelUuidVersion >= 2) $dt->setTimeZone(new DateTimeZone("UTC")); |
|
1569 | 1626 | ||
1570 | // First Sort block 5, nibbles from left to right (i.e. 000000000001 < 000000000010 < ... < 010000000000 < 100000000000) |
1627 | // First Sort block 5, nibbles from left to right (i.e. 000000000001 < 000000000010 < ... < 010000000000 < 100000000000) |
1571 | if ($hickelUuidVersion == 1) { |
1628 | if ($hickelUuidVersion == 1) { |
1572 | $block5 = "000000000000"; |
1629 | $block5 = "000000000000"; |
1573 | } else if ($hickelUuidVersion == 2) { |
1630 | } else if ($hickelUuidVersion == 2) { |
Line 1596... | Line 1653... | ||
1596 | $block3 = sprintf('%01x%03x', $uuidVersion, $dayOfYear); |
1653 | $block3 = sprintf('%01x%03x', $uuidVersion, $dayOfYear); |
1597 | } |
1654 | } |
1598 | 1655 | ||
1599 | // Then: Sort block 2, bytes from right to left |
1656 | // Then: Sort block 2, bytes from right to left |
1600 | if ($hickelUuidVersion == 1) { |
1657 | if ($hickelUuidVersion == 1) { |
1601 | $block2 = $dt->format('ih'); |
1658 | $block2 = $dt->format('iH'); |
1602 | } else { |
1659 | } else { |
1603 | $minuteOfDay = (intval($dt->format('i')) + intval($dt->format('h')) * 60) + 1; // 1..1440 |
1660 | $minuteOfDay = (intval($dt->format('i')) + intval($dt->format('H')) * 60) + 1; // 1..1440 |
1604 | $block2 = sprintf('%04x', $minuteOfDay); |
1661 | $block2 = sprintf('%04x', $minuteOfDay); |
1605 | } |
1662 | } |
1606 | 1663 | ||
1607 | // Then: Sort block 1, bytes from right to left |
1664 | // Then: Sort block 1, bytes from right to left |
1608 | if ($hickelUuidVersion == 1) { |
1665 | if ($hickelUuidVersion == 1) { |