Skip to content

Commit 05e646b

Browse files
Added support for getting utc datetime from v7 guid.
1 parent f09bec3 commit 05e646b

File tree

3 files changed

+68
-6
lines changed

3 files changed

+68
-6
lines changed

Benchmark/GuidV7Demo.dpr

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ program GuidV7Demo;
66

77
uses
88
System.SysUtils,
9+
System.DateUtils,
910
Spring.Benchmark,
1011
VSoft.UUIDv7 in '..\Source\VSoft.UUIDv7.pas';
1112

@@ -43,11 +44,49 @@ begin
4344
end;
4445

4546

47+
//TODO: move to unit tests!
48+
//hacky little test
49+
50+
procedure test;
51+
var
52+
guid : TGuid;
53+
dt : TDateTime;
54+
v : integer;
55+
begin
56+
guid := TGUID.NewGuid;
57+
writeln(guid.ToString);
58+
if (TUUIDv7Helper.IsV7(guid)) then
59+
Writeln('v7 true')
60+
else
61+
Writeln('v7 false');
62+
63+
v := TUUIDV7Helper.Version(guid);
64+
writeln('version : ' + IntTostr(v));
65+
66+
guid := TUUIDv7Helper.CreateV7;
67+
writeln(guid.ToString);
68+
if (TUUIDv7Helper.IsV7(guid)) then
69+
Writeln('v7 true')
70+
else
71+
Writeln('v7 false');
72+
73+
v := TUUIDV7Helper.Version(guid);
74+
writeln('version : ' + IntTostr(v));
75+
76+
77+
dt := TUUIDv7Helper.CreatedUTC(guid);
78+
writeln(dt.ToString);
79+
80+
81+
end;
82+
83+
4684
begin
47-
Benchmark(BM_CreateV4, 'TGuid.NewGuid');
48-
Benchmark(BM_CreateV7, 'TGuidV7Helper.CreateV7');
49-
Benchmark(BM_CreateV7_DT, 'TGuidV7Helper.CreateV7(datetime)');
50-
// Run the benchmark
51-
Benchmark_Main;
85+
test;
86+
// Benchmark(BM_CreateV4, 'TGuid.NewGuid');
87+
// Benchmark(BM_CreateV7, 'TGuidV7Helper.CreateV7');
88+
// Benchmark(BM_CreateV7_DT, 'TGuidV7Helper.CreateV7(datetime)');
89+
// // Run the benchmark
90+
// Benchmark_Main;
5291
readln;
5392
end.

Benchmark/GuidV7Demo.dproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<FrameworkType>None</FrameworkType>
66
<Base>True</Base>
77
<Config Condition="'$(Config)'==''">Release</Config>
8-
<Platform Condition="'$(Platform)'==''">Win32</Platform>
8+
<Platform Condition="'$(Platform)'==''">Win64</Platform>
99
<ProjectName Condition="'$(ProjectName)'==''">GuidV7Demo</ProjectName>
1010
<TargetedPlatforms>3</TargetedPlatforms>
1111
<AppType>Console</AppType>

Source/VSoft.UUIDv7.pas

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ interface
66
TUUIDV7Helper = record
77
class function CreateV7 : TGuid;overload;static;
88
class function CreateV7(const dt : TDateTime) : TGuid;overload;static;
9+
class function CreatedUTC(const guid : TGUID) : TDateTime;static;
10+
class function IsV7(const guid : TGuid) : boolean;static;inline;
11+
class function Version(const guid : TGuid) : integer;static;inline;
912
end;
1013

14+
1115
implementation
1216

1317
uses
@@ -68,6 +72,14 @@ class function TUUIDV7Helper.CreateV7: TGuid;
6872
result.D4[0] := (result.D4[0] and (not Variant10xxMask)) or Variant10xxValue ;
6973
end;
7074

75+
class function TUUIDV7Helper.CreatedUTC(const guid: TGUID): TDateTime;
76+
var
77+
timestamp : UInt64;
78+
begin
79+
timestamp := (UInt64(guid.D1) shl 16) or UInt64(guid.D2) ;
80+
result := (timestamp / MSecsPerDay ) + UnixDateDelta;
81+
end;
82+
7183
class function TUUIDV7Helper.CreateV7(const dt: TDateTime): TGuid;
7284
var
7385
timestamp : UInt64;
@@ -80,4 +92,15 @@ class function TUUIDV7Helper.CreateV7(const dt: TDateTime): TGuid;
8092
result.D4[0] := (result.D4[0] and (not Variant10xxMask)) or Variant10xxValue ;
8193
end;
8294

95+
class function TUUIDV7Helper.IsV7(const guid: TGuid): boolean;
96+
begin
97+
result := Version(guid) = 7;
98+
end;
99+
100+
101+
class function TUUIDV7Helper.Version(const guid: TGuid): integer;
102+
begin
103+
result := integer(guid.D3 shr 12);
104+
end;
105+
83106
end.

0 commit comments

Comments
 (0)