Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix off-by-one error and minor improvements #35

Open
wants to merge 28 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
5893398
Pascal implementation
jimmckeeth Jun 17, 2024
619f45b
turn off mormot by default
jimmckeeth Jun 17, 2024
ed7cb40
randomize
jimmckeeth Jun 17, 2024
c313f27
Update README.md for Pascal
jimmckeeth Jun 17, 2024
5ea06d8
Update README.md
jimmckeeth Jun 17, 2024
86e393b
Update uuidv7.pas
jimmckeeth Jun 17, 2024
88632ee
Consolidated and simplified the Delphi/FPC version
jimmckeeth Jun 17, 2024
63e4439
Further simplified
jimmckeeth Jun 17, 2024
cbe3d43
increasing the accuracy of the Delphi version to milliseconds
errorcalc Jun 17, 2024
5b88b28
removed other files
jimmckeeth Jun 19, 2024
989abd9
Merge branch 'nalgeon:main' into main
jimmckeeth Jun 19, 2024
08f14ef
Merge pull request #1 from errorcalc/patch-1
jimmckeeth Jun 19, 2024
869deeb
Squashed commit of the following:
jimmckeeth Jun 19, 2024
cd39918
Delete UUIDv7ConsoleApp.dproj
jimmckeeth Jun 19, 2024
c042750
Delete .gitignore
jimmckeeth Jun 19, 2024
037aece
Corrected GUID generation algorithm to match the specifications from …
skamradt Aug 21, 2024
c2556c1
Corrected problem with calcuation
skamradt Aug 21, 2024
3e80693
Added ability to send an array of bytes for testing purposes and corr…
skamradt Aug 21, 2024
6740d29
Corrected array move to pull all values
skamradt Aug 21, 2024
4815978
Merge branch 'main' of https://github.com/nalgeon/uuidv7 into nalgeon…
jimmckeeth Sep 13, 2024
d682e8a
Merge branch 'nalgeon-main'
jimmckeeth Sep 13, 2024
dd94c4d
fix off-by one-error
jimmckeeth Sep 27, 2024
d6be59f
Update uuidv7.pas
jimmckeeth Sep 28, 2024
d35c727
Merge branch 'main' into main
jimmckeeth Sep 28, 2024
16b7793
Merge pull request #2 from skamradt/main
jimmckeeth Sep 28, 2024
c481b53
GenerateUUIDv7 called the Ex version
jimmckeeth Sep 28, 2024
b1b30eb
Merge branch 'main' of https://github.com/jimmckeeth/uuidv7
jimmckeeth Sep 28, 2024
51cac86
Update uuidv7.pas
jimmckeeth Sep 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions src/uuidv7.pas
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,27 @@ interface
SysUtils, DateUtils;

function GenerateUUIDv7: TGUID;
function GenerateUUIDv7ex(const Timestamp:Int64; const Bytes:TBytes=[]):TGUID;

implementation

function GenerateUUIDv7: TGUID;

function GenerateUUIDv7ex(const Timestamp:Int64; const Bytes:TBytes): TGUID;
var
timestamp: Int64;
randomBytes: array[0..9] of Byte;
uuid: TGUID;
i: Integer;
begin
FillChar(uuid, SizeOf(uuid), 0);
{$IFDEF FPC}
timestamp := DateTimeToUnix(Now) * 1000; // seconds accuracy
{$ELSE}
timestamp := DateTimeToMilliseconds(Now) - Int64(UnixDateDelta + DateDelta) * MSecsPerDay; // millisecond accuracy
{$ENDIF}

// Generate 10 random bytes
for i := 0 to 9 do
randomBytes[i] := Random($FF);
if Length(Bytes) <> 10 then
begin
// Generate 10 random bytes
for i := 0 to 9 do
randomBytes[i] := Random($100);
end
else
move(Bytes[0], RandomBytes[0],10);

// Populate the TGUID fields
uuid.D1 := (timestamp shr 16) and $FFFFFFFF; // Top 32 bits of the 48-bit timestamp
Expand All @@ -39,6 +40,18 @@ function GenerateUUIDv7: TGUID;
Result := uuid;
end;

function GenerateUUIDv7:TGUID;
var
timestamp: Int64;
begin
{$IFDEF FPC}
timestamp := DateTimeToUnix(Now) * 1000; // seconds accuracy
{$ELSE}
timestamp := DateTimeToMilliseconds(Now) - Int64(UnixDateDelta + DateDelta) * MSecsPerDay; // millisecond accuracy
{$ENDIF}
Result := GenerateUUIDv7ex(Timestamp,[]);
end;

// Optionally remove this to make a regular unit for FPC too
{$IFDEF FPC}
var i: Integer;
Expand Down